Cornstalks
Cornstalks

Reputation: 38218

UDP multicasting: how do I know which group to send to?

I've got a little program that needs to communicate between two computers on the same LAN. I'm rather new to networking, but from what I've read it sounds like I want UDP multicasting so that the two computers can discover each other, after which I can establish a TCP connection for actual data communication. I've found this little example for UDP multicasting which I can follow. However, I'm wondering about the multicast group (HELLO_GROUP in that example, which is 225.0.0.37).

How can I know the group I should use? This program will be running on various networks, so I can't hard code one (as far as I know). Do I get the group from the router, and if so, how do I do that?

Upvotes: 0

Views: 6687

Answers (3)

simonc
simonc

Reputation: 42165

You can choose any multicast address (224.0.0.0 to 239.255.255.255) that isn't listed as reserved by IANA.

Its possible (if unlikely) that another program will also be using the same address. You can minimise the chances of this causing any confusion by making the announcement messages your program sends out suitably specific. e.g.

CORNSTALKS-DISCOVERY
HOST: {address:port}
[newline]

This would inform your recipients of the address to use for their TCP connection but should find its first line rejected by any other recipients.

Upvotes: 2

valdo
valdo

Reputation: 12943

You understood wrong.

What are you talking about is broadcasting. A broadcast UDP datagram is sent to every computer in the subnet. (Technically you send a datagram to the address 255.255.255.255.)

UDP broadcast work inside a specific subnet, but don't cross the subnet boundaties. That is, most of the routers are configured not to route broadcast datagrams (reduce spamming).

OTOH multicast is something completely different. The purpose of multicast is to avoid using TCP (or any other unicast) for data transmission. It's good when you need to send something to many other recipients "at once". Those machine agree preliminary on a specific multicast address (like 225.0.0.37 in your example), and "join" this multicast group. Within a specific subnet everything works pretty similar to broadcast, however in contrast to broadcast the multicast may also cross the subnet boundaries. This is due to the fact that when machines join a multicast group the appropriate routers are notified, and they are capable to route multicast datagrams appropriately.

EDIT:

Conclusion (for clarification).

  1. In order to use a multicast one has to pick a multicast address. This is like choosing a port for the application.
  2. The main purpose of multicast is to deliver content (transmit data) to a number of recipients. It's more efficient than unicast in this case.
  3. A "network discovery" is usually done via broadcast. A multicast can theoretically be used for this as well, but this is like killing a fly with a cannon (because routers should also track the lifetime of the multicast session).

Upvotes: 1

Tom Anderson
Tom Anderson

Reputation: 47173

I would suggest you don't use multicast directly.

Rather, use zero-configuration networking. This, in its mDNS/DNS-SD incarnation, is available through Apple's Bonjour library on OS X and Windows, and Avahi on unices (and possibly on OS X and Windows too, not sure).

With DNS-SD, you define a name for your service, then use the library to advertise its availability on a given host, or to browse for hosts where it's available. This is how Macs discover printers, file shares, etc - exactly your use case, i believe. It's a simple but very effective technology. And it's an open standard with a good open source implementation, so it's not some proprietary Apple scarytime.

Upvotes: 0

Related Questions