I am having client/server application and i want to detect if client is alive.

So i know that send() will return me null but it will not inform my server that client has been disconnected.

Way i came up is each N seconds send "Are you alive" message from server to client but is that good solution?

  • 2
    \$\begingroup\$ This is called a keepalive. Have you observed any problems with using this approach? \$\endgroup\$ – DMGregory Jan 14 at 4:39
  • \$\begingroup\$ No and i am doing it like that but somewhere i read it is not good idea to do it like that so wanted to ask what is other solution \$\endgroup\$ – Aleksa Ristic Jan 14 at 11:11
  • 1
    \$\begingroup\$ Who said it was not good, and why? We can solve a problem better if we know what problem it is we're solving. \$\endgroup\$ – DMGregory Jan 14 at 12:42

This is a good solution, since it allows both sides to know that their partner is alive.

Call it "keepalive" or "hearbeat".

Added benefit, when done frequently (1/sec) you can monitor lag / ping.

  • 1
    \$\begingroup\$ Another benefit is when you try to send a heartbeat message to the "dead" client it will cause a SocketError on your end, forcing it to close. Otherwise, it may hang for a very long time. \$\endgroup\$ – badunius Oct 2 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.