❔ Retrieving message from a UDP socket
Heyo I'm following some guide thats using js, I need to find the C# equivalent
9 Replies
in his code he sends a UDP message then does:
socket.on('message', () => {});
I have something similar:
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?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.0UdpClient.BeginReceive(AsyncCallback, Object) Method (System.Net.So...
Receives a datagram from a remote host asynchronously.
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, soUdpClient 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
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
You can pass a cancellation token, which can be canceled after a certain time
There's also the WaitAsync method on Tasks these days
Socket
also supports connecting by hostnameSocket Class (System.Net.Sockets)
Implements the Berkeley sockets interface.
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.