When server crash or just call shutdown(fd, SHUT_WR)
, client all read()
return 0
.
How to distinguish them?
I will appreciate it if you help me.
When server crash or just call shutdown(fd, SHUT_WR)
, client all read()
return 0
.
How to distinguish them?
I will appreciate it if you help me.
The only reliable way for a peer to distinguish between the other peer crashing vs intentionally closing the connection is if the communication protocol defines a goodbye message for that purpose.
If possible, each peer should send a protocol-defined goodbye message when it is closing its side of the connection on purpose. If a peer detects the connection has been closed/lost and such a message was not received beforehand, it can assume the other peer crashed and act accordingly (ie, a client can reconnect with a new socket, etc).
When a process terminates, the OS will close any active connections the process still had open. But, whether the surviving peer will receive a "graceful" close notification or an "abnormal" socket error is largely implementation-defined by the respective network stacks. Which is why, if distinction is important, don't rely solely on the type of connection closure detected. Make your peers be explicit about their intention to close a connection.