UdpReceiveResult Struct

System.Net.Sockets

Summary

Represents the result of a UDP datagram reception operation.

This structure is returned by the UdpClient.ReceiveAsync and Socket.ReceiveFromAsync methods, providing information about the received datagram, including the data buffer, the endpoint from which it was received, and the number of bytes received.

Fields

D
int BytesTransferred
The number of bytes received.
E
IPEndPoint RemoteEndPoint
The endpoint of the remote host from which the datagram was received.
B
byte[] Buffer
The buffer containing the received UDP datagram data.

Constructors

+
UdpReceiveResult(byte[] buffer, IPEndPoint remoteEndPoint)
Initializes a new instance of the UdpReceiveResult structure with the specified buffer and remote endpoint.
+
UdpReceiveResult(byte[] buffer, int bytesTransferred, IPEndPoint remoteEndPoint)
Initializes a new instance of the UdpReceiveResult structure with the specified buffer, number of bytes transferred, and remote endpoint.

Properties

P
byte[] Buffer { get; }
Gets the buffer containing the received UDP datagram data.
P
int BytesTransferred { get; }
Gets the number of bytes received.
P
IPEndPoint RemoteEndPoint { get; }
Gets the endpoint of the remote host from which the datagram was received.

Methods

M
bool Equals(object obj)
Determines whether the specified object is equal to the current object.
M
int GetHashCode()
Serves as the default hash function.
M
string ToString()
Returns a string that represents the current object.

Example

The following example demonstrates how to use UdpClient.ReceiveAsync to receive UDP datagrams and process the UdpReceiveResult.

1 using System; 2 using System.Net; 3 using System.Net.Sockets; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 public class UdpReceiver 8 { 9 public async static Task ReceiveDataAsync(int port) 10 { 11 using var udpClient = new UdpClient(port); 12 // Set a larger buffer size if expecting larger datagrams 13 byte[] receiveBuffer = new byte[1024]; 14 udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000); 15 16 Console.WriteLine($"Listening on port {port}..."); 17 18 while (true) 19 { 20 try 21 { 22 // ReceiveAsync returns a Task<UdpReceiveResult> 23 UdpReceiveResult result = await udpClient.ReceiveAsync(receiveBuffer); 24 25 // The result contains the received data, the number of bytes, and the remote endpoint 26 byte[] receivedBytes = new byte[result.BytesTransferred]; 27 Array.Copy(result.Buffer, receivedBytes, result.BytesTransferred); 28 29 string receivedMessage = Encoding.ASCII.GetString(receivedBytes); 30 31 Console.WriteLine( 32 $"Received {result.BytesTransferred} bytes from {result.RemoteEndPoint}: {receivedMessage}" 33 ); 34 35 // Optionally, send a response back 36 byte[] responseBytes = Encoding.ASCII.GetBytes("Acknowledged!"); 37 await udpClient.SendAsync(responseBytes, responseBytes.Length, result.RemoteEndPoint); 38 } 39 catch (SocketException ex) 40 { 41 // Handle potential timeouts or other socket errors 42 Console.WriteLine("SocketException: {ex.Message}"); 43 if (ex.SocketErrorCode == SocketError.TimedOut) 44 { 45 Console.WriteLine("Receive timed out. Still listening..."); 46 } 47 else 48 { 49 // Consider breaking the loop or re-throwing for critical errors 50 break; 51 } 52 } 53 catch (Exception ex) 54 { 55 Console.WriteLine("General Exception: {ex.Message}"); 56 break; 57 } 58 } 59 } 60 61 // Example of how to call this receiver 62 public static void Main(string[] args) 63 { 64 // Replace 11000 with your desired listening port 65 ReceiveDataAsync(11000).GetAwaiter().GetResult(); 66 } 67}