共用方式為


Socket.LingerState 屬性

定義

取得或設定值,指定 Socket 是否會延遲關閉通訊端,以嘗試傳送所有暫止資料。

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,指定關閉通訊端時如何延遲。

例外狀況

嘗試存取通訊端時發生錯誤。

範例

下列程式代碼範例示範 屬性的使用 LingerState

static void ConfigureTcpSocket(Socket^ tcpSocket)
{
     
    // Don't allow another socket to bind to this port.
    tcpSocket->ExclusiveAddressUse = true;
     
    // The socket will linger for 10 seconds after
    // Socket.Close is called.
    tcpSocket->LingerState = gcnew LingerOption(true, 10);
     
    // Disable the Nagle Algorithm for this tcp socket.
    tcpSocket->NoDelay = true;
     
    // Set the receive buffer size to 8k
    tcpSocket->ReceiveBufferSize = 8192;
     
    // Set the timeout for synchronous receive methods to
    // 1 second (1000 milliseconds.)
    tcpSocket->ReceiveTimeout = 1000;
     
    // Set the send buffer size to 8k.
    tcpSocket->SendBufferSize = 8192;
     
    // Set the timeout for synchronous send methods
    // to 1 second (1000 milliseconds.)
    tcpSocket->SendTimeout = 1000;
     
    // Set the Time To Live (TTL) to 42 router hops.
    tcpSocket->Ttl = 42;
    Console::WriteLine("Tcp Socket configured:");
    Console::WriteLine("  ExclusiveAddressUse {0}", 
        tcpSocket->ExclusiveAddressUse);
    Console::WriteLine("  LingerState {0}, {1}", 
        tcpSocket->LingerState->Enabled,
        tcpSocket->LingerState->LingerTime);
    Console::WriteLine("  NoDelay {0}",
        tcpSocket->NoDelay);
    Console::WriteLine("  ReceiveBufferSize {0}", 
        tcpSocket->ReceiveBufferSize);
    Console::WriteLine("  ReceiveTimeout {0}",
        tcpSocket->ReceiveTimeout);
    Console::WriteLine("  SendBufferSize {0}",
        tcpSocket->SendBufferSize);
    Console::WriteLine("  SendTimeout {0}",
        tcpSocket->SendTimeout);
    Console::WriteLine("  Ttl {0}",
        tcpSocket->Ttl);
    Console::WriteLine("  IsBound {0}",
        tcpSocket->IsBound);
    Console::WriteLine("");
}
static void ConfigureTcpSocket(Socket tcpSocket)
{
    // Don't allow another socket to bind to this port.
    tcpSocket.ExclusiveAddressUse = true;

    // The socket will linger for 10 seconds after
    // Socket.Close is called.
    tcpSocket.LingerState = new LingerOption (true, 10);

    // Disable the Nagle Algorithm for this tcp socket.
    tcpSocket.NoDelay = true;

    // Set the receive buffer size to 8k
    tcpSocket.ReceiveBufferSize = 8192;

    // Set the timeout for synchronous receive methods to
    // 1 second (1000 milliseconds.)
    tcpSocket.ReceiveTimeout = 1000;

    // Set the send buffer size to 8k.
    tcpSocket.SendBufferSize = 8192;

    // Set the timeout for synchronous send methods
    // to 1 second (1000 milliseconds.)
    tcpSocket.SendTimeout = 1000;

    // Set the Time To Live (TTL) to 42 router hops.
    tcpSocket.Ttl = 42;

    Console.WriteLine("Tcp Socket configured:");

    Console.WriteLine($"  ExclusiveAddressUse {tcpSocket.ExclusiveAddressUse}");

    Console.WriteLine($"  LingerState {tcpSocket.LingerState.Enabled}, {tcpSocket.LingerState.LingerTime}");

    Console.WriteLine($"  NoDelay {tcpSocket.NoDelay}");

    Console.WriteLine($"  ReceiveBufferSize {tcpSocket.ReceiveBufferSize}");

    Console.WriteLine($"  ReceiveTimeout {tcpSocket.ReceiveTimeout}");

    Console.WriteLine($"  SendBufferSize {tcpSocket.SendBufferSize}");

    Console.WriteLine($"  SendTimeout {tcpSocket.SendTimeout}");

    Console.WriteLine($"  Ttl {tcpSocket.Ttl}");

    Console.WriteLine($"  IsBound {tcpSocket.IsBound}");

    Console.WriteLine("");
}

備註

屬性 LingerState 會變更 Close 方法的行為方式。 設定 時,這個屬性會修改 Winsock 可以重設連線的條件。 線上重設仍可能會根據IP通訊協議行為進行。

此屬性會控制連接導向連接在呼叫 Close 後仍會保持開啟的時間長度,當數據仍要傳送時。

當您呼叫方法以將數據傳送至對等時,此數據會放在傳出網路緩衝區中。 這個屬性可用來確保這個數據會在方法卸除連線之前 Close 傳送至遠端主機。

若要啟用閑置,請建立 LingerOption 包含所需值的實例,並將屬性設定 LingerState 為這個實例。

下表描述 方法的行為 Close ,以取得屬性的 Enabled 可能值,以及 LingerTime 儲存在 屬性中的 LingerState 屬性。

LingerState.Enabled LingerState.LingerTime 行為
false (停用) 預設值 逾時不適用, (預設) 。 嘗試傳送擱置的數據,直到預設IP通訊協定逾時到期為止。
true 已啟用 () 非零逾時 嘗試傳送擱置的數據,直到指定的逾時到期為止,如果嘗試失敗,Winsock 會重設連線。
true 已啟用 () 零逾時。 捨棄任何擱置的數據。 針對連線導向套接字 (TCP,例如) ,Winsock 會重設連線。

IP 堆疊會根據連線的來回時間計算要使用的預設IP通訊協定逾時期間。 在大部分情況下,堆疊所計算的逾時比應用程式所定義的逾時還要相關。 當未設定 屬性時 LingerState ,這是套接字的默認行為。

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

適用於