Why I can't get all UDP packets?

2019-06-28 00:04发布

My program use UdpClient to try to receive 27 responses from 27 hosts. The size of the response is 10KB. My broadband incoming bandwidth is 150KB/s.

The 27 responses are sent from the hosts almost at the same time and for every 10 secs.

However, I can only receive 8 - 17 responses each time. The number of responses that I can receive is quite dynamic but within the range.

Can anyone tell me why? why can't I receive all?

I understand UDP is not reliable. but I tried receiving 5 - 10 responses at the same time, it worked. I guess the network links are not so bad.

The code is very simple. ON the 27 hosts, I just use UdpClient to send 10KB to my machine.

On my machine, I have one UdpClient receive datagrams. Each time I get a data, I create a thread to handle it (basically handling it means just print out "I received 10KB", but it runs in a thread).

listener = new UDPListener(Port);
listener.Start();
while (true) {
    try {
        UDPContext context = listener.Accept();
        ThreadPool.QueueUserWorkItem(new WaitCallback(HandleMessage), context);

    } catch (Exception) { }
}

If I reduce the size of the response down to 3KB, the case gets much better that roughly 25 responses can be received.

Any more idea? UDP buffer problems???

标签: c# sockets udp
3条回答
萌系小妹纸
2楼-- · 2019-06-28 00:38

As you said yourself, UDP is not reliable. So chances are packets are dropped somewhere.

Note that packet drop is caused just as much by overloaded switches/routers/network cards as by bad links. If someone sends you 27 10Kb responses "simultaneously". it might very well be that the buffers of your network card, or a nearby switch are full, and packets get dropped.

Until you have some code to show, there's probably not much else to say.

查看更多
Ridiculous、
3楼-- · 2019-06-28 00:49

I think that UDP is not reliable at all, so i think this trouble is because you are getting a bottleneck (how tipically it is call) UDP sends everything but disordered and without check so i think you must create kind of this protocol using UDP, i tell this cause i already made it the key is trying to send all the packages with an ID. this way the receiver knows wich packages are missing and could ask for them to the transmitter, like TCP normally does

查看更多
家丑人穷心不美
4楼-- · 2019-06-28 00:56

The 10kb packets are probably being fragmented. If even one of the fragments is dropped, the packet can't be reassembled. Depending on your network, the 3kb packets may not be fragmented, but in any case they would be fragmented less, increasing the chances that they make it through. You could run a PMTU discovery tool to find out the largest packet size the links support.

查看更多
登录 后发表回答