我正在使用 UdpClient
从单个主机接收数据(实际上它是一个每4毫秒发送32个字节数据的微控制器 .
我写的程序非常简单 .
我正在初始化 UdpClient
(在Program.cs中):
public static UdpClient client = new UdpClient(1414);
之后,我在Form_Load事件中执行此操作:
static UdpClient client = Program.client;
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
然后像这样调用 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.");
}
}
}
});
程序在我自己的系统上运行正常(使用Windows 10) . 但是,当我在Windows 7上运行此程序时,随机时间,但有100%的机会 client.Recieve()
停止工作,程序不再接收任何数据 . 没有异常被抛出 . 为了找到问题的根源,我安装了Wireshark以测试是否有任何传入数据 . 答案是否定的(LAN端口指示灯也会停止闪烁) . 令我困惑的是,这不会发生在Windows 10上 .
1 回答
问题是,你会错过除SocketException之外的所有异常 . 要了解,发生了什么,请重写您的catch块: