Export (0) Print
Expand All

LingerOption Constructor

Initializes a new instance of the LingerOption class.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public LingerOption(
	bool enable,
	int seconds


Type: System.Boolean

true to remain connected after the Socket.Close method is called; otherwise, false.

Type: System.Int32

The number of seconds to remain connected after the Socket.Close method is called.

There may still be data available in the outgoing network buffer after you close the Socket. Use the enable parameter to specify whether you would like the Socket to continue transmitting unsent data after the close method is called. Use the seconds parameter to indicate how long you would like the Socket to attempt transferring unsent data before timing out. If you specify true for the enable parameter and 0 for the seconds parameter, the Socket will attempt to send data until there is no data left in the outgoing network buffer. If you specify false for the enable parameter, the Socket will close immediately and any unsent data will be lost.

The following table describes the behavior on the Socket.Close and TcpClient.Close methods based on the possible values of the enable and seconds parameters when an T:System.Net.Sockets.LingerOption instance is created and set in the Socket.LingerState or TcpClient.LingerState property.




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.

The following example sets a previously created Socket to linger one second after calling the Close method.

LingerOption myOpts = new LingerOption(true,1);

mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, myOpts);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft