【发布时间】:2014-06-23 10:32:47
【问题描述】:
我遇到了一个我似乎无法调试的奇怪问题。我的应用程序从通过特定端口发送 UDP 数据包的设备接收数据包。设置好 UDP 侦听器后,while 循环会定期触发 Receive 命令。
我应该在每个给定的时间间隔收到 400 个值,我什至设置了一个流程来确保这些值通过。下面是相关代码的sn-p:
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 中没有给出错误,但代码只是停止,因为没有收到数据。
顺便说一句,两台机器是相同的(运行 64 位 Windows 7 Ultimate N 的 Mac Mini)。
我什至在主程序中包含了一个 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();
}
这可能是 Windows 中的应用程序权限问题吗?关于查明问题的最佳方法的任何想法?
【问题讨论】:
-
可能是防火墙问题,您检查了吗?
-
防火墙实际上似乎阻止了 UDP 连接,但没有阻止与 Internet 的连接。忽略它感到愚蠢。谢谢@oleksii!