[SDL] Re: [SDLnet] server shutdown produces strange client behavior (Windows XP)
Benjamin Deutsch
ben at fictiongroup.de
Wed Feb 9 10:33:53 PST 2005
Hello,
>> ---
>> while (true) {
>> int nr = SDLNet_CheckSockets(set, 0);
>> int rdy = SDLNet_SocketReady(server);
>>
>> if (( nr > 0) && (rdy != 0)) {
>> cout << "nr: " << nr << "rdy: " << rdy << endl;
>> // receive and print data
>> }
>> }
>> ---
>>
>> Whenever the server suddenly closes, the client gets stuck in a loop
>> telling me both nr and rdy are 1. So there is activity on the server
>> socket, according to my client. But there is no data to receive. Is
>> there a way for my client to handle this problem? I would like my
>> client to say something like 'server offline' and quit. But I haven't
>> found any function that shows me whether a connection has gone
>> offline.
>
> I did some additional testing, and it seems that when the client
> suddenly closes the server gets into this loop as well. At the client,
> SDLNet_CheckSockets(set, 0) continuously returns 1 and
> SDLNet_SocketReady(server) too. But the received message is always 0
> bytes long. Is this normal behavior? When a program is stuck in a loop
> continuously receiving 0 byte messages, does this mean the other side
> has incorrectly shut down the connection?
Incorrectly or correctly. On Win32 and Unix-like platforms, SDL_net uses
recv() for reading from the socket (check the CVS code with the web
interface: http://www.libsdl.org/cgi/cvsweb.cgi/SDL_net/ -> lates
version of SDL_TCP). Try looking at the man page for recv, also
available through Google: http://www.google.com/search?q=man+recv
Basically, recv has three possible return values r:
r > 0 : Exactly r bytes (possibly less than you asked for) were read
r = 0 : "End of file", the connection has been closed
r < 0 : An error has occurred.
So a socket which has been closed will immediately return 0, and
therefor is considered "ready" by select(). I think it's platform
dependent how often 0 is returned before it becomes an error to read
from the socket. Generally, after the first 0, you should assume the
connection has been closed, and close the socket on the client side.
By the way, an excellent guide to networking is Beej's guide, if you
don't know it yet: http://www.ecst.csuchico.edu/~beej/guide/net/ . It's
Unix-centric, but the same principles apply to Win32 and many other
operating systems, so it's a worthwhile read, in my opinion.
Good luck,
Benjamin Deutsch
More information about the SDL
mailing list