[英]Send and receive UDP multicast packets with the same socket
I could not find a working example for this, so I am going to post a question (and let's see if I can reduce this to an MVP code example).我找不到一个工作示例,所以我将发布一个问题(让我们看看是否可以将其简化为 MVP 代码示例)。 So, I need to do mdns queries, I can use two sockets (one for sending / second for receiving) but so far I have not been able to make it work with a single socket.
所以,我需要做 mdns 查询,我可以使用两个套接字(一个用于发送/第二个用于接收),但到目前为止我还无法使其与单个套接字一起工作。
So the steps that I am performing are:所以我正在执行的步骤是:
Enumerate all the interface addresses on the host.枚举主机上的所有接口地址。 Then for each host:
然后对于每个主机:
So depending on what I do in step 2 either read or write fails:因此,根据我在第 2 步中执行的操作,读取或写入失败:
So which address I need to bind to then?那么我需要绑定到哪个地址呢? Also, since the
IP_ADD_MEMBERSHIP
parameter ip_mreq
has the interface field, do I really need the IP_MULTICAST_IF
?另外,由于
IP_ADD_MEMBERSHIP
参数ip_mreq
有 interface 字段,我真的需要IP_MULTICAST_IF
吗?
If you bind to a specific interface address (on Linux at least), you'll be unable to receive multicast.如果您绑定到特定的接口地址(至少在 Linux 上),您将无法接收多播。 If you bind to a multicast address, you can't send.
如果绑定到多播地址,则无法发送。
What you need to do is bind to INADDR_ANY
, then when you set the IP_ADD_MEMBERSHIP
option, you set the interface address that you want to receive multicast packets on.您需要做的是绑定到
INADDR_ANY
,然后在设置IP_ADD_MEMBERSHIP
选项时,设置要接收多播数据包的接口地址。 If you have multiple interfaces, you can call this for each interface you want to bind to.如果您有多个接口,则可以为要绑定到的每个接口调用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.