Export (0) Print
Expand All

Socket.LingerState Property

Note: This property is new in the .NET Framework version 2.0.

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)

Property Value

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

Exception typeCondition

SocketException

An error occurred when attempting to access the socket.

ObjectDisposedException

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.

LingerState

Behavior

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}", 
                tcpSocket.ExclusiveAddressUse);

    Console.WriteLine("  LingerState {0}, {1}", 
                 tcpSocket.LingerState.Enabled, 
                     tcpSocket.LingerState.LingerTime);

    Console.WriteLine("  NoDelay {0}", 
                                        tcpSocket.NoDelay);

    Console.WriteLine("  ReceiveBufferSize {0}", 
                tcpSocket.ReceiveBufferSize);

    Console.WriteLine("  ReceiveTimeout {0}", 
                tcpSocket.ReceiveTimeout);

    Console.WriteLine("  SendBufferSize {0}", 
                tcpSocket.SendBufferSize);

    Console.WriteLine("  SendTimeout {0}", 
                                        tcpSocket.SendTimeout);

    Console.WriteLine("  Ttl {0}", 
                                        tcpSocket.Ttl);

                Console.WriteLine("  IsBound {0}", 
                                        tcpSocket.IsBound);

    Console.WriteLine("");
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft