有人提到 TCP Socket 的 keepAlive 能力。这里很好地描述了:
http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
我以这种方式使用它:连接套接字后,我正在调用此函数,它将 keepAlive 设置为打开。该keepAliveTime
参数指定超时,以毫秒为单位,在发送第一个保持活动数据包之前没有活动。该keepAliveInterval
参数指定在没有收到确认的情况下发送连续保持活动数据包之间的时间间隔,以毫秒为单位。
void SetKeepAlive(bool on, uint keepAliveTime, uint keepAliveInterval)
{
int size = Marshal.SizeOf(new uint());
var inOptionValues = new byte[size * 3];
BitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0);
BitConverter.GetBytes((uint)keepAliveTime).CopyTo(inOptionValues, size);
BitConverter.GetBytes((uint)keepAliveInterval).CopyTo(inOptionValues, size * 2);
socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
}
我也在使用异步阅读:
socket.BeginReceive(packet.dataBuffer, 0, 128,
SocketFlags.None, new AsyncCallback(OnDataReceived), packet);
在回调中,这里被捕获 timeout SocketException
,当套接字在保持活动数据包后没有收到 ACK 信号时引发。
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = socket.EndReceive(asyn);
}
catch (SocketException ex)
{
SocketExceptionCaught(ex);
}
}
这样,我就能够安全地检测到 TCP 客户端和服务器之间的断开连接。