UdpClient.Connect UdpClient.Connect UdpClient.Connect UdpClient.Connect Method

定義

建立預設遠端主機。Establishes a default remote host.

多載

Connect(IPEndPoint) Connect(IPEndPoint) Connect(IPEndPoint) Connect(IPEndPoint)

使用指定的網路端點,建立預設的遠端主機。Establishes a default remote host using the specified network endpoint.

Connect(IPAddress, Int32) Connect(IPAddress, Int32) Connect(IPAddress, Int32) Connect(IPAddress, Int32)

使用指定的 IP 位址和通訊埠編號,建立預設遠端主機。Establishes a default remote host using the specified IP address and port number.

Connect(String, Int32) Connect(String, Int32) Connect(String, Int32) Connect(String, Int32)

使用指定的主機名稱和通訊埠編號,建立預設遠端主機。Establishes a default remote host using the specified host name and port number.

Connect(IPEndPoint) Connect(IPEndPoint) Connect(IPEndPoint) Connect(IPEndPoint)

使用指定的網路端點,建立預設的遠端主機。Establishes a default remote host using the specified network endpoint.

public:
 void Connect(System::Net::IPEndPoint ^ endPoint);
public void Connect (System.Net.IPEndPoint endPoint);
member this.Connect : System.Net.IPEndPoint -> unit
Public Sub Connect (endPoint As IPEndPoint)

參數

endPoint
IPEndPoint IPEndPoint IPEndPoint IPEndPoint

IPEndPoint,指定您想傳送資料所至的網路端點。An IPEndPoint that specifies the network endpoint to which you intend to send data.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列範例會使用IPEndPoint來建立預設遠端主機。The following example uses an IPEndPoint to establish a default remote host.

//Uses a remote endpoint to establish a socket connection.
UdpClient^ udpClient = gcnew UdpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddress,11004 );
try
{
   udpClient->Connect( ipEndPoint );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
//Uses a remote endpoint to establish a socket connection.
UdpClient udpClient = new UdpClient();
IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004);
try{
	udpClient.Connect(ipEndPoint);
}
catch (Exception e ) {
           Console.WriteLine(e.ToString());
       }
'Uses a remote endpoint to establish a socket connection.
Dim udpClient As New UdpClient()
Dim ipAddress As IPAddress = Dns.Resolve("www.contoso.com").AddressList(0)
Dim ipEndPoint As New IPEndPoint(ipAddress, 11004)
Try
   udpClient.Connect(ipEndPoint)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

備註

方法會使用endPoint參數中指定的值來建立預設遠端主機。 ConnectThe Connect method establishes a default remote host using the value specified in the endPoint parameter. 一旦建立之後, 您就不需要在每次呼叫Send方法時指定遠端主機。Once established, you do not have to specify a remote host in each call to the Send method.

建立預設遠端主機是選擇性的。Establishing a default remote host is optional. 指定預設遠端主機僅限於該主機。Specifying a default remote host limits you to that host only. 如果您想要將資料包傳送至不同的遠端主機, 您必須呼叫Connect方法, 或在沒有預設遠端主機的情況下建立另一個。 UdpClientIf you want to send datagrams to a different remote host, you must make another call to the Connect method or create another UdpClient without a default remote host. 如果您已建立預設遠端主機, 而且也在呼叫Send方法時提供遠端主機, Send將會擲SocketException回。If you have established a default remote host and you also provide a remote host in your call to the Send method, Send will throw a SocketException. 如果您收到SocketException, 請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後, 您可以參考Windows socket 第2版 API 錯誤碼檔, 以取得錯誤的詳細描述。Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

如果您呼叫Connect方法, 則會捨棄從指定之預設值以外的位址抵達的任何資料包。If you call the Connect method, any datagrams that arrive from an address other than the specified default will be discarded. 除非您繼承自UdpClient, 否則無法使用此方法將預設遠端主機設定為廣播位址, 請使用用戶端方法來取得基礎Socket, 並將通訊端選項設定為。 SocketOptionName.BroadcastYou cannot set the default remote host to a broadcast address using this method unless you inherit from UdpClient, use the Client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast.

不過, 如果您在對方法的IPAddress.Broadcast Send呼叫中指定, 您可以將資料廣播到預設廣播位址 (255.255.255.255)。You can however, broadcast data to the default broadcast address, 255.255.255.255, if you specify IPAddress.Broadcast in your call to the Send method. 如果您的應用程式需要更進一步控制廣播位址, 您也可以使用Socket類別來還原。If your application requires greater control over broadcast addresses, you can also revert to using the Socket class.

注意

因為 UDP 通訊協定是不需連線的Connect , 所以方法不會封鎖。Since the UDP protocol is connectionless, the Connect method does not block. 如果您想要Connect接收多播的資料包, 請勿呼叫方法。Do not call the Connect method if you intend to receive multicasted datagrams.

另請參閱

Connect(IPAddress, Int32) Connect(IPAddress, Int32) Connect(IPAddress, Int32) Connect(IPAddress, Int32)

使用指定的 IP 位址和通訊埠編號,建立預設遠端主機。Establishes a default remote host using the specified IP address and port number.

public:
 void Connect(System::Net::IPAddress ^ addr, int port);
public void Connect (System.Net.IPAddress addr, int port);
member this.Connect : System.Net.IPAddress * int -> unit
Public Sub Connect (addr As IPAddress, port As Integer)

參數

addr
IPAddress IPAddress IPAddress IPAddress

您想連接的遠端主機的 IPAddressThe IPAddress of the remote host to which you intend to send data.

port
Int32 Int32 Int32 Int32

您想傳送資料所至的通訊埠編號。The port number to which you intend send data.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列範例會使用 IP 位址和埠號碼來連接遠端主機。The following example uses an IP address and port number to connect with a remote host.

//Uses the IP address and port number to establish a socket connection.
UdpClient^ udpClient = gcnew UdpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
try
{
   udpClient->Connect( ipAddress, 11003 );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
//Uses the IP address and port number to establish a socket connection.
UdpClient udpClient = new UdpClient();
IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
try{
    udpClient.Connect(ipAddress, 11003);
}
catch (Exception e ) {
           Console.WriteLine(e.ToString());
}
'Uses the IP address and port number to establish a socket connection.
Dim udpClient As New UdpClient()
Dim ipAddress As IPAddress = Dns.Resolve("www.contoso.com").AddressList(0)
Try
   udpClient.Connect(ipAddress, 11003)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

備註

方法會使用addrport參數中指定的值來建立預設遠端主機。 ConnectThe Connect method establishes a default remote host using the values specified in the addr and port parameters. 一旦建立之後, 您就不需要在每次呼叫Send方法時指定遠端主機。Once established, you do not have to specify a remote host in each call to the Send method.

建立預設遠端主機是選擇性的。Establishing a default remote host is optional. 指定預設遠端主機僅限於該主機。Specifying a default remote host limits you to that host only. 如果您想要將資料包傳送至不同的遠端主機, 您必須呼叫Connect方法, 或在沒有預設遠端主機的情況下建立另一個。 UdpClientIf you want to send datagrams to a different remote host, you must make another call to the Connect method or create another UdpClient without a default remote host. 如果您已建立預設遠端主機, 而且也在呼叫Send方法時提供遠端主機, Send將會擲SocketException回。If you have established a default remote host and you also provide a remote host in your call to the Send method, Send will throw a SocketException. 如果您收到SocketException, 請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後, 您可以參考Windows socket 第2版 API 錯誤碼檔, 以取得錯誤的詳細描述。Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

如果您呼叫Connect方法, 則會捨棄從指定之預設值以外的位址抵達的任何資料包。If you call the Connect method, any datagrams that arrive from an address other than the specified default will be discarded. 除非您繼承自UdpClient, 否則無法使用此方法將預設遠端主機設定為廣播位址, 請使用用戶端方法來取得基礎Socket, 並將通訊端選項設定為。 SocketOptionName.BroadcastYou cannot set the default remote host to a broadcast address using this method unless you inherit from UdpClient, use the client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast.

不過, 如果您在對方法的IPAddress.Broadcast Send呼叫中指定, 您可以將資料廣播到預設廣播位址 (255.255.255.255)。You can however, broadcast data to the default broadcast address, 255.255.255.255, if you specify IPAddress.Broadcast in your call to the Send method. 如果您的應用程式需要更進一步控制廣播位址, 您也可以使用Socket類別來還原。If your application requires greater control over broadcast addresses, you can also revert to using the Socket class.

注意

因為 UDP 通訊協定是不需連線的Connect , 所以方法不會封鎖。Since the UDP protocol is connectionless, the Connect method does not block. 如果您想要Connect接收多播的資料包, 請勿呼叫方法。Do not call the Connect method if you intend to receive multicasted datagrams.

另請參閱

Connect(String, Int32) Connect(String, Int32) Connect(String, Int32) Connect(String, Int32)

使用指定的主機名稱和通訊埠編號,建立預設遠端主機。Establishes a default remote host using the specified host name and port number.

public:
 void Connect(System::String ^ hostname, int port);
public void Connect (string hostname, int port);
member this.Connect : string * int -> unit
Public Sub Connect (hostname As String, port As Integer)

參數

hostname
String String String String

您想傳送資料所至的遠端主機的 DNS 名稱。The DNS name of the remote host to which you intend send data.

port
Int32 Int32 Int32 Int32

您想傳送資料所至的遠端主機的通訊埠編號。The port number on the remote host to which you intend to send data.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列範例會使用主機名稱和埠號碼來連接至遠端主機。The following example uses the host name and port number to connect to a remote host.

//Uses a host name and port number to establish a socket connection.
UdpClient^ udpClient = gcnew UdpClient;
try
{
   udpClient->Connect( "www.contoso.com", 11002 );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
 //Uses a host name and port number to establish a socket connection.
UdpClient udpClient = new UdpClient();
try{
    udpClient.Connect("www.contoso.com", 11002);
}
catch (Exception e ) {
           Console.WriteLine(e.ToString());
       }
'Uses a host name and port number to establish a socket connection.
Dim udpClient As New UdpClient()
Try
   udpClient.Connect("www.contoso.com", 11002)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

備註

方法會使用porthostname參數中指定的值來建立預設遠端主機。 ConnectThe Connect method establishes a default remote host using the values specified in the port and hostname parameters. 一旦建立之後, 您就不需要在每次呼叫Send方法時指定遠端主機。Once established, you do not have to specify a remote host in each call to the Send method.

建立預設遠端主機是選擇性的。Establishing a default remote host is optional. 指定預設遠端主機僅限於該主機。Specifying a default remote host limits you to that host only. 如果您想要將資料包傳送至不同的遠端主機, 您必須呼叫Connect方法, 或在沒有預設遠端主機的情況下建立另一個。 UdpClientIf you want to send datagrams to a different remote host, you must make another call to the Connect method or create another UdpClient without a default remote host.

如果您已建立預設遠端主機, 而且也在呼叫Send方法時提供遠端主機, Send將會擲SocketException回。If you have established a default remote host and you also provide a remote host in your call to the Send method, Send will throw a SocketException. 如果您收到SocketException, 請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後, 您可以參考Windows socket 第2版 API 錯誤碼檔, 以取得錯誤的詳細描述。Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

如果您呼叫Connect方法, 則會捨棄從指定之預設值以外的位址抵達的任何資料包。If you call the Connect method, any datagrams that arrive from an address other than the specified default will be discarded. 除非您繼承自UdpClient, 否則無法使用此方法將預設遠端主機設定為廣播位址, 請使用用戶端方法來取得基礎Socket, 並將通訊端選項設定為。 SocketOptionName.BroadcastYou cannot set the default remote host to a broadcast address using this method unless you inherit from UdpClient, use the client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast.

不過, 如果您在對方法的IPAddress.Broadcast Send呼叫中指定, 您可以將資料廣播到預設廣播位址 (255.255.255.255)。You can however, broadcast data to the default broadcast address, 255.255.255.255, if you specify IPAddress.Broadcast in your call to the Send method. 如果您的應用程式需要更進一步控制廣播位址, 您也可以使用Socket類別來還原。If your application requires greater control over broadcast addresses, you can also revert to using the Socket class.

注意

因為 UDP 通訊協定是不需連線的Connect , 所以方法不會封鎖。Since the UDP protocol is connectionless, the Connect method does not block. 如果您想要Connect接收多播的資料包, 請勿呼叫方法。Do not call the Connect method if you intend to receive multicasted datagrams.

另請參閱

適用於