2

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?

haelix
  • 4,245
  • 4
  • 34
  • 56

1 Answers1

3

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)

Joel Cunningham
  • 641
  • 6
  • 17
  • That quotation is about protocol processing. It doesn't have anything to do with multicast API design or behaviour. – user207421 Aug 20 '16 at 00:14
  • I don't see how this could be a protocol question. When it gets to the protocol (wire) level it's always a packet on each interface. The inefficiency (to me) is having to create N sockets and submit N datagrams to the stack rather than having a 1-to-all interfaces send mechanism. The question even mentions trying to use a single socket in the last paragraph. How are you interpreting the question? – Joel Cunningham Aug 20 '16 at 00:30
  • How would you implement service discovery? Server and client(s) in the same network, but the client(s) need to know the server's IP – David Gomes Mar 18 '22 at 21:35