问题
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?
回答1:
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.
回答2:
If you are running this on Windows Vista or beyond, it is probably the UAC
. It can quietly prevent sockets from working properly. If you turn the UAC
level down, it won't just block the socket.
来源:https://stackoverflow.com/questions/24363729/c-sharp-application-not-receiving-packets-on-udpclient-receive