Export (0) Print
Expand All

LingerOption Class

Specifies whether a Socket will remain connected after a call to the Close or Close methods and the length of time it will remain connected, if data remains to be sent.

System.Object
  System.Net.Sockets.LingerOption

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

public class LingerOption

The LingerOption type exposes the following members.

  NameDescription
Public methodLingerOptionInitializes a new instance of the LingerOption class.
Top

  NameDescription
Public propertyEnabledGets or sets a value that indicates whether to linger after the Socket is closed.
Public propertyLingerTimeGets or sets the amount of time to remain connected after calling the Socket.Close method if data remains to be sent.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

There may still be data available in the outgoing network buffer after an application calls the Socket or TcpClient method. If you want to specify the amount of time that the Socket will attempt to transmit unsent data after closing, create a LingerOption with the enable parameter set to true, and the seconds parameter set to the desired amount of time. The seconds parameter is used to indicate how long you would like the Socket to remain connected before timing out. If you do not want the Socket to stay connected for any length of time after closing, create a LingerOption instance with the enable parameter set to true and the seconds parameter set to zero. In this case, the Socket will close immediately and any unsent data will be lost. Once created, pass the LingerOption to the Socket.SetSocketOption method. If you are sending and receiving data with a TcpClient, then set the LingerOption instance in the TcpClient.LingerState property.

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 and for a TcpClient when the LingerState property is not set.

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.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft