P
P
Papayaved2018-06-21 09:20:30
Windows
Papayaved, 2018-06-21 09:20:30

How to enable UDP data reception in Windows from C#?

Unable to receive both broadcast UDP packets and IP addresses. The device that sends the packets (microcontroller) does not support dynamic address allocation, only ARP. The only thing the program can do is send broadcast packets. In this case, everything starts working if you run Wireshark in parallel, the example code for SharpPcap behaves similarly, as well as the Socket Test program sourceforge.net/projects/sockettest I

use .Net Framework 4.62. Disabled firewall and antivirus. In the program (in the manifest file) I require administrator rights, but this does not help. I also set a static address using the arp -s command, but this did not help either.

I think these are Windows network settings that Wireshark can change. Perhaps Windows receives them from the server and remembers when it is disconnected from the network and connected to the device. The computer is configured to work on the enterprise network, is included in the workgroup. I have not yet encountered network issues, and I do not understand this. Tell me where to dig?

Here is the C# code, but it seems to work:

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;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Papayaved, 2018-06-21
@Papayaved

The problem turned out to be on the microcontroller side, I sent short ARP and UDP packets. The minimum size of an Ethernet packet is 64 bytes and must be appended with a 0 at the end.

A
Alexander Kuznetsov, 2018-06-21
@DarkRaven

Have you looked at this: www.jarloo.com/c-udp-multicasting-tutorial ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question