Compartir vía


Socket.ExclusiveAddressUse Propiedad

Definición

Obtiene o establece un valor que indica si Socket solo permite que un proceso se enlace a un puerto.

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

Valor de propiedad

true Socket si solo permite que un socket se enlace a un puerto específico; de lo contrario, false. El valor predeterminado es true para Windows Server 2003 y Windows XP y versiones más recientes.

Excepciones

Error al intentar acceder al socket.

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

Ejemplos

En el ejemplo de código siguiente se muestra el uso de la ExclusiveAddressUse 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 = 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 tiene 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 de Bind llamarse; de lo contrario, se producirá una InvalidOperationException excepción .

Se aplica a