共用方式為


Socket.ExclusiveAddressUse 屬性

定義

取得或設定一個值,指示是否 Socket 只允許一個程序綁定一個埠。

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

屬性值

true 若 允許 Socket 僅一個插槽綁定特定埠;否則, false。 預設是 true Windows Server 2003 以及 Windows XP 及更新版本。

例外狀況

嘗試存取該套接字時發生錯誤。

該店 Socket 已經關閉。

範例

以下程式碼範例示範了該 ExclusiveAddressUse 屬性的使用方式。

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

備註

ExclusiveAddressUsefalse多個套接字可使用此 Bind 方法綁定特定埠;但只有一個套接字能對傳送至該埠的網路流量執行操作。 如果多個 socket 嘗試使用該 Bind(EndPoint) 方法綁定特定埠口,則擁有較特定 IP 位址的 socket 會處理傳送到該埠的網路流量。

ExclusiveAddressUsetrue,則該方法首次 Bind 嘗試綁定特定埠口,無論網際協定(IP)位址為何,皆將成功;之後 Bind 所有嘗試綁定該埠的方法都會失敗,直到原始綁定套接字被摧毀為止。

此性質必須在被呼叫前 Bind 設定;否則會拋出 will InvalidOperationException

適用於