This documentation is archived and is not being maintained.

Socket.LingerState Property

Gets or sets a value that specifies whether the Socket will delay closing a socket in an attempt to send all pending data.

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

public LingerOption LingerState { get; set; }
/** @property */
public LingerOption get_LingerState ()

/** @property */
public void set_LingerState (LingerOption value)

public function get LingerState () : LingerOption

public function set LingerState (value : LingerOption)

Not applicable.

Property Value

A LingerOption that specifies how to linger while closing a socket.

Exception typeCondition


An error occurred when attempting to access the socket.


The Socket has been closed.

Using the LingerState option changes the way Close behaves. The following table describes the behavior for each of the possible property values.



Disabled, time-out not applicable (default).

Attempts to send pending data until the default time-out expires, and if the attempt fails, then resets the connection.

Enabled with nonzero time-out

Attempts to send pending data until the specified time-out expires, and if the attempt fails, then resets the connection.

Enabled with zero time-out

Discards any pending data and resets the connection.

The IP stack computes the default time-out period 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. If the remote host might be slow in responding to your host, consider calculating your own time-out period.

The following code example demonstrates the use of the LingerState property.

static void ConfigureTcpSocket(Socket tcpSocket)
    // Don't allow another socket to bind to this port.
    tcpSocket.ExclusiveAddressUse = true;

    // The socket will linger for 10 seconds after 
                // Socket.Close is called.
    tcpSocket.LingerState = new LingerOption (true, 10);

    // Disable the Nagle Algorithm for this tcp socket.
    tcpSocket.NoDelay = true;

    // Set the receive buffer size to 8k
    tcpSocket.ReceiveBufferSize = 8192;

    // Set the timeout for synchronous receive methods to 
    // 1 second (1000 milliseconds.)
    tcpSocket.ReceiveTimeout = 1000;

    // Set the send buffer size to 8k.
    tcpSocket.SendBufferSize = 8192;

    // Set the timeout for synchronous send methods
    // to 1 second (1000 milliseconds.)            
    tcpSocket.SendTimeout = 1000;

    // Set the Time To Live (TTL) to 42 router hops.
    tcpSocket.Ttl = 42;

    Console.WriteLine("Tcp Socket configured:");

    Console.WriteLine("  ExclusiveAddressUse {0}", 

    Console.WriteLine("  LingerState {0}, {1}", 

    Console.WriteLine("  NoDelay {0}", 

    Console.WriteLine("  ReceiveBufferSize {0}", 

    Console.WriteLine("  ReceiveTimeout {0}", 

    Console.WriteLine("  SendBufferSize {0}", 

    Console.WriteLine("  SendTimeout {0}", 

    Console.WriteLine("  Ttl {0}", 

                Console.WriteLine("  IsBound {0}", 


Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0