Re: How to detect if the other end of the TCP connection has gone offline



Karjala wrote:
I'm writing a simple client-server application, and I'm trying to find a way to tell if the other end of an open TCP connection has gone offline.

If the other side has closed its end of the connection, you detect this by reading from the socket and receiving EOF (0 bytes read), or writing to the socket and getting SIGPIPE.


If the other side has crashed (not just process terminated, but server itself has crashed), or the network has gone down between the two, you either need to use some kind of timeout (based on what a "reasonable" response time would be), or use the SO_KEEPALIVE socket option. With the latter, the kernel will probe the remote side after some period of inactivity and shutdown the connection if the other side doesn't respond.
.




Relevant Pages

  • Re: non blocking sockets
    ... >> determined that the socket was ready. ... > Upon successful completion, the pselector selectfunction shall ... > arguments to indicate which file descriptors are ready for reading, ... > descriptor shall be considered ready for reading.) ...
    (comp.unix.programmer)
  • Re: Multithread safety
    ... I want to create myself a library usable both in console apps and ... that calls to reading and writing to the socket are a little serialized.. ... I would need a good article on Winsock specific functions I guess:) ...
    (microsoft.public.win32.programmer.networks)
  • Re: strange multithreaded read() behavior
    ... will a client socket test true for reading all the time? ...
    (comp.unix.solaris)
  • Re: strange multithreaded read() behavior
    ... will a client socket test true for reading all the time? ...
    (comp.unix.programmer)
  • Re: strange multithreaded read() behavior
    ... then other jobs won't be able to do new I/O. ... still be in select when the worker finishes and the socket won't be selected ... > socket test true for reading 7 times or just one time? ... Master thread calls select, sees socket 1 is ready for reading. ...
    (comp.unix.solaris)