🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do i detect when a Client Disconnects/Crashes

Started by
7 comments, last by Gachuk 23 years, 4 months ago
Hi this has been bugging me, i have created a Non Blocking Server in winsock. Its all great. But i need to be able to detect when a client quits or crashes or anything happens so its not actually there. Id like suggesions on how to go about this perhaps some code too Thankyou Gachuk
Gachuk
Advertisement
With the project I''m working on the client sends an exit message to the server when it''s logged off.

For clash/lost-connection victims, what you could do is send out occassional messages to everybody checking to see if they''re still connected. If they aren''t you log them off how they were supposed to have been.

For mine right now if you exit improperly you have to log back in and re exit. Otherwise your character remains online to be used as target practice if anyone notices.

Ben
http://therabbithole.redback.inficad.com
Most interactive TCP-based protocols send a periodic PING messages to the remote host to detect broken connections. If the remote side doesn''t respond within an arbitrary timeout, or if you get an error when trying to send data, then you know that he is gone.

With BSD Sockets, you get a SIGPIPE when the connection breaks, and an EPIPE error when you try to send on it. I don''t know the WinSock equivalents.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.
It''s also been my experience that when a client returns something <= 0 from a recv() call, it''s been disconnected.

Anthracks
I"m confused on how i would do this by detecting only one ip address and detecting only one ip address. read my latest post about bein confused with UDP!
ALL YOUR BASE ARE BELONG TO US!!!!
Well, i been told that a TCP server already sends a keepalive message, and theres a function i can use to determine, this could be wrong in which case prolly doing a PING would be best.

Gachuk
Gachuk
TCP takes care of this, as was already said.

For UDP connections, I don''t recommend sending ICMP pings, because ICMP response can be turned off. Additionally, an ICMP ping won''t keep a NAT router''s translation table for UDP connections alive. So you''re better off sending a custom PING packet over your UDP "connection".
Just to make sure you don''t make a mistake

cu,
Prefect

---
Sanity is the trademark of a weak mind.
Widelands - laid back, free software strategy
I should clarify my post, as it has caused some confusion. By PING, i did not mean ICMP ping -- I meant any message sent at idle time to keep the connection alive.

TCP does not automatically tell you when a connection has broken, because it only knows that a connection has broken is if it tries to send data and gets no response. If you don''t send any data, then you never find out that the other guy is gone -- he just seems *really* quiet. Now, you can enable the SO_KEEPALIVE on the socket, which will tell TCP to send it''s own tickle packets (using ICMP) every so often -- and then it will eventually discover that the connection has broken and report it to you.

When I said that most TCP-based protocols send a PING packet, I meant higher level protocols layered *on top of* TCP. For example, IRC sends a "PING\r\n" message and if it doesn''t get back a "PONG\r\n" in 2 minutes or so, then it assumes the other guy is gone. This is a much more portable solution than SO_KEEPALIVE because it doesn''t rely on ioctl() type hooks.

Meanwhile, UDP doesn''t even support SO_KEEPALIVE since there is no actual connection in place -- even if you call connect() on your UDP endpoint. You will need to send your own PING-alike (maybe just a zero-byte packet) using UDP. And definitely don''t rely on ICMP for this.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.
I should clarify my post, as it has caused some confusion. By PING, i did not mean ICMP ping -- I meant any message sent at idle time to keep the connection alive.

TCP does not automatically tell you when a connection has broken, because it only knows that a connection has broken is if it tries to send data and gets no response. If you don''t send any data, then you never find out that the other guy is gone -- he just seems *really* quiet. Now, you can enable the SO_KEEPALIVE socket option, which tells the TCP socket to send it''s own tickle packets (using ICMP) every so often -- and then it will eventually discover that the connection has broken and report it to you.

When I said that most TCP-based protocols send a PING packet, I meant higher level protocols layered *on top of* TCP. For example, IRC sends a "PING\r\n" message and if it doesn''t get back a "PONG\r\n" in 2 minutes or so, then it assumes the other guy is gone. This is a much more portable solution than SO_KEEPALIVE because it doesn''t rely on ioctl() type hooks.

Meanwhile, UDP doesn''t even support SO_KEEPALIVE since there is no actual connection in place -- even if you call connect() on your UDP endpoint. You will need to send your own PING-alike (maybe just a zero-byte packet) using UDP. And definitely don''t use your own ICMP packets for this.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.

This topic is closed to new replies.

Advertisement