In TCP communications a TCP client program should always
end the connection with a EOF packet; basically a TCP packet
with the RST (reset) flag set in the TCP headers and no
data content. I did some experiments and found that even if
I killed the client hard (w/ a "kill -9" or equivalent)
that I still got the RST packet on the server. The RST packet must have
been sent by the program as it was dying or by the OS when
it discovered the abandoned sockethandle. Now what if the
client program dies very hard (like if the machine it is on loses power)
or if network connectivity between the client and your server is lost.
In those cases no RST packet would ever be reveived by your server so it would never realize
that the connection was closed. In that case you would only
realize that the connection was dead when a timeout occurred on the connection or
by the SIGPIPE signal mentioned above. No SIGPIPE or other signal
is sent when a client closes a connection normally. | [reply] |
In the case of an abnormal (before the transaction is
complete) close, you can try to catch a SIGPIPE signal.
You'll get a SIGPIPE when you try to write (print) to
a closed socket, so this is probably your best bet. | [reply] |