Socket.IsBound 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會得到一個值,表示是否 Socket 綁定到特定的本地埠。
public:
property bool IsBound { bool get(); };
public bool IsBound { get; }
member this.IsBound : bool
Public ReadOnly Property IsBound As Boolean
屬性值
true若綁Socket定於本地端口;否則,。 false
範例
以下程式碼範例示範了該 IsBound 屬性的使用方式。
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("");
}
備註
若一個套接字透過呼叫Bind該方法明確綁定,或透過呼叫使用臨時本地埠(由作業系統選擇的空閒埠,大於 1024 的空閒埠)的成員ConnectSendToReceiveFrom,則該套接字被視為綁定於本地埠。伺服器會用這種Bind方法綁定到一個知名的埠口,讓用戶端能夠連接到它們。