UdpClient.Receive Method
Receives a datagram from the UdpClient.
public byte[] Receive(ref System.Net.EndPoint remoteEP)
public System.Threading.Tasks.Task<byte[]> ReceiveAsync()
public System.Threading.Tasks.Task<byte[]> ReceiveAsync(System.Threading.CancellationToken cancellationToken)
Parameters
| Parameter | Description |
|---|---|
ref System.Net.EndPoint remoteEP |
A EndPoint that contains the network address and port number of the sender. |
System.Threading.CancellationToken cancellationToken |
A CancellationToken to observe while waiting for the task to complete. |
Return Value
A byte array containing the data received.
Remarks
The Receive method blocks until a datagram is received.
If you are using a connectionless socket, you must specify the
sender's endpoint by calling ReceiveFrom or
ReceiveFromAsync.
If the UdpClient is bound to a specific remote host using
the Connect method, Receive will only return
datagrams sent from that host.
The ReceiveAsync methods provide an asynchronous way to receive
datagrams, allowing your application to remain responsive.
ReceiveFrom or ReceiveFromAsync to get both the data and the sender's endpoint in a single operation. The Receive method is primarily used when the UdpClient has been connected to a specific remote endpoint using the Connect method.
Example
The following code example demonstrates how to create a UDP client, bind it to a local port, and then receive data.
C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
public class UdpReceiver
{
public static void Main(string[] args)
{
int port = 11000;
UdpClient receiver = null;
try
{
receiver = new UdpClient(port);
Console.WriteLine($"UDP server listening on port {port}...");
// Asynchronously receive data
Task.Run(async () =>
{
while (true)
{
try
{
UdpReceiveResult result = await receiver.ReceiveAsync();
string message = Encoding.ASCII.GetString(result.Buffer);
IPEndPoint remoteEP = result.RemoteEndPoint;
Console.WriteLine($"Received '{message}' from {remoteEP.Address}:{remoteEP.Port}");
// Example: Sending a confirmation back
byte[] confirmation = Encoding.ASCII.GetBytes("Message received.");
await receiver.SendAsync(confirmation, confirmation.Length, remoteEP);
}
catch (Exception ex)
{
Console.WriteLine($"Error receiving data: {ex.Message}");
}
}
});
// Keep the main thread alive
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine($"Error starting UDP server: {ex.Message}");
}
finally
{
receiver?.Close();
}
}
}
Exceptions
| Exception | Condition |
|---|---|
| SocketException | An error occurred while accessing the socket. |
| ObjectDisposedException | The UdpClient has been disposed. |
| OperationCanceledException | The operation was canceled by the CancellationToken. |
Requirements
| Component | Version |
|---|---|
| Namespace | System.Net.Sockets |
| Assembly | System.Net.Sockets.dll |
| .NET Framework versions | Supported in the following versions: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0 Client Profile also supported. |
| Mono versions | Supported in the following versions: 6.4.0, 5.4.0, 4.6.2, 3.12.0, 3.10.0, 3.8.0, 3.6.0, 3.2.0, 2.10.1, 2.8.0 |
| Xamarin.Mac | 1.0 |
| NuGet packages | Can be used in the following packages: - System.Net.Sockets (from version 4.0.0) |