Socket handling on internet disconnect

Hello guys,

i need to know how i can check wheather the server, connected to by the client is still reachable (from the client)and add an exception on internet disconnect. Ive found several results, but there not working fine for me. Something like a keepalive check is done by the server, but theres no option to send keepalive beacons to the server, cause my client is always listening to its server.


Thanks in advance,

You can call setsockopt() to enable keep-alive packets. It does not matter if you're client of server. The stack will handle the timeout checking. If the other side does not respond to the keep-alive packet, you will get an error on your next operation. If you have a read pending on the server, the read will terminate immediately with an error.
int getsockopt(
  (SOCKET) s,      // descriptor identifying a socket 
  (int) SOL_SOCKET,   // level
  (int) SO_KEEPALIVE, // optname
  (char *) optval, // output buffer,
  (int) optlen,  // size of output buffer
There's also the socket timeout value that you can set as the default is probably not suitable in this case; SO_TIMEOUT.
Registered users can post here. Sign in or register to post.