2

The problem: UDP packets are missing.

How could I capture every single UDP packet that is hitting the port?

I want to put received packet on the queue for preprocessing in the background and continue capturing new UDP packets without a single UDP packet loss?

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

    ....

    Console.WriteLine("Collecting data..");

    while (true)
    {
        CaptureUDPPacket(client, _ep);
    }
}

What is the best way to implement this feature?

Best regards!

Linas
  • 65
  • 1
  • 7
  • 2
    You cant determine if UDP packet reached your target, since there is no control on data transmission in UDP. Use TCP/IP instead. – Bartosz Olchowik Apr 14 '22 at 12:53
  • TCP/IP is not an option for this application at all. – Linas Apr 14 '22 at 12:55
  • 2
    and UDP has no controlling functionality for sending/receiving data. Implement functionality that is already implemented in TCP/IP and use UDP or you can forget it. And probably nobody is gonna help you out with reinventing the wheel. – Bartosz Olchowik Apr 14 '22 at 12:56
  • All I need is a sample how to preprocess every single received packet in the background (whether asynchronously, multithreading) so I would have 0 latency for incoming traffic. Idea is to receive the packet, put it on a task or something and continue receiving new UDP packets whilst some work is happening in the background. Currently what is happening, whilst packet is being preprocessed I have to wait for the entire process to be completed and because of this I miss a lot of new traffic. – Linas Apr 14 '22 at 13:02
  • 1
    Tutorial how to make asynchronous server: https://learn.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example But anyway, UDP messages are not guaranteed to be received in the correct order, or even received at all. Switch to TCP if you want to be sure if everything is received in correct order, and that everything was received. So even if you implement an asynchronous server that i mentioned, you might still (and probably you will) experience data loss. – Bartosz Olchowik Apr 14 '22 at 13:03
  • 1
    _"capturing new UDP packets without a single UDP packet loss?"_ The code to put in on a queue must be faster that the udp packets are sent. (also wifi will cause packetlosses, so don't use wifi) – Jeroen van Langen Apr 14 '22 at 13:53
  • What Jeroen van Langen said, but even that will not _guarantee_ that your program receives every packet. In practice, you can capture practically all of the packets if your code is fast enough, but UDP, by design, sacrifices reliability for simplicity. – Solomon Slow Apr 14 '22 at 16:28
  • You are saying TCP is not an option, but why not? You will have a better luck resolving that limitation. Using UDP is great for lossy stuff (Used to be for audio calls 10 years ago, you lose a packet, not a big deal. A few packets received out of order, no problem). But now adays TCP can do almost everything, except broadcasting if i remember correctly. – Adel Alzubeir Apr 19 '22 at 13:43
  • Although preventing 100% packet loss in UDP is impossible without writing your own retransmission layer (somehow reinventing TCP), you can reduce it significantly, by tuning the network card adapter properties and by capturing the data packets on an unmanaged thread, one that is not susceptible to be frozen by the Garbage Collector (packets must also be placed on a lockless queue, to avoid the chance the GC freezes the managed thread while the queue is locked) . Would you be able to add a C++/CLI library to your project? If that is the case, I can share some code I wrote to capture ... – BlueStrat Apr 20 '22 at 23:29
  • ... multiple MPTS broadcasts (~400Mbps on a 1Gbps link) with minimal packet loss. – BlueStrat Apr 20 '22 at 23:30

0 Answers0