为什么收不到udp包? (可以在Wireshark中看到)
Why isn't the udp packet received? (Can be seen in Wireshark)
我在 C# 程序中接收给定的 udp 数据包时遇到问题。
问题代码如下:
//create transport
m_conn = new UdpClient(new IPEndPoint(m_interface_ip, 0));
m_conn.DontFragment = true;
m_conn.EnableBroadcast = true;
m_conn.Connect(new IPEndPoint(IPAddress.Parse(destination_ip), m_port));
m_conn.BeginReceive(new AsyncCallback(OnDataReceived), m_conn);
//create packet
//...
//send
m_conn.Send(buffer, length);
OnDataReceived
函数只是一个带断点的空函数。
我已将防火墙和程序禁用到 'allowed list'。
'request'和'response'可以在Wireshark中看到。请参阅随附的 pcap 文件。数据包似乎是有效的。该设备是经过认证的 Profinet 设备(以防万一)。
但我似乎无法将 'response' 吸收到我的 OnDataReceived
函数中。
我是否遗漏了一些基本的东西? udp/ip header 有什么不寻常的地方吗?
我找到了答案。设备似乎正在响应来自不同源端口的请求。这意味着 Windows 驱动程序(和 Wireshark)将消息标记为不相关。 (不从同一个 socket/port 响应是一件相当愚蠢的事情,我猜这是一个错误。)
但是,它是可以修复的。而不是上面的 'unicast' 套接字,必须创建一个服务器套接字。像这样:
//create transport
IPEndPoint local = new IPEndPoint(m_interface_ip, 0); //bind to any free port
m_conn = new UdpClient();
m_conn.Client.Bind(local);
m_conn.EnableBroadcast = true;
m_conn.BeginReceive(new AsyncCallback(OnDataReceived), m_conn);
//create packet...
//...
//send
m_conn.Send(buffer, length, new IPEndPoint(m_destination_ip, m_port));
这会惹恼防火墙,但它似乎有效。
我在 C# 程序中接收给定的 udp 数据包时遇到问题。
问题代码如下:
//create transport
m_conn = new UdpClient(new IPEndPoint(m_interface_ip, 0));
m_conn.DontFragment = true;
m_conn.EnableBroadcast = true;
m_conn.Connect(new IPEndPoint(IPAddress.Parse(destination_ip), m_port));
m_conn.BeginReceive(new AsyncCallback(OnDataReceived), m_conn);
//create packet
//...
//send
m_conn.Send(buffer, length);
OnDataReceived
函数只是一个带断点的空函数。
我已将防火墙和程序禁用到 'allowed list'。
'request'和'response'可以在Wireshark中看到。请参阅随附的 pcap 文件。数据包似乎是有效的。该设备是经过认证的 Profinet 设备(以防万一)。
但我似乎无法将 'response' 吸收到我的 OnDataReceived
函数中。
我是否遗漏了一些基本的东西? udp/ip header 有什么不寻常的地方吗?
我找到了答案。设备似乎正在响应来自不同源端口的请求。这意味着 Windows 驱动程序(和 Wireshark)将消息标记为不相关。 (不从同一个 socket/port 响应是一件相当愚蠢的事情,我猜这是一个错误。)
但是,它是可以修复的。而不是上面的 'unicast' 套接字,必须创建一个服务器套接字。像这样:
//create transport
IPEndPoint local = new IPEndPoint(m_interface_ip, 0); //bind to any free port
m_conn = new UdpClient();
m_conn.Client.Bind(local);
m_conn.EnableBroadcast = true;
m_conn.BeginReceive(new AsyncCallback(OnDataReceived), m_conn);
//create packet...
//...
//send
m_conn.Send(buffer, length, new IPEndPoint(m_destination_ip, m_port));
这会惹恼防火墙,但它似乎有效。