If recv()
returns 0, the other end has actively and gracefully closed the connection.
If recv()
returns -1, there has (possibly) been an error on the connection, and it is no longer usable.
This means you can tell the difference between the peer closing the connection, and an error happening on the connection. The common thing to do in both cases is to close()
your end of the socket.
There is 2 more points to consider though:
In the case of recv()
returning -1, you should inspect errno, as it might not indicate a real error.
errno
can be EAGAIN/EWOULDBLOCK
if you have placed the socket in non-blocking mode or it could be EINTR
if the system call was interrupted by a signal. All other errno
values means the connection is broken, and you should close it.
TCP can operate in half duplex. If the peer has closed only its writing end of the connection, recv()
returns 0 at your end. The common thing to do is to consider the connection as finished, and close your end of the connection too, but you can continue to write to it and the other end can continue to read from it. Whether to close just the reading or writing end of a TCP connection is controlled by the shutdown()
function.