This page documents the following overloads:
Sends a single datagram to a remote host asynchronously.
datagram
The data to send.
bytes
The number of bytes to send from the datagram buffer.
remoteEP
An IPEndPoint that specifies the remote host and port to which to send the datagram.
A Task<int> object that represents the asynchronous operation. The Result property of the task will contain the number of bytes sent.
ArgumentNullException: datagram is null or remoteEP is null.ArgumentOutOfRangeException: bytes is less than zero or greater than the length of datagram.ObjectDisposedException: The UdpClient has been closed.SocketException: An error occurred while sending the datagram.
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.
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);
}
}
Sends a single datagram to a remote host asynchronously using the specified hostname and port.
datagram
The data to send.
bytes
The number of bytes to send from the datagram buffer.
hostname
The hostname or IP address of the remote host.
port
The port to which to send the datagram.
A Task<int> object that represents the asynchronous operation. The Result property of the task will contain the number of bytes sent.
ArgumentNullException: datagram is null.ArgumentOutOfRangeException: bytes is less than zero or greater than the length of datagram, or port is not within the valid range of port numbers.ArgumentException: hostname is null or empty.ObjectDisposedException: The UdpClient has been closed.SocketException: An error occurred while resolving the hostname or sending the datagram.
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.