Socket.LingerState Socket.LingerState Socket.LingerState Socket.LingerState Property

定义

获取或设置一个值,该值指定 Socket 在尝试发送所有挂起数据时是否延迟关闭套接字。Gets or sets a value that specifies whether the Socket will delay closing a socket in an attempt to send all pending data.

public:
 property System::Net::Sockets::LingerOption ^ LingerState { System::Net::Sockets::LingerOption ^ get(); void set(System::Net::Sockets::LingerOption ^ value); };
public System.Net.Sockets.LingerOption LingerState { get; set; }
member this.LingerState : System.Net.Sockets.LingerOption with get, set
Public Property LingerState As LingerOption

属性值

一个 LingerOption,它指定关闭套接字时如何逗留。A LingerOption that specifies how to linger while closing a socket.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例演示如何将LingerState属性。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 = gcnew 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("");
}
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("");
}

注解

LingerState属性更改的方式Close方法的行为。The LingerState property changes the way Close method behaves. 当集修改的条件下的连接可以重置通过 Winsock 时此属性。This property when set modifies the conditions under which the connection can be reset by Winsock. 连接重置仍会发生基于 IP 协议行为。Connection resets can still occur based on the IP protocol behavior.

此属性控制的面向连接的连接是在调用后仍保持打开的时间长度Close仍然要发送数据时。This property controls the length of time that a connection-oriented connection will remain open after a call to Close when data remains to be sent.

当调用方法以将数据发送到对等方时,此数据位于传出的网络缓冲区中。When you call methods to send data to a peer, this data is placed in the outgoing network buffer. 此属性可以用于确保此数据发送到远程主机之前Close方法用于删除连接。This property can be used to ensure that this data is sent to the remote host before the Close method drops the connection.

若要启用延迟,创建LingerOption实例包含所需的值,并设置LingerState指向此实例的属性。To enable lingering, create a LingerOption instance containing the desired values, and set the LingerState property to this instance.

下表描述的行为Close的可能值的方法Enabled属性和LingerTime属性存储在LingerState属性。The following table describes the behavior of the Close method for the possible values of the Enabled property and the LingerTime property stored in the LingerState property.

LingerState.EnabledLingerState.Enabled LingerState.LingerTimeLingerState.LingerTime 行为Behavior
false (禁用),默认值false (disabled), the default value 不适用,超时值 (默认值)。The time-out is not applicable, (default). 尝试发送挂起的数据,直到默认 IP 协议超时时间已到。Attempts to send pending data until the default IP protocol time-out expires.
true (已启用)true (enabled) 非零值超时A nonzero time-out 尝试发送挂起的数据,直到指定的超时时间已到,如果该尝试失败,然后 Winsock 重置连接。Attempts to send pending data until the specified time-out expires, and if the attempt fails, then Winsock resets the connection.
true (已启用)true (enabled) 零个超时时间。A zero timeout. 将放弃所有挂起的数据。Discards any pending data. 对于面向连接的套接字 (例如 TCP),Winsock 重置连接。For connection-oriented socket (TCP, for example), Winsock resets the connection.

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未设置属性。This is the default behavior for a socket when the LingerState property is not set.

LingerTime属性存储在LingerState属性设置为大于默认 IP 协议超时时间,则默认 IP 协议超时时间仍将应用,并替代。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.

适用于