SocketReceiveControlData
Represents the control data that can be received with a socket. This structure is used in conjunction with the Socket.ReceiveMessageFromAsync and Socket.ReceiveFromAsync methods to retrieve control information from incoming datagrams.
-
Properties
ControlMessagesReceived
Indicates whether control data was received for this datagram.Group
Gets the IP group that the datagram was sent to.Origin
Gets the IP address and port number of the sender of the datagram.ProtocolInformation
Gets protocol-specific information about the received datagram.RemoteEndPoint
Gets the network endpoint of the sender of the datagram.Timestamp
Gets the timestamp indicating when the datagram was received.
ControlMessagesReceived
get: bool
true if control data was available for the received datagram, and false otherwise.
Group
get: System.Net.IPAddress
Origin
get: System.Net.EndPoint
RemoteEndPoint.
ProtocolInformation
get: object
RemoteEndPoint
get: System.Net.EndPoint
Origin.
Timestamp
get: System.Net.Sockets.SocketFlags
The SocketReceiveControlData structure is designed to encapsulate various pieces of control information that might be associated with a received network packet. This allows applications to gain deeper insights into the network communication beyond just the payload data.
When using methods like Socket.ReceiveMessageFromAsync, you can specify SocketFlags.ControlData to request that this control information be populated.
The following example demonstrates how to use SocketReceiveControlData when receiving datagrams asynchronously.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
public class SocketListener
{
private Socket _socket;
private byte[] _buffer = new byte[1024];
public async Task StartListeningAsync(int port)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
Console.WriteLine($"Listening on UDP port {port}...");
while (true)
{
try
{
var result = await _socket.ReceiveMessageFromAsync(
_buffer,
0,
_buffer.Length,
SocketFlags.None,
SocketFlags.ControlData, // Request control data
new CancellationTokenSource().Token);
var receiveData = result.ReceivedData;
var remoteEndPoint = result.RemoteEndPoint;
var controlData = result.ControlData; // This will be populated if ControlData flag was used
if (controlData != null && controlData.ControlMessagesReceived)
{
Console.WriteLine($"\n--- Control Data Received ---");
Console.WriteLine($"Remote Endpoint: {remoteEndPoint}");
Console.WriteLine($"Origin Endpoint: {controlData.Origin}");
Console.WriteLine($"Multicast Group: {controlData.Group ?? IPAddress.None}");
Console.WriteLine($"Timestamp: {controlData.Timestamp}");
if (controlData.ProtocolInformation != null)
{
Console.WriteLine($"Protocol Info Type: {controlData.ProtocolInformation.GetType().Name}");
// You might want to cast and inspect ProtocolInformation further based on expected protocols
}
Console.WriteLine($"---------------------------");
}
string receivedMessage = Encoding.ASCII.GetString(_buffer, 0, receiveData.Value);
Console.WriteLine($"Received from {remoteEndPoint}: {receivedMessage}");
// Clear buffer for next receive (optional, depending on logic)
Array.Clear(_buffer, 0, _buffer.Length);
}
catch (Exception ex)
{
Console.WriteLine($"Error receiving message: {ex.Message}");
// Consider breaking the loop or handling specific exceptions
}
}
}
public void StopListening()
{
_socket?.Close();
_socket?.Dispose();
}
// Example usage:
// public static async Task Main(string[] args)
// {
// var listener = new SocketListener();
// await listener.StartListeningAsync(11000); // Example port
// }
}