Επεξεργασία

Κοινή χρήση μέσω


LingerOption.LingerTime Property

Definition

Gets or sets the amount of time to remain connected after calling the Close() method if data remains to be sent.

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

Property Value

The amount of time, in seconds, to remain connected after calling Close().

Examples

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());
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

Remarks

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.

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. If you change this value, you must pass the altered LingerOption instance to the SetSocketOption method or set the LingerState or LingerState property.

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 (disabled), the default value The time-out is not applicable, (default). Attempts to send pending data for a connection-oriented socket (TCP, for example) until the default IP protocol time-out expires.
true (enabled) A nonzero time-out Attempts to send pending data until the specified time-out expires, and if the attempt fails, then Winsock resets the connection.
true (enabled) A zero timeout. Discards any pending data. For connection-oriented socket (TCP, for example), Winsock resets the connection.

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. This is the default behavior for a socket when the LingerState property is not set.

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.

Applies to