远程主机强制关闭现有连接

时间:2022-04-10 15:13:21

I am working with a commercial application which is throwing a SocketException with the message,

我正在开发一个商业应用程序,它会对消息进行SocketException,

An existing connection was forcibly closed by the remote host

远程主机强制关闭现有连接

This happens with a socket connection between client and server. The connection is alive and well, and heaps of data is being transferred, but it then becomes disconnected out of nowhere.

这发生在客户端和服务器之间的套接字连接上。连接仍然存在,大量的数据正在传输,但是它却被断开了。

Has anybody seen this before? What could the causes be? I can kind of guess a few causes, but also is there any way to add more into this code to work out what the cause could be?

有人见过这个吗?原因是什么?我能猜出一些原因,但是有没有办法在代码中添加更多的东西来找出原因是什么?

Any comments / ideas are welcome.

欢迎提出任何意见。

... The latest ...

…最新的…

I have some logging from some .NET tracing,

我有一些。net追踪的日志记录,

System.Net.Sockets Verbose: 0 : [8188] Socket#30180123::Send() DateTime=2010-04-07T20:49:48.6317500Z

System.Net.Sockets Error: 0 : [8188] Exception in the Socket#30180123::Send - An existing connection was forcibly closed by the remote host DateTime=2010-04-07T20:49:48.6317500Z 

System.Net.Sockets Verbose: 0 : [8188] Exiting Socket#30180123::Send() -> 0#0

Based on other parts of the logging I have seen the fact that it says '0#0' means a packet of 0 bytes length is being sent. But what does that really mean?

基于日志的其他部分,我已经看到了它说“0#0”意味着要发送一个0字节的数据包。但这到底意味着什么呢?

One of two possibilities is occuring, and I am not sure which,

两种可能性之一正在发生,我不确定是哪一种,

1) The connection is being closed, but data is then being written to the socket, thus creating the exception above. The 0#0 simply means that nothing was sent because the socket was already closed.

1)正在关闭连接,但随后将数据写入套接字,从而创建上面的异常。0#0仅仅意味着没有发送任何内容,因为套接字已经关闭。

2) The connection is still open, and a packet of zero bytes is being sent (i.e. the code has a bug) and the 0#0 means that a packet of zero bytes is trying to be sent.

2)连接仍然是打开的,正在发送一个0字节的数据包(即代码有错误),而0#0表示一个0字节的数据包正在尝试发送。

What do you reckon? It might be inconclusive I guess, but perhaps someone else has seen this kind of thing?

你认为什么?我猜这可能是不确定的,但也许有人见过这种事?

9 个解决方案

#1


57  

This generally means that the remote side closed the connection (usually by sending a TCP/IP RST packet). If you're working with a third-party application, the likely causes are:

这通常意味着远程端关闭连接(通常通过发送TCP/IP RST包)。如果您使用的是第三方应用程序,可能的原因是:

  • You are sending malformed data to the application
  • 您正在向应用程序发送畸形数据
  • The network link between the client and server is going down for some reason
  • 由于某种原因,客户端和服务器之间的网络连接正在下降
  • You have triggered a bug in the third-party application that caused it to crash
  • 您在第三方应用程序中触发了一个错误,导致它崩溃
  • The third-party application has exhausted system resources
  • 第三方应用程序已经耗尽了系统资源

It's likely that the first case is what's happening.

第一种情况很可能就是正在发生的。

You can fire up Wireshark to see exactly what is happening on the wire to narrow down the problem.

你可以启动线鲨,看看到底发生了什么,从而缩小问题的范围。

Without more specific information, it's unlikely that anyone here can really help you much.

如果没有更具体的信息,这里的任何人都不可能真正帮助你。

#2


23  

This is not a bug in your code. It is coming from .Net's Socket implementation. If you use the overloaded implementation of EndReceive as below you will not get this exception.

这不是代码中的bug。它来自。net的Socket实现。如果像下面这样使用EndReceive的重载实现,则不会出现这种异常。

    SocketError errorCode;
    int nBytesRec = socket.EndReceive(ar, out errorCode);
    if (errorCode != SocketError.Success)
    {
        nBytesRec = 0;
    }

#3


7  

Simple solution for this common annoying issue:

解决这个常见问题的简单方法:

Just go to your ".context.cs" file (located under ".context.tt" which located under your "*.edmx" file).

进入你的。context。cs"文件(位于".context "之下。tt“位于你的”*。”edmx文件)。

Then, add this line to your constructor:

然后,将这一行添加到您的构造函数中:

public DBEntities() 
        : base("name=DBEntities") 
    { 
        this.Configuration.ProxyCreationEnabled = false; // ADD THIS LINE !
    }

hope this is helpful.

希望这是有帮助的。

#4


6  

Had the same bug. Actually worked in case the traffic was sent using some proxy (fiddler in my case). Updated .NET framework from 4.5.2 to >=4.6 and now everything works fine. The actual request was:
new WebClient().DownloadData("URL");
The exception was:

有同样的错误。实际上,在使用代理(在我的例子中是fiddler)发送流量时是有效的。将。net框架从4.5.2更新到>=4.6,现在一切正常。实际的请求是:new WebClient().DownloadData(“URL”);例外的是:

SocketException: An existing connection was forcibly closed by the remote host

SocketException:远程主机强制关闭现有连接

#5


2  

Using TLS 1.2 solved this error.
You can force your application using TLS 1.2 with this (make sure to execute it before calling your service):

使用TLS 1.2解决了这个错误。您可以使用TLS 1.2来强制您的应用程序(确保在调用您的服务之前执行它):

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 

Another solution :
Enable strong cryptography in your local machine or server in order to use TLS1.2 because by default it is disabled so only TLS1.0 is used.
To enable strong cryptography , execute these commande in PowerShell with admin privileges :

另一种解决方案:在本地机器或服务器中启用强密码学,以便使用TLS1.2,因为默认情况下它是禁用的,所以只使用TLS1.0。要启用强密码学,请在PowerShell中使用管理特权执行这些命令:

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord 

You need to reboot your computer for these changes to take effect.

您需要重新启动计算机以使这些更改生效。

#6


0  

I've got this exception because of circular reference in entity.In entity that look like

我之所以有这个例外是因为实体中的循环引用。在实体中

public class Catalog
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public Catalog Parent { get; set; }
    public ICollection<Catalog> ChildCatalogs { get; set; }
}

I added [IgnoreDataMemberAttribute] to the Parent property. And that solved the problem.

我向父属性添加了[IgnoreDataMemberAttribute]。这就解决了问题。

#7


0  

I got this exception when I was trying to read a row from the database that had a null in an enum column, it couldn't map the null into an enum value.

当我试图从数据库中读取在enum列中为null的行时,它无法将null映射到enum值。

#8


0  

I met this Exception ,when DateTime property of the class not getting the value .I Simply make it nullable DateTime and Found the solution .

当类的DateTime属性没有得到值时,我遇到了这个异常,我简单地将它设为nullable DateTime并找到了解决方案。

public class PlanningBoardBO
    {
        ... Other Properties ...

        public DateTime? PickupDate { get; set; }

        ... Here changed DateTime to DateTime?
    }

#9


0  

This error occurred in my application with the CIP-protocol whenever I didn't Send or received data in less than 10s.

当我在10秒内没有发送或接收数据时,cip协议的应用程序就会出现这个错误。

This was caused by the use of the forward open method. You can avoid this by working with an other method, or to install an update rate of less the 10s that maintain your forward-open-connection.

这是使用正向开放方法造成的。您可以通过使用其他方法来避免这种情况,或者安装一个更新速率小于10的更新速率来维护向前打开的连接。

#1


57  

This generally means that the remote side closed the connection (usually by sending a TCP/IP RST packet). If you're working with a third-party application, the likely causes are:

这通常意味着远程端关闭连接(通常通过发送TCP/IP RST包)。如果您使用的是第三方应用程序,可能的原因是:

  • You are sending malformed data to the application
  • 您正在向应用程序发送畸形数据
  • The network link between the client and server is going down for some reason
  • 由于某种原因,客户端和服务器之间的网络连接正在下降
  • You have triggered a bug in the third-party application that caused it to crash
  • 您在第三方应用程序中触发了一个错误,导致它崩溃
  • The third-party application has exhausted system resources
  • 第三方应用程序已经耗尽了系统资源

It's likely that the first case is what's happening.

第一种情况很可能就是正在发生的。

You can fire up Wireshark to see exactly what is happening on the wire to narrow down the problem.

你可以启动线鲨,看看到底发生了什么,从而缩小问题的范围。

Without more specific information, it's unlikely that anyone here can really help you much.

如果没有更具体的信息,这里的任何人都不可能真正帮助你。

#2


23  

This is not a bug in your code. It is coming from .Net's Socket implementation. If you use the overloaded implementation of EndReceive as below you will not get this exception.

这不是代码中的bug。它来自。net的Socket实现。如果像下面这样使用EndReceive的重载实现,则不会出现这种异常。

    SocketError errorCode;
    int nBytesRec = socket.EndReceive(ar, out errorCode);
    if (errorCode != SocketError.Success)
    {
        nBytesRec = 0;
    }

#3


7  

Simple solution for this common annoying issue:

解决这个常见问题的简单方法:

Just go to your ".context.cs" file (located under ".context.tt" which located under your "*.edmx" file).

进入你的。context。cs"文件(位于".context "之下。tt“位于你的”*。”edmx文件)。

Then, add this line to your constructor:

然后,将这一行添加到您的构造函数中:

public DBEntities() 
        : base("name=DBEntities") 
    { 
        this.Configuration.ProxyCreationEnabled = false; // ADD THIS LINE !
    }

hope this is helpful.

希望这是有帮助的。

#4


6  

Had the same bug. Actually worked in case the traffic was sent using some proxy (fiddler in my case). Updated .NET framework from 4.5.2 to >=4.6 and now everything works fine. The actual request was:
new WebClient().DownloadData("URL");
The exception was:

有同样的错误。实际上,在使用代理(在我的例子中是fiddler)发送流量时是有效的。将。net框架从4.5.2更新到>=4.6,现在一切正常。实际的请求是:new WebClient().DownloadData(“URL”);例外的是:

SocketException: An existing connection was forcibly closed by the remote host

SocketException:远程主机强制关闭现有连接

#5


2  

Using TLS 1.2 solved this error.
You can force your application using TLS 1.2 with this (make sure to execute it before calling your service):

使用TLS 1.2解决了这个错误。您可以使用TLS 1.2来强制您的应用程序(确保在调用您的服务之前执行它):

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 

Another solution :
Enable strong cryptography in your local machine or server in order to use TLS1.2 because by default it is disabled so only TLS1.0 is used.
To enable strong cryptography , execute these commande in PowerShell with admin privileges :

另一种解决方案:在本地机器或服务器中启用强密码学,以便使用TLS1.2,因为默认情况下它是禁用的,所以只使用TLS1.0。要启用强密码学,请在PowerShell中使用管理特权执行这些命令:

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord 

You need to reboot your computer for these changes to take effect.

您需要重新启动计算机以使这些更改生效。

#6


0  

I've got this exception because of circular reference in entity.In entity that look like

我之所以有这个例外是因为实体中的循环引用。在实体中

public class Catalog
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public Catalog Parent { get; set; }
    public ICollection<Catalog> ChildCatalogs { get; set; }
}

I added [IgnoreDataMemberAttribute] to the Parent property. And that solved the problem.

我向父属性添加了[IgnoreDataMemberAttribute]。这就解决了问题。

#7


0  

I got this exception when I was trying to read a row from the database that had a null in an enum column, it couldn't map the null into an enum value.

当我试图从数据库中读取在enum列中为null的行时,它无法将null映射到enum值。

#8


0  

I met this Exception ,when DateTime property of the class not getting the value .I Simply make it nullable DateTime and Found the solution .

当类的DateTime属性没有得到值时,我遇到了这个异常,我简单地将它设为nullable DateTime并找到了解决方案。

public class PlanningBoardBO
    {
        ... Other Properties ...

        public DateTime? PickupDate { get; set; }

        ... Here changed DateTime to DateTime?
    }

#9


0  

This error occurred in my application with the CIP-protocol whenever I didn't Send or received data in less than 10s.

当我在10秒内没有发送或接收数据时,cip协议的应用程序就会出现这个错误。

This was caused by the use of the forward open method. You can avoid this by working with an other method, or to install an update rate of less the 10s that maintain your forward-open-connection.

这是使用正向开放方法造成的。您可以通过使用其他方法来避免这种情况,或者安装一个更新速率小于10的更新速率来维护向前打开的连接。