通过 recvfrom (UDP) 接收部分数据包 [英] Receiving a part of packet via recvfrom (UDP)
问题描述
我正在尝试通过 recvfrom 接收数据包的一部分.它实际上是这样工作的:
I'm trying to receive a part of a packet via recvfrom. It actually works like this:
recvfrom(sockfd, serialised_meta, 12, flags, src_addr, addrlen);
recvfrom(sockfd, serialised_buf, BUFLEN, flags, src_addr, addrlen);
数据是这样发送的:
bufd->Serialise(serialised_buf, BUFLEN+12);
sendto(sockfd, serialised_buf, BUFLEN+12, flags, dest_addr, addrlen);
所以想法是先读取一些元数据,然后再决定是否接收其他内容.问题是如果第二个缓冲区(serialized_buf),我在开始时收到 4 个/0"字节.这似乎不是序列化问题,我之前使用过序列化,一切都很酷,同时我一次接收整个数据包(元数据和数据).关于如何修复它的任何想法?
So the idea is to read some meta data first and then decide whether to receive something else. The problem is that I receive 4 '/0' bytes in the beginning if second buffer (serialised_buf). It doesn't seem to be serialisation issue, I used my serialisation before, and everything was cool, while I was receiving the whole packet (meta and data) at once. Any ideas on how it could be fixed?
PS.我知道我可以跳过不必要的字节)但是无论如何,为什么会发生这种情况?
PS. I understand I can just skip unnecessary bytes) But anyway, why it might be happening?
推荐答案
UDP 不是流"协议...一旦您执行初始 recvfrom,数据包的其余部分将被丢弃.第二个 recvfrom 正在等待下一个数据包...
UDP isn't a "stream" protocol... once you do the initial recvfrom, the remainder of the packet is discarded. The second recvfrom is awaiting the next packet...
这篇关于通过 recvfrom (UDP) 接收部分数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!