简体   繁体   English

C#应用程序未在UDPClient.Receive上接收数据包

[英]C# Application not receiving packets on UDPClient.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. 我的应用程序收到了来自通过特定端口发送UDP数据包的设备的数据包。 After setting up the UDP listener, a while loop triggers the Receive command periodically. 设置UDP侦听器后,while循环会定期触发Receive命令。

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. 我应该在每个给定的时间间隔接收400个值,并且甚至设置一个过程来确保这些值通过。 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;
}

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). 奇怪的是,该应用程序在我的PC上运行正常(通过安装文件/ Installshield以及在Visual Studio中运行),但是在同事的计算机上运行安装文件时不会接收任何数据(在Windows 2000中运行正常)他的Visual Studio环境)。 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 . 我还尝试将Visual Studio附加到应用程序的进程中,在该进程中,我发现代码可以正常运行,直到到达listener.Receive为止。 No exceptions are caught, no errors given in VS, but the code simply stops since no data is received. 没有异常被捕获,VS中没有错误,但是由于没有接收到数据,所以代码只是停止了。

Incidentally, both machines are identical (Mac Minis running 64-bit Windows 7 Ultimate N). 顺便提一句,两台机器都是相同的(运行64位Windows 7 Ultimate N的Mac Mini)。

I've even included an UnhandledExceptionHandler in the Main Program as follows: 我甚至在主程序中包括一个UnhandledExceptionHandler,如下所示:

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();
}

Could this be an issue with Application permissions in Windows? 这可能是Windows中的“应用程序”权限问题吗? Any ideas on the best approach to pinpointing the issue? 关于找出问题的最佳方法有什么想法?

UDP is a connection-less protocol. UDP是无连接协议。 Don't Connect . 不要Connect Instead, you're simply passing packets of data. 相反,您只是在传递数据包。 Also, when you're using UdpClient , don't dig down to the underlying socket. 另外,当您使用UdpClient ,请不要深入研究基础套接字。 There's no point. 毫无意义。

The simplest (and quite stupid) UDP listener would look something like this: 最简单(也很愚蠢)的UDP侦听器如下所示:

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

var ep = default(IPEndPoint);

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

    // Process the data
}

Doing all the stuff around ExclusiveAddressUse (and SocketOptionName.ReuseAddress ) only serves to hide problems from you. 围绕ExclusiveAddressUse (和SocketOptionName.ReuseAddress )进行所有操作只能为您隐藏问题。 Unless you're using broadcast or multi-cast, only one of the UDP listeners on that port will get the message. 除非您使用广播或多播,否则该端口上只有一个UDP侦听器会收到该消息。 That's usually a bad thing. 这通常是一件坏事。

If this simple code doesn't work, check the piping. 如果此简单代码不起作用,请检查管道。 Firewalls, IP addresses, drivers, the like. 防火墙,IP地址,驱动程序等。 Install WireShark and check that the UDP packets are actually coming through - it might be the device's fault, it might be wrong configuration. 安装WireShark并检查UDP数据包是否确实通过-这可能是设备的故障,可能是错误的配置。

Also, ideally you'd want to do all this asynchronously. 另外,理想情况下,您希望异步执行所有这些操作。 If you've got .NET 4.5, this is actually quite easy. 如果您有.NET 4.5,这实际上很容易。

If you are running this on Windows Vista or beyond , it is probably the UAC . 如果您在Windows Vista或更高版本上运行此程序 ,则可能是UAC It can quietly prevent sockets from working properly. 它可以悄悄地阻止套接字正常工作。 If you turn the UAC level down, it won't just block the socket. 如果将UAC级别调低,它不仅会阻塞套接字。

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

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