Thread: Blocking Sockets Issue

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    Blocking Sockets Issue

    Hey there,
    I've had this problem for a little bit, and it is now time to track it down, as its become obscenely troublesome.

    Premise:
    I am using sockets under WinSock2 to retrieve an XML page and parse it for use.

    Problem:
    My socket recv() call intermittently blocks indefinately. I cannot determine a pattern to when it decides its going to block forever, but I speculate it has to do with the response time of the server? There server is a off-site box that operates with (an average) of less than 50ms ping to the development box.

    Details:
    Code:
    	if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) )
    		return;
    	if ( ( ConnectSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == INVALID_SOCKET )
    		return;
    	if ( connect( ConnectSocket, (sockaddr *) &clientService, sizeof( clientService ) ) == SOCKET_ERROR )
    		return;
    	/* ... */
    	send( ConnectSocket, outBuffer, strlen( outBuffer ), 0 );
    	shutdown( ConnectSocket, 0x01 );
    	do
    	{
    		bytes = recv( ConnectSocket, inBuffer, 1024, 0 );
    		if ( bytes > 0 )
    		{
    			/* ... */
    		}
    		else if ( bytes < 0 )
    		{
    			Disconnect( );
    			return WSAGetLastError( );
    		}
    		/* ... */
    	} while ( bytes > 0 );
    	/* ... */
    	closesocket( ConnectSocket );
    	WSACleanup( );

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why are you doing shutdown() immediately after send, and BEFORE doing a recv ?

    How are outBuffer and inBuffer declared?

    Do you attempt to add a \0 to inBuffer, even though it may be full. Doing so seems like a buffer overflow if you have char inBuffer[1024];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by Salem View Post
    Why are you doing shutdown() immediately after send, and BEFORE doing a recv ?
    I am telling the socket that no more data is to be written to it.
    shutdown Function (Windows)

    Quote Originally Posted by Salem View Post
    How are outBuffer and inBuffer declared?
    One's a fixed array 1024, the other is a char* pointer of variable length depending on the length of the data I'm sending /out/.

    Both operate as expected under run conditions.

    Quote Originally Posted by Salem View Post
    Do you attempt to add a \0 to inBuffer, even though it may be full. Doing so seems like a buffer overflow if you have char inBuffer[1024];
    inBuffer operates as expected under run conditions.

    My issue is recv() intermittantly decides to block forever, my assumption is that its based on the response time of the server, but I have no data to correlate to that (and correlation does not imply causation, anyway).

    Please post back with any ideas you may have

    Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you DO have a buffer of 1024.
    And if you receive 1024 characters and do inBuffer[bytes] = '\0' to make it a string, you HAVE A BUFFER OVERFLOW.

    Where does that \0 go? Is that the cause of your problems?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by Salem View Post
    So you DO have a buffer of 1024.
    And if you receive 1024 characters and do inBuffer[bytes] = '\0' to make it a string, you HAVE A BUFFER OVERFLOW.

    Where does that \0 go? Is that the cause of your problems?
    Sorry, I should have made it clear that I am not null terminating the string; I don't think recv() would write past the 1024 limit I specified? I do not have much experience with Sockets; The rest of the language I have a fair grip on

    This is not the error I'm looking for

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    How do you bind to the socket? I'm pretty sure that if you don't specify the socket as non-blocking then recv() is going to block when there is no data to receive. That's how it works.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    All I can suggest at the moment is you run Wireshark · Go deep. to observe the connection, and try and figure out the difference between "good" and "bad" cases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    Thumbs up

    Oi. This is embarassing, the problem [seems] to be resolved. By some bizzare twist of logic the code I had written was *ahem* poorly interacting with other code I wrote, causing the socket to open and close a couple hundred times a second.

    Resolved it when I was mucking about with binding (which doesn't seem to be needed if the connection being established is a client and not a server? or something?)

    Anyway, thank you guys for your help, you helped me arrive at the solution

    Regards,
    Teeg

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    You need to bind to a socket if you are going to listen on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. Pros and Cons of blocking versus non-blocking sockets?
    By abachler in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-08-2008, 06:52 AM
  3. Sockets, multi home, multicast, security, complicated issue
    By ekymk in forum Networking/Device Communication
    Replies: 6
    Last Post: 08-13-2004, 02:12 AM
  4. Non blocking sockets
    By osal in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-23-2004, 06:16 PM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM