C
C#9mo ago
Asher

❔ Retrieving message from a UDP socket

Heyo I'm following some guide thats using js, I need to find the C# equivalent
9 Replies
Asher
Asher9mo ago
in his code he sends a UDP message then does: socket.on('message', () => {}); I have something similar:
using (UdpClient udpClient = new UdpClient())
{
udpClient.Client.SendTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
udpClient.Client.ReceiveTimeout = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;

Logger.Log($"sending message to {uri.Host}, returning.", source: "UdpRequest");

udpClient.Send(message, message.Length, uri.Host, uri.Port);

// begin recieve right after request
result = udpClient.BeginReceive(null, null);
if (result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)))
{
Logger.Log($"Recieved message from endpoint, returning.", source: "UdpRequest");

#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
data = udpClient.EndReceive(result, ref any);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
else
{
// here the client just times out.
}
}
using (UdpClient udpClient = new UdpClient())
{
udpClient.Client.SendTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
udpClient.Client.ReceiveTimeout = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;

Logger.Log($"sending message to {uri.Host}, returning.", source: "UdpRequest");

udpClient.Send(message, message.Length, uri.Host, uri.Port);

// begin recieve right after request
result = udpClient.BeginReceive(null, null);
if (result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)))
{
Logger.Log($"Recieved message from endpoint, returning.", source: "UdpRequest");

#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
data = udpClient.EndReceive(result, ref any);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
else
{
// here the client just times out.
}
}
the point is the 2 lines in the middle, a Send() then right after a Recieve, this fails me however and it says it's not getting any message back is there a better way of handling this where a socket automatically returns me the message back?
Jimmacle
Jimmacle9mo ago
i'm pretty sure that's not how you use the old async APIs, you have to give it a handler that calls EndReceive inside it: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient.beginreceive?view=net-7.0
UdpClient.BeginReceive(AsyncCallback, Object) Method (System.Net.So...
Receives a datagram from a remote host asynchronously.
Jimmacle
Jimmacle9mo ago
if i were you i'd use a Socket which has actual async/await methods afaik TcpClient and UdpClient are just thin less flexible wrappers over that anyway but it also doesn't look like you're actually doing something asynchronously, so
jcotton42
jcotton429mo ago
UdpClient has Async methods as well @archyinuse there is no reason at all to use the Begin/End methods these days they're a very very old way of doing async but yeah I would just use a Socket here, TcpClient and UdpClient are just super thin wrappers over one anyhow
Asher
Asher9mo ago
I have to use UDP client because it has auto DNS lookup which I don't know if I want to do because I have some really weird URLs I need a timeout that's why I'm using it receive async doesn't have a timeout I am, the send is async but I changed it for testing purposes there's an await in there
jcotton42
jcotton429mo ago
You can pass a cancellation token, which can be canceled after a certain time There's also the WaitAsync method on Tasks these days
Jimmacle
Jimmacle9mo ago
Socket also supports connecting by hostname
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.