TcpClient.LingerState Property

Gets or sets information about the sockets linger time.

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. By default, lingering is disabled.

This property controls the length of time that the TCP connection will remain open after a call to Close when data remains to be sent. When you call the Write method, data is placed in the outgoing network buffer. This property can be used to ensure that this data is sent to the remote host before the Close method drops the connection.

To enable lingering, create a LingerOption containing the desired values, and set this property to that LingerOption. If the Enabled property of the LingerOption is true, then data is sent to the network with a time-out of LingerOption.LingerTime seconds. After the data is sent, or if the time-out expires, the connection is closed and any unsent data is lost. If the Enabled property of the LingerOption is false, then the connection closes immediately after a call to the Close method, even if data remains to be sent.

The following code example sets and gets the sockets linger time.

// sets the amount of time to linger after closing, using the LingerOption public property.
LingerOption lingerOption = new LingerOption (true, 10);

tcpClient.LingerState = lingerOption;

// gets the amount of linger time set, using the LingerOption public property.
if (tcpClient.LingerState.LingerTime == 10)
    Console.WriteLine ("The linger state setting was successfully set to " + tcpClient.LingerState.LingerTime.ToString ());


// sets the amount of time to linger after closing, using the
// LingerOption public property.
LingerOption lingerOption = new LingerOption(true, 10);
tcpClient.set_LingerState(lingerOption);

// gets the amount of linger time set, using the LingerOption public
// property.
if (tcpClient.get_LingerState().get_LingerTime() == 10) {
    Console.WriteLine("The linger state setting was successfully set to "
        + ((Int32)tcpClient.get_LingerState().get_LingerTime()).ToString());
}

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, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: