C#应用程序未在UDPClient.Receive上接收数据包 [英] C# Application not receiving packets on UDPClient.Receive

查看:77
本文介绍了C#应用程序未在UDPClient.Receive上接收数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个我似乎无法调试的奇怪问题.我的应用程序收到了来自通过特定端口发送UDP数据包的设备的数据包.设置UDP侦听器后,while循环会定期触发Receive命令.

I've come across a curious issue I can't seem to debug. My application received packets from a device sending UDP packets over a specific port. After setting up the UDP listener, a while loop triggers the Receive command periodically.

我应该在每个给定的时间间隔接收400个值,并且我甚至设置了一个过程来确保这些值通过.以下是相关代码的片段:

I should be receiving 400 values at every given time interval and I've even set a process to make sure these values are coming through. Below is a snippet of the relevant code:

public UdpClient listener;

IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort); 
//where listenPort is an int holding the port values should be received from

listener.ExclusiveAddressUse = false;
listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Client.Bind(groupEP);

if (listener.Client.Connected)
{
     listener.Connect(groupEP);
}
//I've actually never seen the app actually enter the code contained above

try
{
    while (!done)
    {
        if (isListenerClosed == false && currentDevice.isConnected)
        {
             try
             {
                  receive_byte_array = listener.Receive(ref groupEP); 
             }
             catch (Exception ex)
             {
                  throw ex;
             }
        }
    }
}
catch (SocketException ex)
{
    throw ex;
}

奇怪的是,该应用程序在我的PC上运行得很好(通过安装文件/Installshield以及在Visual Studio中运行),但是在同事的计算机上运行安装文件时却不会收到任何数据(它可以运行在他的Visual Studio环境中就可以了).我还尝试将Visual Studio附加到应用程序的进程中,在该进程中,我发现代码可以正常运行,直到到达 listener.Receive .没有异常被捕获,VS中没有错误,但是由于没有接收到数据,因此代码只是停止了.

The odd thing is that the application runs just fine on my PC (both through setup file/Installshield and when run in Visual Studio) but won't receive any data when running off the setup file on a colleague's computer (it runs just fine in his Visual Studio environment). I've also tried attaching Visual Studio to the app's process, where I found that the code runs fine until it reaches listener.Receive. No exceptions are caught, no errors given in VS, but the code simply stops since no data is received.

顺便说一句,两台机器都是相同的(运行64位Windows 7 Ultimate N的Mac Mini).

Incidentally, both machines are identical (Mac Minis running 64-bit Windows 7 Ultimate N).

我甚至在主程序中包括一个UnhandledExceptionHandler,如下所示:

I've even included an UnhandledExceptionHandler in the Main Program as follows:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    MessageBox.Show("Unhandled Exception Caught " + e.ToString());
    throw new NotImplementedException();
}

这可能是Windows中的应用程序"权限问题吗?关于找出问题的最佳方法的任何想法吗?

Could this be an issue with Application permissions in Windows? Any ideas on the best approach to pinpointing the issue?

推荐答案

UDP是无连接协议.不要 Connect .相反,您只是传递数据包.另外,当您使用 UdpClient 时,请不要深入研究底层套接字.没有意义.

UDP is a connection-less protocol. Don't Connect. Instead, you're simply passing packets of data. Also, when you're using UdpClient, don't dig down to the underlying socket. There's no point.

最简单(也很愚蠢)的UDP侦听器如下所示:

The simplest (and quite stupid) UDP listener would look something like this:

var listener = new UdpClient(54323, AddressFamily.InterNetwork);

var ep = default(IPEndPoint);

while (!done)
{
    var data = listener.Receive(ref ep);

    // Process the data
}

处理 ExclusiveAddressUse (和 SocketOptionName.ReuseAddress )周围的所有内容仅能为您隐藏问题.除非您使用广播或多播,否则该端口上只有一个UDP侦听器将收到该消息.通常这是一件坏事.

Doing all the stuff around ExclusiveAddressUse (and SocketOptionName.ReuseAddress) only serves to hide problems from you. Unless you're using broadcast or multi-cast, only one of the UDP listeners on that port will get the message. That's usually a bad thing.

如果此简单代码不起作用,请检查管道.防火墙,IP地址,驱动程序等.安装WireShark并检查UDP数据包是否确实通过-这可能是设备的故障,可能是错误的配置.

If this simple code doesn't work, check the piping. Firewalls, IP addresses, drivers, the like. Install WireShark and check that the UDP packets are actually coming through - it might be the device's fault, it might be wrong configuration.

此外,理想情况下,您希望异步执行所有这些操作.如果您拥有.NET 4.5,这实际上很容易.

Also, ideally you'd want to do all this asynchronously. If you've got .NET 4.5, this is actually quite easy.

这篇关于C#应用程序未在UDPClient.Receive上接收数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆