Socket.ExclusiveAddressUse Propiedad

Definición

Obtiene o establece un valor de Boolean que especifica si Socket permite sólo a un proceso el enlace a un puerto.

C#
public bool ExclusiveAddressUse { get; set; }

Valor de propiedad

true si Socket permite sólo a un socket el enlace a un puerto concreto; de lo contrario, false. El valor predeterminado es true para Windows Server 2003 y Windows XP Service Pack 2, y false para todas las demás versiones.

Excepciones

Error al intentar acceder al socket.

El Socket se ha cerrado.

Se ha llamado a Bind(EndPoint) para este Socket.

Ejemplos

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

C#
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

Si ExclusiveAddressUse es false, varios sockets pueden usar el Bind método para enlazar a un puerto específico; sin embargo, solo uno de los sockets puede realizar operaciones en el tráfico de red enviado al puerto. Si más de un socket intenta usar el Bind(EndPoint) método para enlazar a un puerto determinado, el que tenga la dirección IP más específica controlará el tráfico de red enviado a ese puerto.

Si ExclusiveAddressUse es true, el primer uso del Bind método para intentar enlazar a un puerto determinado, independientemente de la dirección del Protocolo de Internet (IP), se realizará correctamente; todos los usos posteriores del Bind método para intentar enlazar a ese puerto producirán un error hasta que se destruya el socket enlazado original.

Esta propiedad debe establecerse antes Bind de llamar a ; de lo contrario, se producirá una InvalidOperationException excepción .

Se aplica a

Producto Versiones
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1