Επεξεργασία

Κοινή χρήση μέσω


LingerOption(Boolean, Int32) Constructor

Definition

Initializes a new instance of the LingerOption class.

public:
 LingerOption(bool enable, int seconds);
public LingerOption (bool enable, int seconds);
new System.Net.Sockets.LingerOption : bool * int -> System.Net.Sockets.LingerOption
Public Sub New (enable As Boolean, seconds As Integer)

Parameters

enable
Boolean

true to remain connected after the Close() method is called; otherwise, false.

seconds
Int32

The number of seconds to remain connected after the Close() method is called.

Examples

The following example sets a previously created Socket to linger one second after calling the Close method.

LingerOption^ myOpts = gcnew LingerOption( true,1 );
mySocket->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger, myOpts );
LingerOption myOpts = new LingerOption(true,1);

mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, myOpts);
Dim myOpts As New LingerOption(True, 1)
       
mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, _
   myOpts)

Remarks

There may still be data available in the outgoing network buffer after you close the Socket. Use the enable parameter to specify whether you would like the Socket to continue transmitting unsent data after the close method is called. Use the seconds parameter to indicate how long you would like the Socket to attempt transferring unsent data before timing out. If you specify true for the enable parameter and 0 for the seconds parameter, the Socket will attempt to send data until there is no data left in the outgoing network buffer. If you specify false for the enable parameter, the Socket will close immediately and any unsent data will be lost.

The following table describes the behavior on the Socket.Close and TcpClient.Close methods based on the possible values of the enable and seconds parameters when an T:System.Net.Sockets.LingerOption instance is created and set in the Socket.LingerState or TcpClient.LingerState property.

enable seconds Behavior
false (disabled), the default value The time-out is not applicable, (default). Attempts to send pending data for a connection-oriented socket (TCP, for example) until the default IP protocol time-out expires.
true (enabled) A nonzero time-out Attempts to send pending data until the specified time-out expires, and if the attempt fails, then Winsock resets the connection.
true (enabled) A zero timeout. Discards any pending data. For connection-oriented socket (TCP, for example), Winsock resets the connection.

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.

When the LingerTime property stored in the LingerState property is set greater than the default IP protocol time-out, the default IP protocol time-out will still apply and override.

Applies to