3

I'm trying to join a multicast group after creating a MulticastSocket.

Doing something like:

MulticastSocket mySocket = new MulticastSocket(4444);
mySocket.joinGroup(InetAddress.getByName("230.0.0.1")); // as an example

works fine. When I use the MulticastSocket constructor that accepts a SocketAddress as a parameter, though, the multicast group is not joined and a separate call on joinGroup() is required.

MulticastSocket mySocket = new MulticastSocket(new InetSocketAddress("230.0.0.1", 4444));

Why is this?

Thanks!

A Timmes
  • 3,093
  • 3
  • 16
  • 5

5 Answers5

9

Unless I'm wrong multicast is implemented by registering with the local network switch (using the joinGroup method that sends an IGMP message) and then the switch will forward all packets sent to the multicast address to every device that has registered. If you don't invoke joinGroup, no group registration message gets sent, the local switch has no clue that you care about the messages, and so you don't actually get any messages.

Some switches are stricter than others about sending: I've seen some switches where a device could send sent to the multicast group without registering, but not receive packets to the multicast group, and I've seen other switches where the device had to register to successfully send to other members of the multicast group or receive packets sent to the multicast group.

Femi
  • 64,273
  • 8
  • 118
  • 148
2

I think it is the way the multicast protocol/RFC was designed. Until the "joinGroup" is invoked, the application will ignore all multicast datagrams. It is the "joinGroup" which makes the application accept packets.

Read more about it here: http://tldp.org/HOWTO/Multicast-HOWTO-2.html

Goto section Joining a Multicast Group. in the link above for the specific details.

mrk
  • 4,999
  • 3
  • 27
  • 42
kensen john
  • 5,439
  • 5
  • 28
  • 36
2

If you don't construct the MulticastSocket with a group address you have to join the group some other way. Otherwise why should you receive any messages for that group?

NB constructing with a group address doesn't work on all platforms AFAIK. In fact the constructor that takes a SocketAddress is specified to use this as a local bind-address, not a multicast group. I believe specifying a group address does work on Linux but not some other platforms. I would use it the way it's documented and call joinGroup() explicitly. Note that on multi-homed hosts you may need to call joinGroup() for each available NIC.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Femi is correct: you need to call joinGroup() to inform your network that you're interested in receiving packets for that multicast group. (Keep in mind that all IP traffic is broadcast traffic; that's why Wireshark and other "promiscuous" programs work.)

The constructor that takes an IP address isn't specifying a group to join: it's specifying which of your network cards to use. If you don't specify the IP address of the card itself, you're requesting the card that routes messages to the address you specified. (Wow, that's a lot of specifying.) So you still need to joinGroup() afterwards; it's just that only the card you ... indicated ... will be receiving the packets.

pankar
  • 1,623
  • 1
  • 11
  • 21
Judebert
  • 551
  • 4
  • 4
-1

When I use the MulticastSocket constructor that accepts a SocketAddress as a parameter, though, the multicast group is not joined and a separate call on joinGroup() is required.

The parameter in the constructor can be null. So the constructor may not be able to call joinGroup. Moreover, the document says it only binds.

I think not calling joingGroup() in the constructor is just standard good practice in Java. If the class constructor did, the user would loose the possibility to control when this method is called.

He/She may want to do prepare datastructure before joining the group or it may want wait for a user signal or some other process to finish.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453