Socket.NoDelay Socket.NoDelay Socket.NoDelay Socket.NoDelay Property

定义

获取或设置 Boolean 值,该值指定流 Socket 是否正在使用 Nagle 算法。Gets or sets a Boolean value that specifies whether the stream Socket is using the Nagle algorithm.

public:
 property bool NoDelay { bool get(); void set(bool value); };
public bool NoDelay { get; set; }
member this.NoDelay : bool with get, set
Public Property NoDelay As Boolean

属性值

如果 Socket 使用 Nagle 算法,则为 false;否则为 truefalse if the Socket uses the Nagle algorithm; otherwise, true. 默认值为 falseThe default is false.

异常

尝试访问 Socket 时出错。An error occurred when attempting to access the Socket.

示例

下面的代码示例演示如何将NoDelay属性。The following code example demonstrates the use of the NoDelay 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("");
}

注解

Nagle 算法设计为套接字缓冲小数据包,然后组合并在某些情况下一个数据包中发送,从而减少网络流量。The Nagle algorithm is designed to reduce network traffic by causing the socket to buffer small packets and then combine and send them in one packet under certain circumstances. TCP 数据包包含 40 个字节的标头以及发送的数据。A TCP packet consists of 40 bytes of header plus the data being sent. 与 TCP 发送的数据的小数据包,从而从 TCP 标头的开销会变为网络流量的重要组成部分。在负载较重的网络上导致这种开销的拥塞可能会导致丢失的数据报和重传现象,以及由阻塞导致过多的传播时间。When small packets of data are sent with TCP, the overhead resulting from the TCP header can become a significant part of the network traffic.On heavily loaded networks, the congestion resulting from this overhead can result in lost datagrams and retransmissions, as well as excessive propagation time caused by congestion. Nagle 算法禁止发送的新 TCP segmentswhen 新传出数据到达从用户中,如果在连接上的任何处数据仍未确认。The Nagle algorithm inhibits the sending of new TCP segmentswhen new outgoing data arrives from the user if any previouslytransmitted data on the connection remains unacknowledged.

大多数网络应用程序应使用 Nagle 算法。The majority of network applications should use the Nagle algorithm.

用户数据报协议 (UDP) 套接字上设置此属性不会影响。Setting this property on a User Datagram Protocol (UDP) socket will have no effect.

适用于