UdpClient.SendAsync Method

System.Net.Sockets

Overloads

This page documents the following overloads:

SendAsync(byte[], Int32, IPEndPoint)

public Task<int> SendAsync(byte[] datagram, int bytes, IPEndPoint remoteEP)

Description

Sends a single datagram to a remote host asynchronously.

Parameters

Return Value

A Task<int> object that represents the asynchronous operation. The Result property of the task will contain the number of bytes sent.

Exceptions

Remarks

The SendAsync method sends the specified UDP datagram to the specified remote host. This method does not guarantee that the datagram will reach the remote host. UDP is a connectionless protocol.

If you are using a connection-oriented UDP socket, you should call the Connect method before calling SendAsync. If you are using a connectionless UDP socket, you can specify the destination address and port for each send operation.

Example


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

public class UdpClientExample
{
    public static async Task SendDatagramAsync(string ipAddress, int port, string message)
    {
        using (UdpClient udpClient = new UdpClient())
        {
            try
            {
                byte[] messageBytes = Encoding.UTF8.GetBytes(message);
                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);

                Console.WriteLine($"Sending '{message}' to {remoteEndPoint}...");

                int bytesSent = await udpClient.SendAsync(messageBytes, messageBytes.Length, remoteEndPoint);

                Console.WriteLine($"Successfully sent {bytesSent} bytes.");
            }
            catch (SocketException e)
            {
                Console.WriteLine($"Socket error while sending: {e.Message}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"An error occurred: {e.Message}");
            }
        }
    }

    public static async Task Main(string[] args)
    {
        // Replace with a valid IP address and port of a UDP listener
        string targetIp = "127.0.0.1";
        int targetPort = 11000;
        string dataToSend = "Hello, UDP!";

        await SendDatagramAsync(targetIp, targetPort, dataToSend);
    }
}

SendAsync(byte[], Int32, string, Int32)

public Task<int> SendAsync(byte[] datagram, int bytes, string hostname, int port)

Description

Sends a single datagram to a remote host asynchronously using the specified hostname and port.

Parameters

Return Value

A Task<int> object that represents the asynchronous operation. The Result property of the task will contain the number of bytes sent.

Exceptions

Remarks

This overload of SendAsync performs a DNS lookup for the specified hostname to obtain an IPAddress. It then sends the datagram to the resolved address and port.

If the UdpClient has been connected to a specific endpoint using Connect, calling this overload will throw a SocketException.

See Also

UdpClient Class

System.Net.Sockets Namespace