The accepted answer does not work if you disconnect the network cable. Or the server is crashing. Or your router crashes. Or if you forget to pay your Internet bill. Set TCP save options for better reliability.
public static class SocketExtensions { public static void SetSocketKeepAliveValues(this Socket instance, int KeepAliveTime, int KeepAliveInterval) {
The time value sets the timeout since the last data was sent. He then tries to send and receive a keep-alive packet. If it fails, it retries 10 times (the number is hard-coded with Vista AFAIK) in the interval specified before deciding to disconnect the connection.
Thus, the above values ββwill lead to the detection of 2 + 10 * 1 = 12 seconds. After that, any read operations / wrtie / poll will not be performed on the socket.
toster-cx Sep 23 '16 at 15:34 2016-09-23 15:34
source share