I know this question has been discussed several times, but I didn‘t manage to detect whether a socket disconnected in C / C++ on Unix. Is there any way to do so ?
I assume that the socket might be closed by the peer.
It very depends on where you want to detect the disconnection.
If you are in the blocking recv
:
manpage
These calls return the number of bytes received, or -1 if an error occurred. In the event of an error, errno is set to indicate the error.
When a stream socket peer has performed an orderly shutdown, the return value will be 0 (the traditional "end-of-file" return).
If you want to know on send
:
manpage
On success, these calls return the number of bytes sent. On error, -1 is returned, and errno is set appropriately.