[英]C# asynchronous receive causes system-wide network crash!
I have a very weird bug that I know very little about here, so please bear with me. 我有一个非常奇怪的错误,我对此不太了解,所以请多多包涵。
I have a socket, connecting to a piece of hardware. 我有一个连接到硬件的插座。 I send synchronously, but receive asynchronously.
我同步发送,但异步接收。
I send data via the following code: 我通过以下代码发送数据:
_dicSockets[lclIPEndPoint].Send(lclBytesSent, lclBytesSent.Length, SocketFlags.None);
and receive via the following code: 并通过以下代码接收:
protected virtual void receiveCallback(IAsyncResult ar)
{
PFDoReceiveDataArgs receiveData = (PFDoReceiveDataArgs)ar.AsyncState;
Socket lclSocket = receiveData.MySocket;
etc...
This scheme works fine when the hardware responds quickly, within 10seconds. 当硬件在10秒内快速响应时,此方案可以正常工作。 However, crazy problems happen when the hardware takes longer (about 5 minutes) to respond.
但是,当硬件花费更长的时间(大约5分钟)来响应时,就会发生疯狂的问题。 During this time, there is a thread which is in a while loop, checking whether the command received a response yet or not.
在这段时间内,有一个线程处于while循环中,以检查命令是否已收到响应。
I know that the hardware is working and functional during this time. 我知道这段时间内硬件可以正常工作。 I send the command, and 5 minutes later, the response arrives.
我发送命令,五分钟后,响应到达。 4 out of 5 times, there is no problem, everything works as expected.
5次中有4次没有问题,一切正常。 Same command, same piece of hardware, same everything...
相同的命令,相同的硬件,相同的一切...
The 1 out of 5 times, there's a system-wide network crash where my internet connection is lost! 5次中有1次发生系统范围的网络崩溃,我的互联网连接丢失了!
Since I have no idea what the problem could be, I'll wait for some ideas from you and paste more code as necessary. 由于我不知道问题可能出在哪里,因此我将等待您的想法,并根据需要粘贴更多代码。
可能与问题不直接相关,但是我还将在控件离开receiveCallback方法上下文之前处理套接字实例。
When in doubt, sniff the network out. 如有疑问,请嗅探网络。 I had a problem years ago where a computer on the network was broadcasting malformed packets and would periodically take down our main Cisco switch.
几年前,我遇到了一个问题,网络上的一台计算机正在广播格式错误的数据包,并且会定期关闭我们的主要Cisco交换机。 The mystery piece of hardware you're talking to might be broadcasting some horrible traffic the 1 out of 5 times.
您正在谈论的神秘硬件可能正在广播一些可怕的流量,五分之一。
I found the problem by sniffing the network while things were going wrong and was able to narrow down the problem. 我通过在出现问题的情况下嗅探网络来发现问题,并能够缩小问题的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.