Thread: recv Thread

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    recv Thread

    Can anybody please tell me whats wrong with the following code, its used to receive messages from a server.

    Initialization:

    Code:
    	int ThreadId;
    	hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceiveMessages, NULL, 0, (LPDWORD)&ThreadId);
    Actual Routine:

    bRunning = true;
    bConnected = true;
    Server is a socket which is what i use to send to the server. (Sending works but not receiving)


    Code:
    void ReceiveMessages()
    {
    	while (bRunning)
    	{
    		if (!bConnected)
    			continue;
    
    		Server = Client;
    
    		int nBytes;
    
    		unsigned long messageSize;
    
    		nBytes = recv(Server, (char*)&messageSize, sizeof(messageSize), 0);
    
    		if (nBytes == SOCKET_ERROR) 
    		{
    			int Error = WSAGetLastError();
    
    			if (Error == WSAECONNRESET) 
    			{
    				//closesocket(Server);
    
    				Notification(Server, CLIENT_DISCONNECT);
    
    				bConnected = false;
    
    				StopClient(Client);
    
    				EnableWindow(Button_Connect, true);
    				EnableWindow(Button_Disconnect, false);
    
    				continue;
    			} 
    			else
    			{
    				MessageBox(NULL, "Receive Failed", "", MB_OK);
    				//bRunning = false;
    				break;
    			}
    		}
    
    		if (nBytes == 0) 
    		{
    			Notification(Server, CLIENT_DISCONNECT);
    
    			bConnected = false;
    
    			StopClient(Client);
    
    			EnableWindow(Button_Connect, true);
    			EnableWindow(Button_Disconnect, false);
    
    			continue;
    		}
    
    		messageSize = ntohl(messageSize);
    
    		nBytes = recv(Server, mBuffer, messageSize, 0);
    
    		if (nBytes == SOCKET_ERROR)
    			break;
    
    		mBuffer[messageSize] = '\0';
    		HandleMessage(mBuffer);
    	}
    }
    Notification() is just a function i use to notify myself that i am connected to the server or disconnected.

    HandleMessage() is the function i use to process the received data but its never reached that far...

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What is the problem?

    Kuphryn

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You should be checking that you are recv()'ing the correct number of bytes. Don't assume that just because you asked for 10 (example only), that's what you got.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    37
    The recv should definitely check the number of bytes received. It looks like you are expecting 4 bytes on the first recv and messageSize on the second.

    Code:
    ///////////////////////////////////////////////////////////////////////////////
    // Reads a specified number of bytes from a socket.  This call will not return
    // until the specified number of bytes are read or there is an error.
    //
    // INPUT:
    // SOCKET soc - the socket to read from
    // int numBytesToRead - the number of bytes to read from the socket.
    //
    // OUTPUT:
    // char *buffer - buffer containing data read from the socket.
    //
    // RETURNS:
    // TRUE on success and FALSE on error.  Call WSAGetLastError() to get the
    // error code.
    ///////////////////////////////////////////////////////////////////////////////
    BOOL SafeRecv(SOCKET soc, char *buffer, int numBytesToRead)
    {
    	int totalBytesRecvd = 0;
    
            while (true)
    	{
    		//temp holder for the number of bytes read per recv() call
    		int numBytes = 0;
    
    		//calculate how many more bytes are left to read
    		int bytesLeft = numBytesToRead - totalBytesRecvd;
    
    		//Read into the correct offset in the buffer the number of bytes
    		//left to read.
    		numBytes = recv(soc, buffer + totalBytesRecvd, bytesLeft, 0);
    
    		//Was there an error?
    		if (numBytes == SOCKET_ERROR)
    		{
    			//YES
    			return FALSE;
    		}
    
    		//Increment the number of bytes we have received so far
    		bytesRecvd += numBytes;
    
    		//Have we received everything?
    		if (numBytes == numBytesToRead)
    		{
    			//YES
    			break;
    		}
    	}//while
    
    	return TRUE;
    }//SafeRecv
    This code has not been compiled or tested!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM
  5. Replies: 12
    Last Post: 05-17-2003, 05:58 AM