简体   繁体   English

C ++ UDP套接字数据包排队

[英]C++ UDP sockets packet queuing

I am using the same UDP socket for sending and receiving data. 我使用相同的UDP套接字来发送和接收数据。 I am wondering if packet queuing for DGRAM sockets is already present, or do we have to handle it separately. 我想知道DGRAM套接字的数据包排队是否已经存在,或者我们是否必须单独处理它。

If the user code has to handle queueing, how is it done? 如果用户代码必须处理排队,它是如何完成的? Do we have separate threads to recvfrom for the socket and put the packet in the reciver_queue and to sendto from another sending_queue? 我们是否有针对套接字的recvfrom的单独线程,并将数据包放入reciver_queue并从另一个sending_queue发送到?

An example code will be absolutely awesome. 示例代码绝对令人敬畏。 Thanks for your help. 谢谢你的帮助。

There is a packet queue. 有一个数据包队列。 However when the packet queue is filled then UDP packets start getting discarded. 但是,当数据包队列被填满时,UDP数据包开始被丢弃。 When they are discarded they are lost forever so make sure you keep reading data! 当它们被丢弃时,它们将永远丢失,因此请确保您继续阅读数据!

As Goz has noted, there is a packet queue. 正如Goz所指出的那样,有一个数据包队列。 There is more than one, actually, at various places of the whole pipeline that ends in your application. 实际上,在您的应用程序中以整个管道的不同位置存在多个。 There are usually some buffers on the NIC, then there are some managed by the kernel. NIC上通常有一些缓冲区,然后有一些由内核管理。 The kernel buffers often can be sized for individual sockets using setsockopt(). 通常可以使用setsockopt()为各个套接字调整内核缓冲区的大小。

As Goz has already noted, UDP packets can be lost on their way to you, or they can arive in different order. 正如Goz已经注意到的那样,UDP数据包可能在丢失给你的途中丢失,或者它们可以以不同的顺序运行。 If you need both realiability and ordering and if you cannot use TCP instead, you will have to implement some kind of protocol that will provide both atop UDP, eg sliding window protocol . 如果你需要可靠性和排序,如果你不能使用TCP,你将不得不实现某种协议,它将在UDP上提供,例如滑动窗口协议

With UDP there's actually only the receive socket buffer . 使用UDP,实际上只有接收套接字缓冲区 While there is SO_SNDBUF socket option, the value supplied is just the upper limit for the datagram size. 虽然有SO_SNDBUF套接字选项,但提供的值只是数据报大小的上限。 The outbound datagram is either given to the hardware in whole, or in fragments (if it's bigger then the MTU), or discarded. 出站数据报要么全部提供给硬件,要么分段(如果它比MTU大),或者丢弃。 The hardware usually have some ring buffers, but that really has to do with DMA and of no concern to userland apps. 硬件通常有一些环形缓冲区,但这实际上与DMA有关,而与用户态应用无关。

The most straightforward technique for packet queueing in the application is, again, a circular buffer - make it large enough for normal usage, lose some packets during heavy spikes. 应用程序中最直接的数据包排队技术同样是循环缓冲区 - 使其足够大,可以正常使用,在重峰期间丢失一些数据包。 Surely there are other approaches. 当然还有其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM