当我们使用recvfrom()
从UDP 套接字读取数据包时,我们无法部分读取它。因为如果我们首先读取 UDP 数据包的一小部分(通过一个小缓冲区),数据包的提醒将被丢弃,如下所示:
All receive operations return only one packet. When the packet is smaller than the passed buffer, only that much data is returned; when it is bigger, the packet is truncated and the MSG_TRUNC flag is set.
但我想知道如果我们只使用 MSG_PEEK
标志检查数据包是否会发生同样的事情。只看UDP报文会不会提示丢包?
最佳答案
Will reminder of packet is dropped if I just peek the UDP message?
什么都不会被丢弃,因为设置了 MSG_PEEK
标志,套接字的传入数据缓冲区的状态没有被修改;整个数据包将保留在套接字的缓冲区中。
当然,只有数据包数据的第一部分会被复制到您太小的目标数据缓冲区中。
关于linux - UDP 套接字和 MSG_PEEK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63859134/