Original Post
OK, I picked up TCP/IP in about an hour a day ago. I have already written a very basic chat server which works great. However, the client I am now working on, wants to give me problems. If I use FD_SET() each loop to put my socket into the fd_set variable, it will read data from the socket whenever it is recieved, but if I only set it before the main loop, it will recieve the first message and nothing more! Here is a sample.
int WINAPI WinMain(...)
{
int SocketFD;
fd_set ReadSet;
//Grab a socket, fill in fields, etc
//Connect no problem
FD_ZERO(&ReadSet);
FD_SET(SocketFD, &ReadSet);
while(!Done)
{
//Handle Windoze messages
select(SocketFD + 1, &ReadSet, NULL, NULL, &TV);
if(FD_ISSET(SocketFD, &ReadSet))
{
if((numBytes = recv(SocketFD, MSGBuffer, sizeof(MSGBuffer), 0)) <= 0)
{
if(numBytes == 0)
{
MessageBox(MainWnd, "Lost connection to host.", "Error", MB_OK);
shutdown(SocketFD, 2);
WSACleanup();
return Message.wParam;
}
else
{
MessageBox(MainWnd, "Error in recieve.", "Error", MB_OK);
shutdown(SocketFD, 2);
WSACleanup();
return Message.wParam;
}
}
AddLog(MainWnd);
}
}
return Message.wParam;
}
Doing it this way only recieves one message and no more. Now if I put the FD_* macro calls into the main loop above the select() function, it tries to get a message at every loop, which KILLS my CPU (it can take up to 2min to redraw the screen on a P3/866!). What am I not doing right? The server application works fine with recv() and send().
-The Great Sephiroth