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

例外

ソケットへのアクセスを試行しているときにエラーが発生しました。

Socket が閉じられました。

次のコード例では、 プロパティの使用方法を 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 (有効) 0 以外のタイムアウト 指定したタイムアウトが切れるまで保留中のデータの送信を試行し、試行が失敗した場合、Winsock は接続をリセットします。
true (有効) タイムアウトが 0 です。 保留中のデータを破棄します。 接続指向ソケット (TCP など) の場合、Winsock は接続をリセットします。

IP スタックは、接続のラウンド トリップ時間に基づいて、使用する既定の IP プロトコルタイムアウト期間を計算します。 ほとんどの場合、スタックによって計算されるタイムアウトは、アプリケーションによって定義されたタイムアウトよりも関連性が高くなります。 これは、 プロパティが設定されていない場合のソケットの既定の LingerState 動作です。

プロパティに LingerTime 格納されている LingerState プロパティが既定の IP プロトコルタイムアウトより大きく設定されている場合、既定の IP プロトコルタイムアウトは引き続き適用され、オーバーライドされます。

適用対象