Socket.LingerState Propiedad

Definición

Obtiene o establece un valor que especifica si Socket retrasará el cierre de un socket en un intento de envío de todos los datos pendientes.

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; }
public System.Net.Sockets.LingerOption LingerState { get; set; }
member this.LingerState : System.Net.Sockets.LingerOption with get, set
Public Property LingerState As LingerOption

Valor de propiedad

LingerOption que especifica cómo aplicar un retraso mientras se cierra un socket.

Excepciones

Error al intentar acceder al socket.

El Socket se ha cerrado.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de la LingerState propiedad .

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("");
}

Comentarios

La LingerState propiedad cambia el comportamiento Close del método . Esta propiedad cuando se establece modifica las condiciones en las que Winsock puede restablecer la conexión. Todavía se pueden producir restablecimientos de conexión en función del comportamiento del protocolo IP.

Esta propiedad controla el período de tiempo durante el que una conexión orientada a la conexión permanecerá abierta después de una llamada a Close cuando se sigan enviando datos.

Al llamar a métodos para enviar datos a un elemento del mismo nivel, estos datos se colocan en el búfer de red saliente. Esta propiedad se puede usar para asegurarse de que estos datos se envían al host remoto antes de que el Close método deje de establecer la conexión.

Para habilitar la persistencia, cree una LingerOption instancia que contenga los valores deseados y establezca la LingerState propiedad en esta instancia.

En la tabla siguiente se describe el comportamiento del Close método para los posibles valores de la Enabled propiedad y la LingerTime propiedad almacenada en la LingerState propiedad .

LingerState.Enabled LingerState.LingerTime Comportamiento
false (deshabilitado), el valor predeterminado El tiempo de espera no es aplicable (valor predeterminado). Intenta enviar datos pendientes hasta que expire el tiempo de espera predeterminado del protocolo IP.
true (habilitado) Tiempo de espera distinto de cero Intenta enviar datos pendientes hasta que expire el tiempo de espera especificado y, si se produce un error en el intento, Winsock restablece la conexión.
true (habilitado) Un tiempo de espera cero. Descarta los datos pendientes. En el caso del socket orientado a la conexión (TCP, por ejemplo), Winsock restablece la conexión.

La pila de IP calcula el período de tiempo de espera predeterminado del protocolo IP que se usará en función del tiempo de ida y vuelta de la conexión. En la mayoría de los casos, el tiempo de espera calculado por la pila es más relevante que uno definido por una aplicación. Este es el comportamiento predeterminado de un socket cuando no se establece la LingerState propiedad .

Cuando la LingerTime propiedad almacenada en la LingerState propiedad se establece mayor que el tiempo de espera predeterminado del protocolo IP, el tiempo de espera predeterminado del protocolo IP se seguirá aplicando e invalidará.

Se aplica a