LingerOption.LingerTime LingerOption.LingerTime LingerOption.LingerTime LingerOption.LingerTime Property


获取或设置在调用 Close() 方法后仍有数据要发送的情况下将保持连接的时间量。Gets or sets the amount of time to remain connected after calling the Close() method if data remains to be sent.

 property int LingerTime { int get(); void set(int value); };
public int LingerTime { get; set; }
member this.LingerTime : int with get, set
Public Property LingerTime As Integer


调用 Close() 后保持连接的时间量(以秒为单位)。The amount of time, in seconds, to remain connected after calling Close().


以下示例显示此属性的值。The following example displays the value of this property.

Console::Write(  "This application will timeout if Send does not return within " );
Console::WriteLine( Encoding::ASCII->GetString( s->GetSocketOption( SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 4 ) ) );

// Blocks until send returns.
int i = s->Send( msg );

// Blocks until read returns.
array<Byte>^ bytes = gcnew array<Byte>(1024);

s->Receive( bytes );

//Displays to the screen.
Console::WriteLine( Encoding::ASCII->GetString( bytes ) );
s->Shutdown( SocketShutdown::Both );
Console::Write(  "If data remains to be sent, this application will stay open for " );
Console::WriteLine( safe_cast<LingerOption^>(s->GetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger ))->LingerTime.ToString() );
Console.WriteLine ("This application will timeout if Send does not return within " + Encoding.ASCII.GetString (s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4)));

// blocks until send returns
int i = s.Send (msg);

// blocks until read returns
byte[] bytes = new byte[1024];

s.Receive (bytes);

//Display to the screen
Console.WriteLine (Encoding.ASCII.GetString (bytes));
s.Shutdown (SocketShutdown.Both);
Console.WriteLine ("If data remains to be sent, this application will stay open for " + ((LingerOption)s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger)).LingerTime.ToString ());
s.Close ();
    Console.WriteLine(("This application will timeout if Send does not return within " + Encoding.ASCII.GetString(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4))))
    ' blocks until send returns
    Dim i As Integer = s.Send(msg)

    ' blocks until read returns
    Dim bytes(1024) As Byte

    'Display to the screen

    Console.WriteLine(("If data remains to be sent, this application will stay open for " + CType(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger), LingerOption).LingerTime.ToString()))
End Sub 'SetSocketOptions


使用此值,如果你想要确定如何长时间关闭Socket将尝试传输超时前未发送的数据。此外可以将此值设置到所需的超时时间 (秒)。Use this value if you want to determine how long a closed Socket will attempt to transfer unsent data before timing out. You can also set this value to the desired time-out period, in seconds.

如果Enabled属性是true,并设置LingerTime为 0,Socket丢弃所有挂起的要在传出的网络缓冲区中发送的数据。If the Enabled property is true, and you set LingerTime to 0, the Socket discards any pending data to send in the outgoing network buffer. 如果更改此值,则必须通过更改LingerOption实例向SetSocketOption方法或一组LingerStateLingerState属性。If you change this value, you must pass the altered LingerOption instance to the SetSocketOption method or set the LingerState or LingerState property.

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

enable seconds 行为Behavior
false (禁用),默认值false (disabled), the default value 不适用,超时值 (默认值)。The time-out is not applicable, (default). 尝试发送挂起数据的面向连接的套接字 (例如 TCP) 直到默认 IP 协议超时过期。Attempts to send pending data for a connection-oriented socket (TCP, for example) 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.

LingerTime属性存储在LingerState属性设置为大于默认 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.