How to use multicasting with DatagramSocket?

I’m trying to make an audio application that can connect to itself over the network between two computers. I understand how InterprocessConnection works, but I’m very new to the entire concept of broadcast/multicast. I’ve been also researching that, and I think I have a good handle on how it works.

I found DatagramSocket’s function joinMulticast(), too, but I’m not sure how to use it exactly. I can see that it connects the DatagramSocket to a multicast address, but what can be done from there exactly? Should I be Googling how to, and coding myself, sending a find request, or do these libraries already know how to do that and I just can’t find it? Or am I wrong about needing to do that, too?

If my goal is to use multicast to get two computers connected, should I even be doing what I’m attempting right now?: using multicast to have the programs find each others’ IP addresses, and then connecting to them with InterprocessConnection?

Hey! Good questions.

So - you need some Multicast info more than juce. Multicast is a special case of UDP. It’s similar to Broadcast. Basically, you join a group and send to that group. Every machine that has joined that group (every socket, really) will receive all the information that you send (I think there’s some flag about whether you even get your own data back).

So - you can do what you want. If you want each machine to know the other machines, you can see the source address (normally, don’t have juce code open to check) and then make a TCP/IP socket to that address, but if UDP works for you (main limitation in practice is the packet size, about 1492 bytes) then you can just send to the group and receive also.

In short - you need to make your own protocol. XML, JSON, even plain text will work, and parse the incoming data yourself.


Thank you! That helped me understand this a lot better. Now, I’m facing a new issue. It looks like the DatagramSocket class doesn’t support being copied, which means I can’t do what I was trying to do: declare an Array<DatagramSocket> (or any sort of vector at all) and iterate over it. I have no idea how else I could have an indeterminate amount of DatagramSockets, which means I have no idea how I could have this program multicast over more than one network interface, without providing a bunch of checkboxes in the GUI for each one that an average user would be confused by.

Unless I’m misunderstanding this whole thing? For example, I’m not sure if I actually need to cast over each interface, or if the way that multicast works handles that for me. The conflicting facts that it uses a certain set of IP addresses that are the same numbers for every interface, and yet the joinMulticast()/multicast() code that I read needs the socket to be bound to an interface IP and port first, are what is confusing me there.

You’re looking to have one datagram socket for each possible network interface?

I’d assume you can have an ownedarray or similar of DatagramSockets. (Or referenceCountedArray of a subclass like: multicastSocket).

That’s a good idea, btw.

You may hit a snag with multiple sockets trying to bind to the same port.