-
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...
-
What is the problem?
Kuphryn
-
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.
-
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!!! :D