Socket.Bind(EndPoint) 方法

定義

使 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 關聯的本機 SocketThe local EndPoint to associate with the Socket.

例外狀況

localEPnulllocalEP is null.

嘗試存取通訊端時發生錯誤。An error occurred when attempting to access the socket.

Socket 已關閉。The Socket has been closed.

在呼叫堆疊中位置較高的呼叫端對於要求的作業沒有權限。A caller higher in the call stack does not have permission for the requested operation.

範例

下列程式碼範例會使用指定的本機端點來系結 SocketThe 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. 除非您需要使用特定的本機端點,否則在使用 Connect 方法之前,不需要先呼叫 BindYou 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之前,您必須先建立您要用來傳達資料的本機 IPEndPointBefore calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. 如果您不在意指派的本機位址,您可以使用 IPAddress.Any 作為 address 參數來建立 IPEndPoint,而基礎服務提供者會指派最適當的網路位址。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. 如果您不在意使用哪個本機埠,可以使用0來建立埠號碼的 IPEndPointIf 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. 如果您使用的是連線導向的通訊協定,除非您呼叫 ConnectEndConnect 方法,否則 LocalEndPoint 不會傳回本機指派的網路位址。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 通訊端想要在收到的封包上接收介面資訊,應該在呼叫 Bind 方法之後,明確地呼叫 SetSocketOption 方法,並將 socket 選項設為 PacketInformationIf 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.

注意

如果您想要使用 ReceiveFrom 方法來接收不需連線的資料包,您必須呼叫 Bind 方法。You must call the Bind method if you intend to receive connectionless datagrams using the ReceiveFrom method.

注意

如果您在呼叫 Bind 方法時收到 SocketException,請使用 SocketException.ErrorCode 屬性來取得特定的錯誤碼。If you receive a SocketException when calling the Bind method, use the SocketException.ErrorCode property to obtain the specific error code. 取得此程式碼之後,請參閱Windows socket 第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

適用於

另請參閱