LingerOption 类

定义

指定在调用 SocketClose() 方法之后,如果仍有数据要发送,Close() 是否保持连接以及保持多长时间。Specifies whether a Socket will remain connected after a call to the Close() or Close() methods and the length of time it will remain connected, if data remains to be sent.

public ref class LingerOption
public class LingerOption
type LingerOption = class
Public Class LingerOption
继承
LingerOption

示例

下面的示例在调用 Close 方法后,将以前创建的 Socket 设置为逗留一秒钟。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)

注解

在应用程序调用 SocketTcpClient 方法后,传出网络缓冲区中可能仍有可用的数据。There may still be data available in the outgoing network buffer after an application calls the Socket or TcpClient method. 如果要指定 Socket 在关闭后将尝试传输未发送数据的时间量,请创建 enable 参数设置为 trueLingerOption,并将 seconds 参数设置为所需的时间量。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 enable parameter set to true, and the seconds parameter set to the desired amount of time. seconds 参数用于指示在超时前 Socket 保持连接的时间。如果你不希望 Socket 在关闭后一直保持连接状态,则创建一个 LingerOption 实例,并将 enable 参数设置为 true,将 seconds 参数设置为零。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 instance with the enable parameter set to true and the seconds parameter set to zero. 在这种情况下,Socket 会立即关闭,任何未发送的数据都将丢失。In this case, the Socket will close immediately and any unsent data will be lost. 创建后,将 LingerOption 传递到 Socket.SetSocketOption 方法。Once created, pass the LingerOption to the Socket.SetSocketOption method. 如果要使用 TcpClient发送和接收数据,请在 TcpClient.LingerState 属性中设置 LingerOption 实例。If you are sending and receiving data with a TcpClient, then set the LingerOption instance in the TcpClient.LingerState property.

IP 堆栈根据连接的往返时间来计算要使用的默认 IP 协议超时期限。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. 当未设置 LingerState 属性时,如果未设置 LingerState 属性和 TcpClient,则这是 Socket 的默认行为。This is the default behavior for a Socket when the LingerState property is not set and for a TcpClient when the LingerState property is not set.

构造函数

LingerOption(Boolean, Int32)

初始化 LingerOption 类的新实例。Initializes a new instance of the LingerOption class.

属性

Enabled

获取或设置一个值,指示 Socket 关闭后是否逗留。Gets or sets a value that indicates whether to linger after the Socket is closed.

LingerTime

获取或设置在调用 Close() 方法后仍有数据要发送的情况下将保持连接的时间量。Gets or sets the amount of time to remain connected after calling the Close() method if data remains to be sent.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于