WinForm C# 程序不捕获传入的 UDP 数据包

WinForm C# program does not catch incoming UDP packets

我有简单的 MCU 网络,仅支持 ARP、广播和单播 UDP/IP 协议,我在没有 PC 网络的情况下直接连接到 MCU (point-to-point)。

在 C# 中,我有两个 UDP 套接字 - 发送方和侦听方。绑定到端点的侦听器(侦听端口 60001)。

但是我的程序只有在 运行ning Wireshark 时才能运行。如果没有 Wireshark,它只能发送广播数据包,但不能接收。

MCU实现了ARP协议(我也在Windows中尝试了静态IP。命令arp -s)。我尝试关闭 Windows 10 防火墙和防病毒软件,以管理员身份关闭 运行 程序,什么都没有。仅当我 运行ning Wireshark 时,我的 C# 程序才会接收数据包。

IP header 校验和正确 - 我在 Wireshark 中启用了检查。 Udp checksum = 0(PC也不计算校验和)

C#代码:

public void UdpConnect() {
    udpSender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

    udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    udpListener.Bind(new IPEndPoint(IPAddress.Any, 60001));
    // MCU send data to dstIP = PC_IP and dstPort = 60001
}

int Send() {
    byte[] dgram = tx_buf.ToArray();
    int n = udpSender.SendTo(dgram, SocketFlags.DontRoute, new IPEndPoint(IPAddress.Parse("192.168.0.200"), 60000));
    // PC send data to MCU IP 192.168.0.200 and dstPort = 60000
    Debug.WriteLine("Send " + n + " bytes");
    return n;
}

byte[] Receive(int timeout_ms = 3000) {
    byte[] data = new byte[1518];
    int byteCount = 0;

    Stopwatch sw = new Stopwatch();
    sw.Start();

    do {
        if (udpListener.Available != 0) {
            Debug.WriteLine("Available: " + udpListener.Available);
            byteCount = udpListener.Receive(data, data.Length, SocketFlags.None);
            Debug.WriteLine("Received UDP packet length: " + byteCount);
        }
        else
            Thread.Sleep(100);
    } while (byteCount == 0 && sw.ElapsedMilliseconds < timeout_ms);

    return byteCount == 0 ? null : data.Take(byteCount).ToArray();
}

byte[] SendReceive(int timeout_ms = 3000, int attempts = 3) {
    byte[] result = null;

    for (int i = 0; i < attempts; i++) {
        Send();
        result = Receive(timeout_ms);

        if (result != null)
            break;
        else
            Debug.WriteLine("Attempt " + (i + 1) + " failed");
    }

    if (result == null) {
        Debug.WriteLine("UDP receiver timeout");
        throw new TimeoutException();
    }

    return result;
}

问题出在短的 ARP 和 UDP 数据包中。当我将我的设备连接到本地网络时我明白了(通常以太网控制器会自动添加填充,但不是 XMOS)。

以太网数据包的最小大小为 64 字节(header + 数据 + crc32)。我发送了没有填充的 ARP,我也发送了短广播 UDP。

但我在捕获 GOOSE 数据包和 SharpPcap 库时遇到问题 How I can grant permission for Pcap library in Windows 10 from C#?