C ++ UDP Packet Packet Queue

I use the same UDP socket to send and receive data. I am wondering if batch queuing for DGRAM sockets is already present, or we should handle it separately.

If the user code should handle the sequence, how is this done? Do we have separate streams for recvfrom for the socket and put the packet in reciver_queue and sendto from another send_queue?

The sample code will be absolutely amazing. Thank you for your help.

+4
source share
3 answers

There is a packet queue. However, when the packet queue is full, UDP packets begin to be discarded. When they are discarded, they are lost forever, so watch out for data reading!

+8
source

As Goz noted, there is a queue of packages. In fact, in different places of the entire pipeline ending with your application, there is more than one. A network adapter usually has several buffers, then some of them are managed by the kernel. Kernel buffers can often be set for individual sockets using setsockopt ().

As Goz already pointed out, UDP packets may be lost on the way to you, or they may appear in a different order. If you need both realism and order, and if you cannot use TCP instead, you will have to implement some kind of protocol that will provide both over UDP, for example. sliding window protocol .

+1
source

In UDP, there is actually only a receive socket buffer. Although the SO_SNDBUF socket option SO_SNDBUF , the value presented is only the upper limit for the size of the datagram. The outgoing datagram is either provided to the hardware as a whole, or to fragments (if it is larger than the MTU) or discarded. The hardware usually has several ring buffers, but it is really DMA related and not related to user applications.

The simplest method of a packet queue in an application is, again, a circular buffer - to make it large enough for normal use, to lose some packets during heavy bursts. Of course, there are other approaches.

+1
source

All Articles