简体   繁体   English

udpclient.receive()突然停止接收

[英]udpclient.receive() suddenly stops receiving


I'm using UdpClient to receive data from a single host(actually it's a microcontroller that sends 32 bytes of data every 4 milliseconds. 我正在使用UdpClient从单个主机接收数据(实际上,它是一个微控制器,每4毫秒发送32字节的数据。
The program I wrote is pretty simple. 我编写的程序非常简单。
I'm initializing the UdpClient like this(in Program.cs): 我正在这样初始化UdpClient (在Program.cs中):

public static UdpClient client = new UdpClient(1414);

after that i do this in Form_Load event: 之后,我在Form_Load事件中执行此操作:

static UdpClient client = Program.client; 
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

and then call the client.Recieve() like this: 然后像这样调用client.Recieve()

                Task.Run(() =>
                {
                    while (true)
                    {                       
                        try
                        {
                            data = client.Receive(ref RemoteIpEndPoint);                            
                        }
                        catch (Exception ex)
                        {
                            String err_type = ex.GetType().Name;
                            if (err_type == "SocketException")
                            {                                    
                                MessageBox.Show("Cannot Find The Device.", "Device Error.");
                            }
                        }                                                        
                    }
                });

the program runs fine on my own system (using Windows 10). 该程序可以在我自己的系统上正常运行(使用Windows 10)。 However when i run this program on windows 7,at random times,but with 100% chance client.Recieve() stops working and the program no longer receives any data. 但是,当我在Windows 7上随机运行此程序时,但client.Recieve()停止运行的机会为100%,该程序不再接收任何数据。 no exception is thrown. 没有异常被抛出。 to find the root of the problem, I installed Wireshark to test if there is any incoming data.The answer was no(the LAN port light stops blinking too) . 为了找到问题的根源,我安装了Wireshark来测试是否有任何传入的数据。答案是否定的(LAN端口指示灯也停止闪烁)。 What has me confused is that this does not happen on windows 10. 我感到困惑的是,这不会在Windows 10上发生。

The thing is, you miss all exceptions except SocketException. 事实是,您会错过除SocketException之外的所有异常。 To find out, what's going on, please, rewrite your catch block: 要了解发生了什么,请重写您的catch块:

Task.Run(() =>
                {
                    while (true)
                    {                       
                        try
                        {
                            data = client.Receive(ref RemoteIpEndPoint);                            
                        }
                        catch (SocketException ex)
                        {

                                MessageBox.Show("Cannot Find The Device.", "Device Error.");

                        }  
                        catch (Exception e)
                        {
                                MessageBox.Show(e.GetType().Name, e.Message);
                        }

                    }
                });

Turns out my code was completely fine. 原来我的代码是完全可以的。
This was a hardware problem on our side. 这是我们这方面的硬件问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM