LingerOption.LingerTime 属性

定义

获取或设置在调用 Close() 方法后仍有数据要发送的情况下将保持连接的时间量。

public:
 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

属性值

Int32

调用 Close() 后保持连接的时间量(以秒为单位)。

示例

以下示例显示此属性的值。

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() );
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
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
    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 " + CType(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger), LingerOption).LingerTime.ToString()))
    s.Close()
End Sub

注解

如果要确定关闭 Socket 后尝试在超时之前传输未发送的数据的时间,请使用此值。还可以将此值设置为所需的超时时间段(以秒为单位)。

Enabled如果该属性设置为 trueLingerTime 0,则Socket丢弃在传出网络缓冲区中发送的任何挂起数据。 如果更改此值,则必须将更改 LingerOption 的实例传递给 SetSocketOption 方法或设置 LingerState or LingerState 属性。

下表描述了属性和LingerTime属性中LingerState存储的属性的可能值Enabled的行为。

enable seconds 行为
false (禁用) 默认值 超时不适用, (默认) 。 尝试为面向连接的套接字 (TCP 发送挂起的数据,例如,在默认 IP 协议超时过期之前) 。
true 已启用 () 非零超时 尝试发送挂起的数据,直到指定的超时过期,如果尝试失败,则 Winsock 将重置连接。
true 已启用 () 零超时。 放弃任何挂起的数据。 对于面向连接的套接字 (TCP,例如) ,Winsock 会重置连接。

IP 堆栈根据连接的往返时间计算要使用的默认 IP 协议超时时间。 在大多数情况下,堆栈计算的超时比应用程序定义的超时更相关。 如果未设置属性, LingerState 则这是套接字的默认行为。

LingerTime当属性中LingerState存储的属性设置为大于默认 IP 协议超时时,默认 IP 协议超时仍将应用和替代。

适用于