UdpClient.Connect Method (IPAddress, Int32)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Net.Sockets
Assembly:  System (in System.dll)

public void Connect(
	IPAddress addr,
	int port


Type: System.Net.IPAddress

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

Type: System.Int32

The port number to which you intend send data.

Exception Condition

UdpClient is closed.


addr is null.


port is not between MinPort and MaxPort.


An error occurred when accessing the socket. See the Remarks section for more information.

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 in MSDN 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,, 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.


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.

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 = new UdpClient();
IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
    udpClient.Connect(ipAddress, 11003);
catch (Exception e ) {

.NET Framework
Available since 1.1
Return to top