This documentation is archived and is not being maintained.

LingerOption Class

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

For a list of all members of this type, see LingerOption Members.


[Visual Basic]
Public Class LingerOption
public class LingerOption
public __gc class LingerOption
public class LingerOption

Thread Safety

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


There may still be data available in the outgoing network buffer after you close the Socket. 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 enabled 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 with the enabled parameter set to false. 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 pass the LingerOption to the TcpClient.LingerState method.

By default, lingering is enabled with a zero time-out. As a result, the Socket will attempt to send pending data until there is no data left in the outgoing network buffer.


[Visual Basic, C#, C++] The following example sets a previously created Socket to linger one second after calling the Close method.

[Visual Basic] 
Dim myOpts As New LingerOption(True, 1)
mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, _

LingerOption myOpts = new LingerOption(true,1);

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

LingerOption* myOpts = new LingerOption(true,1);

mySocket->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger, myOpts);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Net.Sockets

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

LingerOption Members | System.Net.Sockets Namespace