Socket.IsBound 属性

定义

获取一个值,该值指示 Socket 是否绑定到特定本地端口。

public:
 property bool IsBound { bool get(); };
public bool IsBound { get; }
member this.IsBound : bool
Public ReadOnly Property IsBound As Boolean

属性值

如果 Socket 绑定到本地端口,则为 true;否则为 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 方法显式绑定,或者通过调用成员(如 ConnectSendToReceiveFrom)进行隐式绑定,则将其视为绑定到本地端口,这些成员使用操作系统选择的临时本地端口 (大于 1024 的可用端口。) 服务器使用 Bind 方法绑定到已知端口,以便客户端可以连接到它们。

适用于