Socket.LingerState 属性

定义

获取或设置一个值,该值指定 Socket 在尝试发送所有挂起数据时是否延迟关闭套接字。Gets or sets a value that specifies whether the Socket will delay closing a socket in an attempt to send all pending data.

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; }
member this.LingerState : System.Net.Sockets.LingerOption with get, set
Public Property LingerState As LingerOption

属性值

一个 LingerOption,它指定关闭套接字时如何逗留。A LingerOption that specifies how to linger while closing a socket.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例演示如何使用 LingerState 属性。The following code example demonstrates the use of the LingerState property.

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 方法行为的方式。The LingerState property changes the way Close method behaves. 设置此属性时,此属性修改 Winsock 可以重置连接时的条件。This property when set modifies the conditions under which the connection can be reset by Winsock. 仍然可以基于 IP 协议行为重置连接。Connection resets can still occur based on the IP protocol behavior.

此属性控制在数据保留时,连接到 Close 后,面向连接的连接将保持打开状态的时间长度。This property controls the length of time that a connection-oriented connection will remain open after a call to Close when data remains to be sent.

调用方法将数据发送到对等节点时,此数据将被置于传出网络缓冲区中。When you call methods to send data to a peer, this data is placed in the outgoing network buffer. 此属性可用于确保在 Close 方法断开连接之前,将此数据发送到远程主机。This property can be used to ensure that this data is sent to the remote host before the Close method drops the connection.

若要启用延迟,请创建包含所需值的 LingerOption 实例,并将 LingerState 属性设置为此实例。To enable lingering, create a LingerOption instance containing the desired values, and set the LingerState property to this instance.

下表描述了 Enabled 属性的可能值和 LingerState 属性中存储的 LingerTime 属性的 Close 方法的行为。The following table describes the behavior of the Close method for the possible values of the Enabled property and the LingerTime property stored in the LingerState property.

LingerState.EnabledLingerState.Enabled LingerState.LingerTimeLingerState.LingerTime 行为Behavior
false (已禁用),默认值为false (disabled), the default value 超时不适用,(默认值)。The time-out is not applicable, (default). 在默认 IP 协议超时过期之前,尝试发送挂起的数据。Attempts to send pending data until the default IP protocol time-out expires.
true (已启用)true (enabled) 非零超时A nonzero time-out 尝试发送挂起的数据直到指定的超时过期,如果尝试失败,Winsock 将重置连接。Attempts to send pending data until the specified time-out expires, and if the attempt fails, then Winsock resets the connection.
true (已启用)true (enabled) 零超时。A zero timeout. 丢弃所有挂起的数据。Discards any pending data. 对于面向连接的套接字(例如 TCP),Winsock 重置连接。For connection-oriented socket (TCP, for example), Winsock resets the connection.

IP 堆栈根据连接的往返时间来计算要使用的默认 IP 协议超时期限。The IP stack computes the default IP protocol time-out period to use based on the round trip time of the connection. 在大多数情况下,堆栈计算出的超时比应用程序定义的超时更密切。In most cases, the time-out computed by the stack is more relevant than one defined by an application. 这是未设置 LingerState 属性时套接字的默认行为。This is the default behavior for a socket when the LingerState property is not set.

如果在 LingerState 属性中存储的 LingerTime 属性设置为大于默认 IP 协议超时值,则默认的 IP 协议超时将仍适用并将替代。When the LingerTime property stored in the LingerState property is set greater than the default IP protocol time-out, the default IP protocol time-out will still apply and override.

适用于