我有一个从远程主机接收 UDP 数据的 C# 应用程序。我发现有时我的套接字什么也没收到,而且我似乎找不到任何关于为什么的线索!Wireshark 告诉我我的数据正在从远程设备正确发送。我似乎不明白为什么有时我能收到罚款,有时却不能。
我没有得到任何例外,但 OnRecieve 永远不会被调用
这是我的代码以防万一:
class cUdpRx
{
private Thread rxThread = null;
private Socket UdpSocket;
private IPEndPoint localEp;
byte[] byData;
//rxbytes event
public delegate void OnRxBytesEventHandler(byte[] rxBuf);
public event OnRxBytesEventHandler OnRxBytesEvent;
/// <summary>
/// Creates the udp socket
/// </summary>
/// <param name="Port"></param>
/// <returns></returns>
public bool CreateSocket(int Port)
{
try
{
byData = new byte[1500]; //create our buffer
UdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
localEp = new IPEndPoint(IPAddress.Any,Port);
UdpSocket.Bind(localEp);
UdpSocket.BeginReceive(byData,0,byData.Length, SocketFlags.None, new AsyncCallback(OnRecieve), UdpSocket);
return true; //seemed to work ok
}
catch
{
Dispose();
return false; //something went wrong, abort
}
}
private void OnRecieve(IAsyncResult iar)
{
byte[] rxData;
int nBytesRec = 0;
Socket socket = (Socket)iar.AsyncState;
try //in case something else has already disposed of the socket
{
nBytesRec = socket.EndReceive(iar);
}
catch
{
Debug.WriteLine("cant access udp rx socket");
}
try
{
if (nBytesRec > 0)
{
rxData = new byte[nBytesRec];
Array.Copy(byData, rxData, nBytesRec); //copy exact data into new array
OnRxBytesEvent(rxData);
}
if(!killThreads)
UdpSocket.BeginReceive(byData, 0, byData.Length, SocketFlags.None, new AsyncCallback(OnRecieve), UdpSocket);
}
catch(SocketException se)
{
Debug.WriteLine(se.ToString());
}
}
任何帮助都将不胜感激,因为它阻止了我继续我的项目。谢谢
更新 似乎使用 IPAdress.any 是问题所在。如果我改变:
localEp = new IPEndPoint(IPAddress.Any,Port);
至
localEp = new IPEndPoint(IPAddress.Parse("192.168.0.33"),Port);
其中 192.168.0.33 是我电脑的 IP 地址,它每次都会接收数据。谁能解释为什么?使用 IPAddress.any 并从无线和有线连接接收是非常有用的。IPAddress.any 相当于 0.0.0.0 并且根据 MSDN,它应该在所有网络接口上接收。