[SDLnet] server shutdown produces strange client behavior (Windows XP)

Hey all,

I’m experimenting a bit with SDLnet. I have made a simple multi-client
server and can connect to it with my own client / telnet. The server
sends a message ‘hi’ to each client whenever a new client connects.
However, if I kill my server process the clients start to behave really
wierd. (Windows) Telnet gives the following output:—
Connection Lost.

Press any key to continue…

This is what I want to achieve with my client as well. But when the
server suddenly closes my client gets into a infinite loop. This is my code:


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. Is there
such a thing? This is my first attempt at network programming, so don’t
flame me please :slight_smile: thanks in advance

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?

writser wrote:> Hey all,

I’m experimenting a bit with SDLnet. I have made a simple multi-client
server and can connect to it with my own client / telnet. The server
sends a message ‘hi’ to each client whenever a new client connects.
However, if I kill my server process the clients start to behave really
wierd. (Windows) Telnet gives the following output:


Connection Lost.

Press any key to continue…

This is what I want to achieve with my client as well. But when the
server suddenly closes my client gets into a infinite loop. This is my
code:


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. Is there
such a thing? This is my first attempt at network programming, so don’t
flame me please :slight_smile: thanks in advance

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