Does it make sense to have more than one UDP Datagram socket in standby? Are "simultaneous" packets discarded or queued by the kernel?

I am encoding a network application on Android.

I am thinking of having one UDP port and a Datagram socket, which receives all the datagrams that are sent to it, and then have different processing queues for these messages.

I doubt if I should have a second or third UDP socket in standby mode. Some messages will be very short (100 bytes or so), but others will have to transfer files.

My concern is whether the Android kernel will delete small messages if it is too busy processing larger ones?

Update β€œThe last function calls sock_queue_rcv_skb () (in sock.h), which queues the UDP packet in the socket receive buffer. If there is no more space left in the buffer, the packet will be dropped. Filtering is also performed using this function, which calls sk_filter ( ) is the same as TCP. Finally, data_ready () is called and UDP packets are received. "

+4
source share
3 answers

Let's first describe some basics:

Each socket has a receive and send buffer. When the network equipment signals the arrival of a new packet and the receive buffer is full, the packet is discarded. Buffer sizes are controlled using the SO_RCVBUF and SO_SNDBUF socket options, see setsockopt(3) . The OS sets some default values ​​(and there is the /etc/sysctl.conf file). This is in the BSD system:

  ~ $ sysctl -a | grep space
 net.inet.tcp.recvspace = 16384
 net.inet.tcp.sendspace = 16384
 net.inet.udp.recvspace = 41600
 net.inet.udp.sendspace = 9216

The difference between TCP and UDP is that the former takes care of the data sequence and retransmission of the dropped packets, as well as the flow control (a slow reader slows down the fast script), and the latter does not.

So yes, using UDP to transfer files is not the best, but workable option. You just need to invent a part of TCP and weigh the overhead of re-inventing against one of TCP. In addition, the general wisdom is that UDP is best suited for applications that may suffer some reordering / packet loss (e.g., audio / video streams).

Then there is an erroneous idea that each socket needs a separate stream to send / receive data, which is far from the truth. Many excellent high-performance network applications were written without threads, but using non-blocking sockets and some polling mechanism (see select(2) , poll(2) , epoll(7) ).

To the question itself:

Yes, the kernel can drop packets if the application is too busy to have enough free space in the socket receive buffers. But since each socket has its own, separating control and data flows might help. Personally, although I would go for a simple TCP server setup - listen on the port, accept the connection on one client, implement a meaningful protocol on top of the TCP stream. I agree that playing with UDP and low-level protocol machines is a lot of fun, but it has already been done and decades of research have gone into tuning TCP performance. What matters at the end of the day is the reliability (first) and performance (second) of your application.

Hope this helps.

+7
source

UDP is a bad idea to transfer files, because you cannot guarantee the order in which packets will be received, or they will be received at all. If you plan to create a fail-safe transport layer on top of this, you should just use TCP / IP, namely what it does.

UDP does not buffer or queue received packets. If the packet is accepted and you are expecting data, you will receive it. If a package is received when your program is busy doing some other processing, you will not receive the package at all. Therefore, if you receive two "simultaneous packets" (well, two very close to each other), there is a good chance that you can skip one of them if you perform significant processing of each packet.

I do not see how to open additional ports will help you a lot. If you are busy processing a packet from port 1, you will skip any packets included in any other ports that you are viewing, unless they are running in a dedicated thread. It would be much better for you to copy the packet into your own buffer and pass it to another thread to process it, so that the listener stream can return to listening as soon as possible.

+1
source

TCP flow control will help you reduce dropped packets. Its fault tolerance also ensures that the packet will arrive sequentially.

0
source

Source: https://habr.com/ru/post/1305964/


All Articles