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!!!