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). 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.
So the steps that I am performing are:
Enumerate all the interface addresses on the host. Then for each host:
- Create a non blocking UDP socket
- bind() to either the interface address:5353 or the multicast group address (either of these cause either read or write to fail)
- Set IP_MULTICAST_IF to the local interface
- IP_ADD_MEMBERSHIP to the multicast group
- Add the sockets for read/write in kqueue
So depending on what I do in step 2 either read or write fails:
- If I bind the socket to the multicast group I get read notifications from the kqueue and can read packets, but when I get a write notification from the kqueue and try to write it fails with errno 49
- If I bind the socket to the interface address I can send the packets but no read notifications ever arrive from the kqueue
So which address I need to bind to then? Also, since the
ip_mreq has the interface field, do I really need the
If you bind to a specific interface address (on Linux at least), you'll be unable to receive multicast. 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. If you have multiple interfaces, you can call this for each interface you want to bind to.
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page