SFML community forums

Help => Network => Topic started by: Grimlen on October 05, 2012, 11:10:44 pm

Title: Check when someone has disconnected from my UDP socket?
Post by: Grimlen on October 05, 2012, 11:10:44 pm
In my UDP server, I'm trying to find out when a client has "disconnected".
Obviously, this makes more sense if I were using a TCP socket but UDP is not "connected".

One way I thought of was having the server constantly send a message to the client,
and if it was not able to contact it, we determine that client to be "disconnected", but this seems
highly inefficient.

Does anyone know the proper way to test if someone is disconnected?

I'm not talking about when the user closes the window, I mean if for any reason the user is actually
NOT in the server (i.e., client crashed, client's internet d/c, etc.)
Title: Re: Check when someone has disconnected from my UDP socket?
Post by: Laurent on October 05, 2012, 11:33:10 pm
Quote
One way I thought of was having the server constantly send a message to the client,
and if it was not able to contact it, we determine that client to be "disconnected", but this seems
highly inefficient.
This is the only way (it's called "ping-pong" or "heartbeat"), and it's not inefficient, it's only a few bytes every N seconds.
Title: Re: Check when someone has disconnected from my UDP socket?
Post by: Grimlen on October 06, 2012, 08:48:23 am
All right thanks for elaborating that.
Exactly how often do you think I should be pinging? Or I suppose it wouldn't really matter,
just as long as I'm not using up a ton of bandwidth?
Title: Re: Check when someone has disconnected from my UDP socket?
Post by: Laurent on October 06, 2012, 09:05:12 am
Quote
Exactly how often do you think I should be pinging? Or I suppose it wouldn't really matter,
just as long as I'm not using up a ton of bandwidth?
It depends on your needs. It's not really important unless your application needs to do something specific quickly after a disconnection. And don't forget that the total detection time will be greater than the sending period: after sending the packet, you have to wait for the reply, so you need to choose a timeout to decide when the client is considered inactive. And don't forget that UDP is allowed to drop datagrams, so if you want a robust implementation you might need to send multiple ping-pong packets. You can also apply a small optimization: enable the ping-pong messages only when no data is exchanged with the client (if you receive data, you already know that the client is alive).
Title: Re: Check when someone has disconnected from my UDP socket?
Post by: Qix on October 08, 2012, 12:30:21 am
It's best to not expect to hear anything on a UDP socket.

How many professional games structure their networking is to have two connections to the client; one TCP (for chat and basic, low-cost data transfer) and one for UDP (for everything else that needs to be as fast as possible).

If the TCP side of the connection disconnects, it's safe to assume the UDP connection did too. You could also implement a heartbeat (as Laurent suggested) to make things even safer.