Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

winforms - 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. After setting up the UDP listener, a while loop triggers the Receive command periodically.

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

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.

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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
}

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...