I have a simple client server program that I made but the main issue is that once a connection is established between the client and server, if the client closes the program, the server repeatedly executes the last message and that creates huge problems sometimes. So what I want to is if there is any function that I can call to get the state of a SOCKET structure so if the client closes the program, the server will know to stop. I just need the function what to look for from the function for a bad socket. By the way I am writing this program in Win32 c. I tried if(mySocket==SOCKET_ERROR) which didn’t seem to work… unless I used it wrong. I’m just beginning networking.
if(!sockServer.RecvData( recMessage, STRLEN )){return 0;}
// where
bool Socket::RecvData( char *buffer, int size )
{
int i = recv( mySocket, buffer, size, 0 );
if(!i){return false;}
buffer[i] = '\0';
return true;
} //this isn't working
If the peer closes the socket,
read()andrecv()will return zero. You must be ignoring that if ‘the server repeatedly executes the last message’, which of course would ‘create huge problems’ not just ‘sometimes’ but always.EDIT 1: You’re also making another elementary TCP programming error: you are assuming that you receive an entire message in a single read. There are no messages in TCP, it’s just a byte stream, and no 1::1 correspondence between writes and reads (
send()andrecv()).recv()can return zero or as few as 1 bytes. You have to organize your own message boundaries, and you have to loop until you receive an entire message you can deal with.EDIT 2: You are also ignoring all errors in your receive method. You must look for
recv()returning-1, and not proceed with the logic that processes the data you were trying to read. At a minimum you should callperror()or checkerrnoor in WinsockWSAGetLastError().When you get either an error or an EOS you must stop looping, close the socket, forget about the connection, and forget about this client.