0

I have a UDP library that works fine in .Net 4.x but I am trying to port it to .NET Core for use with an ASP Net Core MVC site. The site sends and receives UDP datagrams from sensors via a third-party 'server'. The UDP protocol with the 'server' is fixed, so connected sockets are not an option.

A UDP listener is to be started when a user logs on and stopped when there are no more users. My question is, apparently with only ReceiveAsync available in Core (BeginReceive/EndReceive having disappeared), how to kill a receiving loop.

So far as I can see, a call to ReceiveAsync only returns when a datagram is received. Thus, short of the site sending itself a terminating datagram, I can't see how to interrupt the process.

Any suggestions, please?

TylerH
  • 20,799
  • 66
  • 75
  • 101
David
  • 657
  • 5
  • 13
  • Maybe this can help you: http://stackoverflow.com/questions/19404199/how-to-to-make-udpclient-receiveasync-cancelable – Akos Nagy May 05 '17 at 14:43
  • 2
    `socket.Dispose()`, when you close the socket the ReceiveAsync will execute the callback, `e.BytesTransferred` will be 0 and `e.SocketError` will not be `SocketError.Success` – Gusman May 05 '17 at 14:43
  • I don't understand what you're asking. Even when you were using `BeginReceive()`/`EndReceive()`, you had a "loop". It just wasn't embedded in a single method. And you still had the problem of how to interrupt that loop. And the answer then was still that you had to close the socket, which would cause the receive operation to complete with an exception (i.e. when you called `EndReceive()`, that would throw the exception). The `ReceiveAsync()` is exactly the same; you just get to put all the code into a single method now. What is it that's not working for you? – Peter Duniho May 05 '17 at 15:51
  • 2
    Peter, surely the difference is that BeginReceive() returns 'immediately' and there is the opportunity to call EndReceive() either in a loop that checks for cancellation or in the callback. ReceiveAsync() blocks that execution path until a datagram is received, so any cancellation has to be carried out somehow from a separate thread. – David May 07 '17 at 10:57

0 Answers0