0

Been having an reoccurring issue with my server/client interaction. One in every like 15-20 disconnections (sometimes more, or less, frequent) my server will register a 10054 exception. When this happens, the server seizes up and has to be restarted, which is really annoying to deal with.

The exception always occurs in this code block on the 6th line down (starting "byte[] data ="):

private static void UDPReceiveCallback(IAsyncResult result)
    {
        try
        {
            IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = udpListener.EndReceive(result, ref clientEndPoint);
            udpListener.BeginReceive(UDPReceiveCallback, null);

            if(data.Length<4)
            {
                return;
            }

            using(Packet packet = new Packet(data))
            {
                int clientID = packet.ReadInt();
                if(clientID == 0)
                {
                    return;
                }

                if(clients[clientID].udp.endPoint == null)
                {
                    clients[clientID].udp.Connect(clientEndPoint);
                    return;
                }

                if(clients[clientID].udp.endPoint.ToString() == clientEndPoint.ToString())
                {
                    clients[clientID].udp.HandleData(packet);
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine($"Error receiving UDP data: {ex}");
        }
    }

I've tried a few different things to try and fix it, but most times end up making it worse. Frankly I don't understand what this exception means or why it is caused, which makes bugfixing harder. I'd appreciate any insight into the source of this exception as well as maybe how I could fix it.

Thanks!

Dan
  • 45
  • 1
  • 5
  • "if you send a UDP datagram and the destination is not reachable (because nobody is listening on that port), this should generate an ICMP Port Unreachable error. When this happens, the next call to Receive will cause the 10054 WSAECONNRESET to be returned (and hence the SocketException)." – Mitch Wheat Feb 09 '21 at 05:28
  • If you look at the [Microsoft of socket error codes](https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2) it will tell you that `10054` is `WSAECONNRESET`, which means that the peer (other end of the "connection") no longer have its end of the channel open. – Some programmer dude Feb 09 '21 at 05:29
  • Would it be better then to not close the sockets client side and only close them server side? That way, at least it doesn't cause the server to freak out and need a restart? – Dan Feb 09 '21 at 06:00
  • Many (if not most) socket errors you can get could be seen as fatal, *for the socket* but not the application. The remedy is usually to close the socket and start over with a brand new socket. – Some programmer dude Feb 09 '21 at 06:10
  • Network errors happen all the time. Your code needs to handle them gracefully. Eliminating them isn't an option. UDP is connectionless, so first thing you should do is not call `Connect()` on the UDP socket. Doing so is a convenience if you know you only have a single remote endpoint to deal with, and you don't mind errors like `WSACONNRESET`, but obviously that doesn't apply to you. You will still get the error if trying to deal with a remote endpoint that has in fact become unreachable, but otherwise the socket will remain usable. See duplicate for more info about the error and mitigations. – Peter Duniho Feb 09 '21 at 08:00
  • I finally fixed it- the message was vague and didn't really help to describe the problem. What ultimately tipped me off was another exception regarding a packet failing to read an integer type value, which would occur occasionally but not always together. The source was packet loss, and some important packets were sent via UDP rather than TCP. Swapping the important ones to TCP fixed the issue entirely – Dan Feb 10 '21 at 20:29

0 Answers0