Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. socket-connection oriented-winsock

socket-connection oriented-winsock

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vanip
    wrote on last edited by
    #1

    Hi , I am infancy to socket programming .I need to connect to an external device through terminalserver4 and i should be able to send command & receive output. Using the following code, i am able to send data to the external device from the two send functions and able to receive data from only the first recv function. The program is blocking at the second recv function. Please check the following code and help me if anywhere went wrong. #define DEST_IP "10.10.1.1" #define DEST_PORT 2001 void CSocktestDlg::process() { WSADATA wsaData; if(WSAStartup(MAKEWORD(1,1),&wsaData)!=0) { AfxMessageBox("WSAStartup failed",MB_OK); exit(1); } else { //AfxMessageBox("WSAStartup succeeded",MB_OK); } int sockfd; struct sockaddr_in dest_addr; //Establishing socket sockfd=socket(AF_INET,SOCK_STREAM,0); dest_addr.sin_family=AF_INET; dest_addr.sin_port=htons(DEST_PORT); dest_addr.sin_addr.s_addr=inet_addr(DEST_IP); memset(&(dest_addr.sin_zero),'\0',8); //For connecting if(connect(sockfd,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr))==0) { //AfxMessageBox("Connected to remote system successfully",MB_OK); } else { AfxMessageBox("Unable to connect to remote machine",MB_OK); } //For sending data CString code= "2,76,69,68,77,73,44,73,77,68,69,73,77,68,69,0,217,105,3"; int len,bytes_sent,n,i; CTokenEx CtlArraySpliter; CStringArray strArray; CtlArraySpliter.Split(code,",",strArray,TRUE); for(i = 0; i < strArray.GetSize(); i++ ) { //len=strArray.GetAt(i).GetLength(); n = atoi(strArray.GetAt(i)); bytes_sent=send(sockfd,(char *) &n,sizeof(int),0); } strArray.RemoveAll(); AfxMessageBox("Sent message through socket to remote IP",MB_OK); Sleep(5000);

    I 1 Reply Last reply
    0
    • V vanip

      Hi , I am infancy to socket programming .I need to connect to an external device through terminalserver4 and i should be able to send command & receive output. Using the following code, i am able to send data to the external device from the two send functions and able to receive data from only the first recv function. The program is blocking at the second recv function. Please check the following code and help me if anywhere went wrong. #define DEST_IP "10.10.1.1" #define DEST_PORT 2001 void CSocktestDlg::process() { WSADATA wsaData; if(WSAStartup(MAKEWORD(1,1),&wsaData)!=0) { AfxMessageBox("WSAStartup failed",MB_OK); exit(1); } else { //AfxMessageBox("WSAStartup succeeded",MB_OK); } int sockfd; struct sockaddr_in dest_addr; //Establishing socket sockfd=socket(AF_INET,SOCK_STREAM,0); dest_addr.sin_family=AF_INET; dest_addr.sin_port=htons(DEST_PORT); dest_addr.sin_addr.s_addr=inet_addr(DEST_IP); memset(&(dest_addr.sin_zero),'\0',8); //For connecting if(connect(sockfd,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr))==0) { //AfxMessageBox("Connected to remote system successfully",MB_OK); } else { AfxMessageBox("Unable to connect to remote machine",MB_OK); } //For sending data CString code= "2,76,69,68,77,73,44,73,77,68,69,73,77,68,69,0,217,105,3"; int len,bytes_sent,n,i; CTokenEx CtlArraySpliter; CStringArray strArray; CtlArraySpliter.Split(code,",",strArray,TRUE); for(i = 0; i < strArray.GetSize(); i++ ) { //len=strArray.GetAt(i).GetLength(); n = atoi(strArray.GetAt(i)); bytes_sent=send(sockfd,(char *) &n,sizeof(int),0); } strArray.RemoveAll(); AfxMessageBox("Sent message through socket to remote IP",MB_OK); Sleep(5000);

      I Offline
      I Offline
      includeh10
      wrote on last edited by
      #2

      in general, each waiting function (such as recv()) should be put in a thread, because u don't know if or when other-side sends message to you. so create threads for your waiting functions.


      A special image tool for C++ programmers, don't miss it! The world unique Software Label Maker is here for you and me ... A nice hyper tool for optimizing your MS html-help contents.


      V 1 Reply Last reply
      0
      • I includeh10

        in general, each waiting function (such as recv()) should be put in a thread, because u don't know if or when other-side sends message to you. so create threads for your waiting functions.


        A special image tool for C++ programmers, don't miss it! The world unique Software Label Maker is here for you and me ... A nice hyper tool for optimizing your MS html-help contents.


        V Offline
        V Offline
        vanip
        wrote on last edited by
        #3

        here, i think there is no waiting problem.Because the external device sends message immediately after sending command code. Is there any other wrong in my code? For the first time it is responding immediately. For the second time not.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups