C# An established connection was aborted by the software in your host machine

Richard Williams picture Richard Williams · Jan 13, 2013 · Viewed 120.2k times · Source

These errors are getting more and more frequent on my Game Server. They are causing the server to keep closing and restarting...

System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine 
   at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state) 
   at iRP.Game.Sessions.Session.SendData(Byte[] Data)

This is the code from which these errors are generated:

public void SendData(byte[] Data)
{
    try
    {
        if (mSocket == null)
        {
            //Output.WriteLine("[SND] Socket has a null exception, which means it is now invalid. Remove this socket!", OutputLevel.CriticalError);
        }
        else
        {
            mSocket.BeginSend(Data, 0, Data.Length, SocketFlags.None, sendCallback, mSocket);
        }
    }
    catch (Exception e)
    {
        string WhatToWrite = "Error handled (SESSION): " + e.ToString() + "\n\n" + e.Message + "\n\nStack: " + e.StackTrace + Environment.NewLine + "\n\n";
        File.AppendAllText(Environment.CurrentDirectory + "\\data\\fatal.txt", WhatToWrite);
        Program.Stop();
    }
}

The buffer sizes are correctly set, we are using KeepAlive on the socket and were using Send and Receive Timeouts.

People suggested that disabling the firewall would help, but whenever I do this our Game Server (Dedicated Server) restarts itself as if it's under attack, so the firewall must remain enabled.

Anyone else got any other solutions for this?

PS: We are behind DDoS Mitigation Services which may be limiting the number of connections...

Answer

Hans Passant picture Hans Passant · Jan 13, 2013

An established connection was aborted by the software in your host machine

That is a boiler-plate error message, it comes out of Windows. The underlying error code is WSAECONNABORTED. Which really doesn't mean more than "connection was aborted". You have to be a bit careful about the "your host machine" part of the phrase. In the vast majority of Windows application programs, it is indeed the host that the desktop app is connected to that aborted the connection. Usually a server somewhere else.

The roles are reversed however when you implement your own server. Now you need to read the error message as "aborted by the application at the other end of the wire". Which is of course not uncommon when you implement a server, client programs that use your server are not unlikely to abort a connection for whatever reason. It can mean that a fire-wall or a proxy terminated the connection but that's not very likely since they typically would not allow the connection to be established in the first place.

You don't really know why a connection was aborted unless you have insight what is going on at the other end of the wire. That's of course hard to come by. If your server is reachable through the Internet then don't discount the possibility that you are being probed by a port scanner. Or your customers, looking for a game cheat.