Socket Programming C/C++ - recv function hangs?

25,335

Solution 1

Your code will block in the recv call until the server sends any data back. Since that isn't happening, perhaps you should ask yourself if your client sent the complete request. The server will probably not start sending the response until the complete request is received.

If your client/server protocol is text based, like HTTP or SMTP, are you sure your response is correctly terminated? The server may be expecting CR+LF ('\r\n') instead of just LF ('\n') or it expects an empty line to terminate the request:

char *request = "GET /index.html HTTP/1.1\r\nHost: www.example.com\r\n\r\n";

PS. You don't declare or initialise 'a' in your code snippet.

Solution 2

Your code will block in the recv call until the server sends any data back. Since that isn't happening, perhaps you should ask yourself if your client sent the complete request. The server will probably not start sending the response until the complete request is received.

If your client/server protocol is text based, like HTTP or SMTP, are you sure your response is correctly terminated? The server may be expecting CR+LF ('\r\n') instead of just LF ('\n') or it expects an empty line to terminate the request:

char *request = "GET /index.html HTTP/1.1\r\nHost: www.example.com\r\n\r\n"; PS. You don't declare or initialise 'a' in your code snippet." by notacat

I was working on my copy on early winsock 1.0 and 1.1 implementation in c++ on win. My code worked all the way to recv() call, and then just hang there... I knew about blocking \Non blocking sockets, but that was not the solution I required. Since I saw my request in wireshark or similar I knew that server was receiving my end of string request on port 80, and my code freezing up on recv() call due to non communication between my client and server on 'net. Which led me to believe that I did not submit proper request. well, author ( notaket ) was on spot on!. As soon as I changed to his request( "GET /index.html HTTP/1.1\r\nHost: www.example.com\r\n\r\n") , all seem to work like a charm!. Thanks!!

Solution 3

recv should hang till u get response form the server, have you stated that the response is being send.
Try wireshark or something like that to sniff the network and see if the actual response is coming or not.

Solution 4

Ummm.... why did you shutdown the socket....

bytes_send = send(sock, a, strlen(a), 0);
    bytes_send = shutdown(sock, 1); /*** That I think is the problem! ***/
 bytes_recieved = recv(sock, recv_data, 4096, 0);

And yet you continued in the code to receive from the same socket that was shutdown??

Hope this hints you in the right direction.

Solution 5

Try using the the Non blocking mode. i.e. recv will return if no data is there to be read, so handle this case (in a while loop or with select. that is up to you)

For more on recv, assuming you are on a POSIX compliant system) read this http://www.opengroup.org/onlinepubs/009695399/functions/recv.html

Share:
25,335

Related videos on Youtube

Vishal
Author by

Vishal

Updated on April 15, 2022

Comments

  • Vishal
    Vishal about 1 month

    My recv function hangs while getting reponse from server.

    Client side code in c/c++:

    void sockStuff() {
     int sock, bytes_recieved,bytes_send;
     char send_data[1024], recv_data[4096];
     struct hostent *host;
     struct sockaddr_in server_addr;
     host = gethostbyname("127.0.0.1");
     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("SocketError");
      exit(1);
     }
     server_addr.sin_family = AF_INET;
     server_addr.sin_port = htons(50500);
     server_addr.sin_addr = *((struct in_addr *) host->h_addr);
     bzero(&(server_addr.sin_zero), 8);
     if (connect(sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr))
       == -1) {
      perror("ConnectToError");
      exit(1);
     }
        bytes_send = send(sock, a, strlen(a), 0);
        bytes_send = shutdown(sock, 1);
     bytes_recieved = recv(sock, recv_data, 4096, 0); //Where the program hangs??
     recv_data[bytes_recieved] = '\0';
     printf("\nRecieved data = %s ", recv_data);
     cout << endl << endl;
     shutdown(sock,2);
    }
    

    My Client is C/C++ and server side is OpenEdge Progress. Please see code and suggest what went wrong with this.??

  • Vishal
    Vishal over 12 years
    Actually after recv() hangs, i tried ctrl+alt+del.By doing this task manager opens but i close this task manager(not killed any process) and suddenly it gives me reponse once.But my program is still in freezed state.
  • Vishal
    Vishal over 12 years
    Thnx. Tom for replying. But shutdown(sock, 1); is not closing socket.It is only telling that send is disallowed(nothing more to send.).Even i remove this line, my recv() hangs.
  • t0mm13b
    t0mm13b over 12 years
    @Vishal: Ok...developerweb.net/forum/showthread.php?t=2940 - there's an interesting discussion abotu when to use shutdown.. The code is in synchronous mode, it could be possible that the receive of data already has happened prior to shutdown and hence hang. Have you thought of that...? Sorry if I am not much of a help here... :(