How to multicast send to all network interfaces?

北城以北 提交于 2019-12-11 13:46:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!