问题
Say I have N multicast-capable network interfaces. I am planning to bind N UDP sockets, one to each interface, and send to the same multicast ip/port. Is there a more direct/efficient approach than this?
When receiving, I know you can listen over multiple interfaces using the same socket, but sending cannot be done with a single socket, or can it?
回答1:
Another aproach is to use IP_MULTICAST_IF to change the interface used for sending multicast on the socket. With this approach, you would call send N times and change the sending interface before each send. This would allow you to reduce the number of sockets in use, but not the number of send calls.
The multicast send behavior is inline with and guides the network application programer towards the Robustness Principle/Postel's law:
Be conservative in what you do, be liberal in what you accept from others (often reworded as "Be conservative in what you send, be liberal in what you accept").
What I mean by this is that the socket APIs and behavior make it really easy to receive from multiple interfaces on a single socket (liberal receiving), but disallow sending out multicast and broadcast out all interfaces from a single socket, thus forcing the programmer to very consciously write the application to send out multiple interfaces (conservative sending)
来源:https://stackoverflow.com/questions/10702870/how-to-multicast-send-to-all-network-interfaces