偵測 Socket client是否斷線?

最重要的是加入一行
listenSocket.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveSetting(1, 5000, 5000), null);

private byte[] GetKeepAliveSetting(int onOff, int keepAliveTime, int keepAliveInterval)
{
byte[] buffer = new byte[12];
BitConverter.GetBytes(onOff).CopyTo(buffer, 0);
BitConverter.GetBytes(keepAliveTime).CopyTo(buffer, 4);
BitConverter.GetBytes(keepAliveInterval).CopyTo(buffer, 8);
return buffer;
}


參考資料來源
http://www.dotblogs.com.tw/lbwshift2/archive/2012/10/07/76320.aspx

留言

熱門文章