Socket.NoDelay Propriedade

Definição

Obtém ou define um valor Boolean que especifica se o Socket do fluxo está usando o Algoritmo de 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

Valor da propriedade

Boolean

false se o Socket usa o algoritmo de Nagle; caso contrário, true.false if the Socket uses the Nagle algorithm; otherwise, true. O padrão é false.The default is false.

Exceções

Ocorreu um erro ao tentar acessar o Socket.An error occurred when attempting to access the Socket.

O Socket foi fechado.The Socket has been closed.

Exemplos

O exemplo de código a seguir demonstra o uso da NoDelay propriedade.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 {tcpSocket.ExclusiveAddressUse}");

    Console.WriteLine($"  LingerState {tcpSocket.LingerState.Enabled}, {tcpSocket.LingerState.LingerTime}");

    Console.WriteLine($"  NoDelay {tcpSocket.NoDelay}");

    Console.WriteLine($"  ReceiveBufferSize {tcpSocket.ReceiveBufferSize}");

    Console.WriteLine($"  ReceiveTimeout {tcpSocket.ReceiveTimeout}");

    Console.WriteLine($"  SendBufferSize {tcpSocket.SendBufferSize}");

    Console.WriteLine($"  SendTimeout {tcpSocket.SendTimeout}");

    Console.WriteLine($"  Ttl {tcpSocket.Ttl}");

    Console.WriteLine($"  IsBound {tcpSocket.IsBound}");

    Console.WriteLine("");
}

Comentários

O algoritmo Nagle foi projetado para reduzir o tráfego de rede, fazendo com que o soquete faça o buffer de pacotes pequenos e, em seguida, combine-os e envie-os em um pacote em determinadas circunstâncias.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. Um pacote TCP consiste em 40 bytes de cabeçalho mais os dados que estão sendo enviados.A TCP packet consists of 40 bytes of header plus the data being sent. Quando pequenos pacotes de dados são enviados com TCP, a sobrecarga resultante do cabeçalho TCP pode se tornar uma parte significativa do tráfego de rede.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. Em redes muito carregadas, o congestionamento resultante dessa sobrecarga pode resultar em datagramas perdidos e retransmissões, bem como tempo excessivo de propagação causado pelo congestionamento.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. O algoritmo Nagle inibe o envio de novos segmentos TCP quando novos dados de saída chegam do usuário se algum dado anteriormente transmitido na conexão permanecer não confirmado.The Nagle algorithm inhibits the sending of new TCP segments when new outgoing data arrives from the user if any previously transmitted data on the connection remains unacknowledged.

A maioria dos aplicativos de rede deve usar o algoritmo Nagle.The majority of network applications should use the Nagle algorithm.

Definir essa propriedade em um soquete de protocolo UDP não terá nenhum efeito.Setting this property on a User Datagram Protocol (UDP) socket will have no effect.

Aplica-se a