Socket.IsBound Socket.IsBound Socket.IsBound Socket.IsBound Property

定义

获取一个值,该值指示 Socket 是否绑定到特定本地端口。Gets a value that indicates whether the Socket is bound to a specific local port.

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

属性值

如果 Socket 绑定到本地端口,则为 true;否则为 falsetrue if the Socket is bound to a local port; otherwise, false.

示例

下面的代码示例演示如何将IsBound属性。The following code example demonstrates the use of the IsBound property.

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 = gcnew 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 {0}", 
        tcpSocket->ExclusiveAddressUse);
    Console::WriteLine("  LingerState {0}, {1}", 
        tcpSocket->LingerState->Enabled,
        tcpSocket->LingerState->LingerTime);
    Console::WriteLine("  NoDelay {0}",
        tcpSocket->NoDelay);
    Console::WriteLine("  ReceiveBufferSize {0}", 
        tcpSocket->ReceiveBufferSize);
    Console::WriteLine("  ReceiveTimeout {0}",
        tcpSocket->ReceiveTimeout);
    Console::WriteLine("  SendBufferSize {0}",
        tcpSocket->SendBufferSize);
    Console::WriteLine("  SendTimeout {0}",
        tcpSocket->SendTimeout);
    Console::WriteLine("  Ttl {0}",
        tcpSocket->Ttl);
    Console::WriteLine("  IsBound {0}",
        tcpSocket->IsBound);
    Console::WriteLine("");
}
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 {0}", 
				tcpSocket.ExclusiveAddressUse);

	Console.WriteLine("  LingerState {0}, {1}", 
			     tcpSocket.LingerState.Enabled, 
		             tcpSocket.LingerState.LingerTime);

	Console.WriteLine("  NoDelay {0}", 
                                              tcpSocket.NoDelay);

	Console.WriteLine("  ReceiveBufferSize {0}", 
				tcpSocket.ReceiveBufferSize);

	Console.WriteLine("  ReceiveTimeout {0}", 
				tcpSocket.ReceiveTimeout);

	Console.WriteLine("  SendBufferSize {0}", 
				tcpSocket.SendBufferSize);

	Console.WriteLine("  SendTimeout {0}", 
                                              tcpSocket.SendTimeout);

	Console.WriteLine("  Ttl {0}", 
                                              tcpSocket.Ttl);

                      Console.WriteLine("  IsBound {0}", 
                                              tcpSocket.IsBound);

	Console.WriteLine("");
}

注解

一个套接字被视为绑定到本地端口,如果它显式绑定通过调用Bind方法,或通过调用成员,例如隐式绑定ConnectSendTo,或ReceiveFrom,它使用临时的本地端口 (了可用端口大于1024,由操作系统选择。)服务器使用Bind方法,以便客户端可能会连接到其绑定到的已知端口。A socket is considered bound to a local port if it is explicitly bound by calling the Bind method, or implicitly bound by calling members like Connect, SendTo, or ReceiveFrom, which use an ephemeral local port (a free port greater than 1024, selected by the operating system.) Servers use the Bind method to bind to a well-known port so that clients may connect to them.

适用于