- addr
- The System.Net.IPAddress of the remote host to which you intend to send data.
- port
- The port number to which you intend send data.
The UdpClient.Connect(string, int) 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 UdpClient.Send(Byte[], int, System.Net.IPEndPoint) 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 UdpClient.Connect(string, int) method or create another System.Net.Sockets.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 UdpClient.Send(Byte[], int, System.Net.IPEndPoint) method, UdpClient.Send(Byte[], int, System.Net.IPEndPoint) will throw a System.Net.Sockets.SocketException. If you receive a System.Net.Sockets.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 Socket.Connect(System.Net.EndPoint) 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 System.Net.Sockets.UdpClient, use the client method to obtain the underlying System.Net.Sockets.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 System.Net.IPAddress.Broadcast in your call to the UdpClient.Send(Byte[], int, System.Net.IPEndPoint) method. If your application requires greater control over broadcast addresses, you can also revert to using the System.Net.Sockets.Socket class.
Since the UDP protocol is connectionless, the UdpClient.Connect(string, int) method does not block. Do not call the UdpClient.Connect(string, int) method if you intend to receive multicasted datagrams.