Socket.Bind(EndPoint) Socket.Bind(EndPoint) Socket.Bind(EndPoint) Socket.Bind(EndPoint) Method

定义

使 Socket 与一个本地终结点相关联。Associates a Socket with a local endpoint.

public:
 void Bind(System::Net::EndPoint ^ localEP);
public void Bind (System.Net.EndPoint localEP);
member this.Bind : System.Net.EndPoint -> unit
Public Sub Bind (localEP As EndPoint)

参数

localEP
EndPoint EndPoint EndPoint EndPoint

要与 EndPoint 关联的本地 SocketThe local EndPoint to associate with the Socket.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

调用堆栈中的较高调用方无权执行所请求的操作。A caller higher in the call stack does not have permission for the requested operation.

示例

下面的代码示例绑定Socket使用指定的本地终结点。The following code example binds a Socket using the specified local endpoint.

try
{
   aSocket->Bind( anEndPoint );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( "Winsock error: {0}", e );
}
try {
    aSocket.Bind(anEndPoint);
}
catch (Exception e) {
    Console.WriteLine("Winsock error: " + e.ToString());
}

Try
    aSocket.Bind(anEndPoint)
Catch e As Exception
    Console.WriteLine("Winsock error: " & e.ToString())
End Try

注解

使用Bind方法如果你需要使用特定的本地终结点。Use the Bind method if you need to use a specific local endpoint. 必须调用Bind调用之前Listen方法。You must call Bind before you can call the Listen method. 不需要调用Bind之前使用Connect方法除非您需要使用特定的本地终结点。You do not need to call Bind before using the Connect method unless you need to use a specific local endpoint. 可以使用Bind上无连接和面向连接的协议方法。You can use the Bind method on both connectionless and connection-oriented protocols.

然后再调用Bind,必须先创建本地IPEndPoint从想要进行数据通信。Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. 如果您不介意分配的本地地址,则可以创建IPEndPoint使用IPAddress.Any地址参数,以及基础的服务提供程序将分配的最合适的网络地址。If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress.Any as the address parameter, and the underlying service provider will assign the most appropriate network address. 这可能有助于简化您的应用程序,如果有多个网络接口。This might help simplify your application if you have multiple network interfaces. 如果你不关心使用哪个本地端口,可以创建IPEndPoint使用 0 作为端口号。If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. 在这种情况下,服务提供商将分配可用的端口号介于 1024年和 5000 之间。In this case, the service provider will assign an available port number between 1024 and 5000.

如果使用上述方法,可以发现哪些本地网络地址和端口号已分配通过调用LocalEndPointIf you use the above approach, you can discover what local network address and port number has been assigned by calling the LocalEndPoint. 如果使用面向连接的协议LocalEndPoint进行调用后不会返回之前的本地分配的网络地址ConnectEndConnect方法。If you are using a connection-oriented protocol, LocalEndPoint will not return the locally assigned network address until after you have made a call to the Connect or EndConnect method. 如果使用的是无连接协议,将不具有访问此信息,直到已完成发送或接收。If you are using a connectionless protocol, you will not have access to this information until you have completed a send or receive.

如果 UDP 套接字想要接收接口上接收数据包的信息SetSocketOption套接字选项设置为应显式调用方法PacketInformation后立即调用Bind方法。If a UDP socket wants to receive interface information on received packets, the SetSocketOption method should be explicitly called with the socket option set to PacketInformation immediately after calling the Bind method.

备注

如果您想要接收多播数据报,必须调用Bind方法具有多路广播的端口号。If you intend to receive multicast datagrams, you must call the Bind method with a multicast port number.

备注

必须调用Bind方法,如果你想要接收无连接的数据报使用ReceiveFrom方法。You must call the Bind method if you intend to receive connectionless datagrams using the ReceiveFrom method.

备注

如果你收到SocketException调用时Bind方法,请使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException when calling the Bind method, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自定义的主机的连接localEPfor accepting connections from the host defined by localEP. 关联的枚举: AcceptAssociated enumeration: Accept

适用于

另请参阅