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。 根據預設,會停用延遲。

範例

下列程式代碼範例會設定並取得套接字閑置時間。

// sets the amount of time to linger after closing, using the LingerOption public property.
LingerOption^ lingerOption = gcnew 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 {0}", tcpClient->LingerState->LingerTime );
// 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 ,這是套接字的默認行為。

LingerTime當屬性中LingerState儲存的屬性設定大於預設 IP 通訊協定逾時,預設 IP 通訊協定逾時仍會套用並覆寫。

適用於

另請參閱