0

My goal is to create UDP listener that handles all datagrams incoming to specified port, regardless of its origin. Then it responds to that origin with some message.

public void StartListening(int port = 13000)
{
    ListenerPort = port;
    udpClient = new UdpClient(ListenerPort);

    udpClient.BeginReceive(new AsyncCallback(handleIncomingMessages), null);
}

private void handleIncomingMessages(IAsyncResult ar)
{
    var receivedData = udpClient.EndReceive(ar, ref senderIpEndPoint);
    //[...]
    udpClient.BeginReceive(new AsyncCallback(handleIncomingMessages), null);
}

Everything worked fine until I turned off one of those "origins". After a few seconds my listener caught SocketException.

An existing connection was forcibly closed by the remote host.

I always thought UDP is a connectionless protocol, yet it seems UdpClient tracks somehow that remote host is available. Is there some standard, .NET Core way I could disable this?

EDIT:

Here is error stack trace:

System.Net.Sockets.SocketException
  HResult=0x80004005
  Message=An existing connection was forcibly closed by the remote host.
  Source=System.Net.Sockets
  StackTrace:
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.EndReceiveFrom(IAsyncResult asyncResult, EndPoint& endPoint)
   at System.Net.Sockets.UdpClient.EndReceive(IAsyncResult asyncResult, IPEndPoint& remoteEP)
   at NetworkController.UDP.NetworkManager.handleIncomingMessages(IAsyncResult ar) in [...]
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.ContextAwareResult.CompleteCallback()
   at System.Net.ContextAwareResult.<>c.<Complete>b__15_0(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

Piotrek
  • 10,919
  • 18
  • 73
  • 136
  • 2
    That error message is translated from Windows API error code, so it is generic for both TCP and UDP. However, it alone does not mean UDP is connection oriented. – Lex Li Jul 10 '20 at 13:35
  • @LexLi That still leaves the question, where does it come from? It should be completely transparent to the UdpClient if one of the "origins" doesn't live anymore. – Fildor Jul 10 '20 at 13:38
  • Not precisely a duplicate, but also [discussed here](https://stackoverflow.com/q/30749423/4137916). – Jeroen Mostert Jul 10 '20 at 14:02
  • The call stack and .NET Core source code should lead you to the actual cause of the exception. Not much to discuss further without such details. – Lex Li Jul 10 '20 at 14:04
  • @LexLi hi, thank you for reponse. I've included stack trace in my question – Piotrek Jul 10 '20 at 16:44
  • There is an answer on SO suggesting "disabling exception" caused by ICMP messages. https://stackoverflow.com/a/39440399/1804027 Unfortunately there is no such thing as SIO_UDP_CONNRESET in .net core – Piotrek Jul 10 '20 at 17:10
  • I've finaly fixed it thanks to this answer https://stackoverflow.com/a/9125485/1804027 – Piotrek Jul 12 '20 at 10:53

0 Answers0