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

注解

如果需要使用特定的本地终结点, 请使用方法。BindUse 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作为 address 参数创建, 基础服务提供程序将分配最合适的网络地址。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.

如果使用以上方法, 则可以通过调用LocalEndPoint来发现分配了哪些本地网络地址和端口号。If 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 , 则应在调用Bind方法后, 使用套接字PacketInformation选项显式调用此方法。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
用于接受来自定义localEP的主机的连接。for accepting connections from the host defined by localEP. 关联的枚举:AcceptAssociated enumeration: Accept

适用于

另请参阅