来自多个发件人的 UDP 接收/接收 [英] UDP recv/recvfrom multiple senders

查看:25
本文介绍了来自多个发件人的 UDP 接收/接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我正在 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 接收/接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆