網絡異常斷開原因主要有那些呢?歸納起來主要有以下兩種:
1、客戶端程序異常。
對於這種情況,我們很好處理,因為客戶端程序異常退出會在服務端引發ConnectionReset的Socket異常(就是WinSock2中的10054異常)。只要在服務端處理這個異常就可以了。
2、網絡鏈路異常。
如:網線拔出、交換機掉電、客戶端機器掉電。當出現這些情況的時候服務端不會出現任何異常。這樣的話上面的代碼就不能處理這種情況了。對於這種情況在MSDN里面是這樣處理的,我在這里貼出MSDN的原文:
如果您需要確定連接的當前狀態,請進行非阻止、零字節的 Send 調用。如果該調用成功返回或引發 WAEWOULDBLOCK 錯誤代碼 (10035),則該套接字仍然處於連接狀態;否則,該套接字不再處於連接狀態。
但是我在實際應用中發現,MSDN說的這種處理方法在很多時候根本無效,無法檢測出網絡已經異常斷開了。那我們該怎么辦呢?
我們知道,TCP有一個連接檢測機制,就是如果在指定的時間內(一般為2個小時)沒有數據傳送,會給對端發送一個Keep-Alive數據報,使用的序列號是曾經發出的最后一個報文的最后一個字節的序列號,對端如果收到這個數據,回送一個TCP的ACK,確認這個字節已經收到,這樣就知道此連接沒有被斷開。如果一段時間沒有收到對方的響應,會進行重試,重試幾次后,向對端發一個reset,然后將連接斷掉。
在Windows中,第一次探測是在最后一次數據發送的兩個小時,然后每隔1秒探測一次,一共探測5次,如果5次都沒有收到回應的話,就會斷開這個連接。但兩個小時對於我們的項目來說顯然太長了。我們必須縮短這個時間。那么我們該如何做呢?我要利用Socket類的IOControl()函數。我們來看看這個函數能干些什么:
使用 IOControlCode 枚舉指定控制代碼,為 Socket 設置低級操作模式。
命名空間:System.Net.Sockets
程序集:System(在 system.dll 中)
語法
C#
public int IOControl (
IOControlCode ioControlCode,
byte[] optionInValue,
byte[] optionOutValue
)
參數
ioControlCode
一個 IOControlCode 值,它指定要執行的操作的控制代碼。
optionInValue
Byte 類型的數組,包含操作要求的輸入數據。
optionOutValue
Byte 類型的數組,包含由操作返回的輸出數據。
返回值
optionOutValue 參數中的字節數。
如:

我們要搞清楚的就是inOptionValues的定義,在C++里它是一個結構體。我們來看看這個結構體:







在C#中,我們直接用一個Byte數組傳遞給函數:






具體實現代碼:
首先,我們引入命名空間:
using System.Runtime.InteropServices;
其次,構建方法:






























好了,這樣就成功了。
{
public static class SocketExtensions
{
private const int BytesPerLong = 4; // 32 / 8
private const int BitsPerByte = 8;
/// <summary>
/// Sets the keep-alive interval for the socket.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="time">Time between two keep alive "pings".</param>
/// <param name="interval">Time between two keep alive "pings" when first one fails.</param>
/// <returns>If the keep alive infos were succefully modified.</returns>
public static bool SetKeepAlive(this Socket socket, ulong time, ulong interval)
{
try
{
// Array to hold input values.
var input = new[]
{
(time == 0 || interval == 0) ? 0UL : 1UL, // on or off
time,
interval
};
// Pack input into byte struct.
byte[] inValue = new byte[3 * BytesPerLong];
for (int i = 0; i < input.Length; i++)
{
inValue[i * BytesPerLong + 3] = (byte)(input[i] >> ((BytesPerLong - 1) * BitsPerByte) & 0xff);
inValue[i * BytesPerLong + 2] = (byte)(input[i] >> ((BytesPerLong - 2) * BitsPerByte) & 0xff);
inValue[i * BytesPerLong + 1] = (byte)(input[i] >> ((BytesPerLong - 3) * BitsPerByte) & 0xff);
inValue[i * BytesPerLong + 0] = (byte)(input[i] >> ((BytesPerLong - 4) * BitsPerByte) & 0xff);
}
// Create bytestruct for result (bytes pending on server socket).
byte[] outValue = BitConverter.GetBytes(0);
// Write SIO_VALS to Socket IOControl.
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);
socket.IOControl(IOControlCode.KeepAliveValues, inValue, outValue);
}
catch (SocketException e)
Console.WriteLine("Failed to set keep-alive: {0} {1}", e.ErrorCode, e);
return false;
}
return true;
The result is not error, no exception and no keepalive in the expected time frame.
I am passing both times in mill-seconds. The Trace that I have shows 12 bytes in the form of
01 00 00 00 10 27 00 00 98 3a 00 00 when called as SetKeepAlive(10000,15000);
I am not really sure if I need to put the bytes in this order or not. If working directly with the Winsock API, the keepalive
option uses a structure of 3 ULONGS with a 1 in the first one, the time in the 2nd one and the retry time (interval in this code)
in the third one.