UdpClient.Connect Method

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.

Overloads

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.

Parameters

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.

Remarks

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.

Exceptions

Example

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.

Important: For UDP, the 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.