C# 已建立的连接被主机中的软件中止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14304658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:22:45  来源:igfitidea点击:

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

c#socketsnetwork-programming

提问by Richard Williams

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.

缓冲区大小设置正确,我们在套接字上使用 KeepAlive 并使用发送和接收超时。

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...

PS:我们支持 DDoS 缓解服务,这可能会限制连接数...

采纳答案by Hans Passant

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.

这是一个样板错误消息,它来自 Windows。底层错误代码是 WSAECONNABORTED。这实际上并不意味着“连接被中止”。您必须对短语的“您的主机”部分小心谨慎。在绝大多数 Windows 应用程序中,确实是桌面应用程序所连接的主机中止了连接。通常是其他地方的服务器。

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 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 canmean 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.

除非您了解线路另一端发生的情况,否则您并不真正知道为什么连接被中止。这当然很难得到。如果您的服务器可以通过 Internet 访问,那么请不要忽视端口扫描程序正在探测您的可能性。或者您的客户,正在寻找游戏作弊。