Breyta

Deila með


UdpClient.Connect Method

Definition

Establishes a default remote host.

Overloads

Connect(IPEndPoint)

Establishes a default remote host using the specified network endpoint.

Connect(IPAddress, Int32)

Establishes a default remote host using the specified IP address and port number.

Connect(String, Int32)

Establishes a default remote host using the specified host name and port number.

Connect(IPEndPoint)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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)

Parameters

endPoint
IPEndPoint

An IPEndPoint that specifies the network endpoint to which you intend to send data.

Exceptions

An error occurred when accessing the socket.

endPoint is null.

Examples

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

Remarks

The Connect method establishes a default remote host using the value specified in the endPoint parameter. 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. If 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. 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. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 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.

If you call the Connect method, any datagrams that arrive from an address other than the specified default will be discarded. You 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.

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. If your application requires greater control over broadcast addresses, you can also revert to using the Socket class.

Note

Since the UDP protocol is connectionless, the Connect method does not block. Do not call the Connect method if you intend to receive multicasted datagrams.

See also

Applies to

Connect(IPAddress, Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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)

Parameters

addr
IPAddress

The IPAddress of the remote host to which you intend to send data.

port
Int32

The port number to which you intend send data.

Exceptions

addr is null.

port is not between MinPort and MaxPort.

An error occurred when accessing the socket.

Examples

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

Remarks

The Connect method establishes a default remote host using the values specified in the addr and port parameters. 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. If 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. 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. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 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.

If you call the Connect method, any datagrams that arrive from an address other than the specified default will be discarded. You 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.

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. If your application requires greater control over broadcast addresses, you can also revert to using the Socket class.

Note

Since the UDP protocol is connectionless, the Connect method does not block. Do not call the Connect method if you intend to receive multicasted datagrams.

See also

Applies to

Connect(String, Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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)

Parameters

hostname
String

The DNS name of the remote host to which you intend send data.

port
Int32

The port number on the remote host to which you intend to send data.

Exceptions

port is not between MinPort and MaxPort.

An error occurred when accessing the socket.

Examples

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

Remarks

The Connect method establishes a default remote host using the values specified in the port and hostname parameters. 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. If 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.

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. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 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.

If you call the Connect method, any datagrams that arrive from an address other than the specified default will be discarded. You 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.

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. If your application requires greater control over broadcast addresses, you can also revert to using the Socket class.

Note

Since the UDP protocol is connectionless, the Connect method does not block. Do not call the Connect method if you intend to receive multicasted datagrams.

See also

Applies to