語言

TcpClient.LingerState 屬性

定義

取得或設定相關套接字的殘留狀態資訊。

public:
 property System::Net::Sockets::LingerOption ^ LingerState { System::Net::Sockets::LingerOption ^ get(); void set(System::Net::Sockets::LingerOption ^ value); };
public System.Net.Sockets.LingerOption? LingerState { get; set; }
public System.Net.Sockets.LingerOption LingerState { get; set; }
member this.LingerState : System.Net.Sockets.LingerOption with get, set
Public Property LingerState As LingerOption

屬性值

LingerOption。 預設情況下,延遲是被關閉的。

範例

以下程式碼範例設定並取得套接字的 linger time。

// sets the amount of time to linger after closing, using the LingerOption public property.
LingerOption lingerOption = new LingerOption (true, 10);

tcpClient.LingerState = lingerOption;

// gets the amount of linger time set, using the LingerOption public property.
if (tcpClient.LingerState.LingerTime == 10)
    Console.WriteLine ("The linger state setting was successfully set to " + tcpClient.LingerState.LingerTime.ToString ());
' Sets the amount of time to linger after closing, using the LingerOption public property.
Dim lingerOption As New LingerOption(True, 10)
tcpClient.LingerState = lingerOption

' Gets the amount of linger time set, using the LingerOption public property.
If tcpClient.LingerState.LingerTime = 10 Then
   Console.WriteLine(("The linger state setting was successfully set to " + tcpClient.LingerState.LingerTime.ToString()))
End If

備註

該 LingerState 性質改變了方法的行為方式 Close 。 此特性設定後,會改變 Winsock 可重置連線的條件。 連線重置仍可能因 IP 協定行為而發生。

此特性控制 TCP 連線在呼叫後保持開啟的時間長度,以及 Close 資料尚未傳送的時間。 當你呼叫該 Write 方法時,資料會被放入外出的網路緩衝區。 此特性可用來確保資料在方法斷線前 Close 已傳送至遠端主機。

要啟用延遲,請建立 LingerOption 一個包含所需值的實例,並將屬性設 LingerState 為該實例。

下表描述了該方法對該屬性可能值Close及該Enabled屬性中儲存屬性的LingerTime行為LingerState。

LingerState.Enabled LingerState.LingerTime 行為
false (停用),預設值 超時不適用(預設)。 嘗試傳送待處理的資料,直到預設 IP 協定逾時結束。
true (啟用) 非零的暫停 嘗試傳送待處理的資料直到指定的逾時結束,若失敗,Winsock 會重置連線。
true (啟用) 零暫停。 丟棄所有待處理的資料後,Winsock 會重置連線。

IP 堆疊會根據連線的往返時間計算預設的 IP 協定逾時期。 在大多數情況下,堆疊計算的逾時比應用程式定義的逾時更為相關。 這是當 LingerState 屬性未設定時,socket 的預設行為。

當 LingerTime 屬性中 LingerState 儲存的屬性設定大於預設 IP 協定逾時值時,預設 IP 協定逾時仍會適用並覆蓋。

適用於

另請參閱