问题描述
美好的一天,
我正在用 VC++ 开发一个应用程序,它使用 UDP 协议与 Windows XP 上的 winsock 进行通信.以前我可以假设该工具接收到的所有数据包都来自一个目标.但是我现在正在做广播接收.侦听线程的开销最小,应该将所有时间都花在下面的行上:
I'm developing an application in VC++ that communicates using UDP protocol with winsock on Windows XP. Previously I've been able to assume that all packets being received by the tool were from a single target. However I'm now doing a broadcast receive. The listening thread has minimal overhead and should spend all of its time on the below line:
rv = recvfrom(socket,
p_buffer_p,
p_size,
0,
(sockaddr*)&clientService_in, //This is set to do a broadcast recv
&SenderAddrSize);
我的问题是我是否可以假设我从 recvfrom 的单个返回中获得的缓冲区来自单个目标.也就是说,发送应用程序中发送的 1 次调用是否等于接收应用程序中从 recvfrom 中返回的 1 次?还是可以将来自多个发件人的多个发送合并为 1 个?
My question is whether or not I can assume that a buffer that I get from a single return from recvfrom is from a single target. That is, does 1 call to send in the sending app equal 1 return from recvfrom in the receiving app? Or can multiple sends from multiple senders get mushed together into 1?
我假设来自目标的单个发送不能拆分为来自 recvfrom 的多个返回.我一直都这么认为,反正从来没有遇到过问题.
I'm assuming that a single send from a target can't get split up into multiple returns from recvfrom. I've always assumed this and never had a problem anyway.
还有一点,它是一种 SOCK_DGRAM 类型的套接字.
One more thing, its a SOCK_DGRAM type of socket.
推荐答案
不,UDP 消息不能拆分.他们在发送时到达.此外,多个 UDP 消息不会连接.
No, a UDP message cannot be split. They arrive as they were sent. Also, multiple UDP messages are NOT concatenated.
所以 N 个 sendto 消息对应于 N 个 recvfrom 调用.
So N sendto messages correspond to N recvfrom calls.
引用自 wiki:
数据报——数据包被单独发送并被检查只有当他们到达时才能保持完整性.数据包有明确的边界收到后兑现,这意味着接收器的读取操作socket 将产生一整条消息,就像它最初发送的那样.
Datagrams – Packets are sent individually and are checked for integrity only if they arrive. Packets have definite boundaries which are honored upon receipt, meaning a read operation at the receiver socket will yield an entire message as it was originally sent.
这篇关于来自多个发件人的 UDP recv/recv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!