UdpClient.Connect Method (String, Int32)

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

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

public void Connect(
	string hostname,
	int port
)

Parameters

hostname
Type: System.String
The DNS name of the remote host to which you intend send data.
port
Type: System.Int32
The port number on the remote host to which you intend to send data.

ExceptionCondition
ObjectDisposedException

The UdpClient is closed.

ArgumentOutOfRangeException

port is not between MinPort and MaxPort.

SocketException

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

NoteNote

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 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 = new UdpClient();
try{
    udpClient.Connect("www.contoso.com", 11002);
}
catch (Exception e ) {
           Console.WriteLine(e.ToString());
       }


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft