Establishes a default remote host and port for the UdpClient. This is useful for scenarios where you are sending all your UDP datagrams to the same destination.
public void Connect(string hostname, int port);Establishes a default remote host and port for the UdpClient.
public void Connect(IPEndPoint remoteEP);Establishes a default remote host and port for the UdpClient.
| Name | Type | Description |
|---|---|---|
hostname |
string |
The host name of the remote device. |
port |
int |
The port number of the remote device. |
remoteEP |
IPEndPoint |
An IPEndPoint object that specifies the remote host and port. |
When you call the Connect method, the UdpClient binds to a local port. If you do not explicitly bind the UdpClient to a local port, the system will assign one.
After calling Connect, you can use the Send and Receive methods without specifying a remote endpoint.
The Connect method is required to use the Receive method without an EndPoint parameter.
Calling Connect on a UdpClient that has already been connected will result in an SocketException.
ArgumentNullException: The hostname parameter is null.ArgumentOutOfRangeException: The port is not within the range of valid port numbers.SocketException: An error occurred while accessing the socket.ObjectDisposedException: The UdpClient has been closed.The following example demonstrates how to use the Connect method to send a UDP datagram.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpClientExample
{
public static void Main(string[] args)
{
try
{
// Specify the remote host and port
string remoteHost = "127.0.0.1";
int remotePort = 11000;
// Create a UdpClient instance
using (var udpClient = new UdpClient())
{
// Connect to the remote host and port
udpClient.Connect(remoteHost, remotePort);
Console.WriteLine($"Connected to {remoteHost}:{remotePort}");
// Data to send
byte[] sendBytes = Encoding.ASCII.GetBytes("Hello, UDP Server!");
// Send the data
int bytesSent = udpClient.Send(sendBytes, sendBytes.Length);
Console.WriteLine($"Sent {bytesSent} bytes.");
// Optional: Receive a response (if the server sends one)
// UdpClient.Connect only sets the default destination for sending.
// To receive, you might still need an IPEndPoint or use the overload of Receive.
// For simple send-only scenarios, you might skip this part.
}
}
catch (SocketException e)
{
Console.WriteLine($"SocketException: {e.Message}");
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
}
This example shows how to connect to a UDP endpoint and send a simple message. Remember that UDP is a connectionless protocol, so Connect primarily sets the default destination for Send operations and enables Receive without an endpoint parameter.
Connect method does not establish a dedicated connection like TCP. It simply sets the default remote endpoint for subsequent Send operations and allows Receive to operate without specifying an endpoint. The underlying socket remains available for sending to other destinations if explicitly specified, though this is less common after calling Connect.