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