8

I was working with a MulticastSocket and when ever I tried to join a group, it would never work when I was running the group on the "localhost" ip. However, I found this article http://lycog.com/programming/multicast-programming-java/ that stated the range should be between 224.0.0.1 and 239.255.255.254. When I made an InetAddress out of that IP and joined the group it worked. Please explain why this is necessary.

Example:

InetAddress group = InetAddress.getByName("localhost");
int port = 8888;
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(group);

//throws

Unable to connect to host:localhost on port:8888
java.net.SocketException: Not a multicast address

Example that works:

InetAddress group = InetAddress.getByName("224.0.0.1");
int port = 8888;
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(group);
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
swrap
  • 314
  • 1
  • 3
  • 11
  • I think: [wildcard group.](https://tools.ietf.org/html/rfc4601#section-4.9.5.1) – markspace Nov 26 '15 at 19:53
  • 1
    "Please explain why this is necessary" <-- well, 127.0.0.1 is not a multicast address; so why does it come as a surprise that you cannot use it as a multicast address? – fge Nov 26 '15 at 20:02
  • I was assuming that it would open a socket out of the port given to it. Therefore it would not matter what the ip is. – swrap Nov 26 '15 at 20:18
  • @swrap You don't seem to understand what IP multicast actually is. Too broad to explain all that here. – user207421 Nov 26 '15 at 20:38
  • @markspace No, that's for routers, not applications. – user207421 Nov 26 '15 at 20:39

1 Answers1

12

it's all about standards. Just a short snippet from wiki article about Multicast addresses:

IPv4 multicast addresses are defined by the leading address bits of 1110, originating from the classful network design of the early Internet when this group of addresses was designated as Class D. The Classless Inter-Domain Routing (CIDR) prefix of this group is 224.0.0.0/4. The group includes the addresses from 224.0.0.0 to 239.255.255.255.

Furthermore, almost the same is said in javadoc for the MulticastSocket

A multicast group is specified by a class D IP address and by a standard UDP port number. Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. The address 224.0.0.0 is reserved and should not be used.

So, yes, when you try to join a multicast group with a group address out of this range (like a localhost 127.0.0.1), you get this exception.

user207421
  • 305,947
  • 44
  • 307
  • 483
Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • I was wondering what is the difference between a multicast socket and a regular socket. For example, why can you not just provide a port number for the mutlicast socket to run on a localhost? – swrap Nov 26 '15 at 20:21
  • 1
    @swrap Because you need to join the multicast group you want to receive from. – user207421 Nov 26 '15 at 20:37
  • @EJP would a MulticastServerThread need to be created first on the InetAddress before having clients connect? – swrap Nov 26 '15 at 21:39
  • @srwap What is a `MulticastServerThread`? There isn't such a class in the JDK. – user207421 Nov 26 '15 at 21:54
  • @EJP sorry I was looking at an example and miss-read the text. Is the range of IP addresses used for multicast groups run on the local network? I am trying to better understand what multicast is. – swrap Nov 27 '15 at 00:51
  • @swrap The range of IP addresses used for multicast is listed in this answer. I don't know what you mean by the 'run on the local network' part. – user207421 Nov 27 '15 at 01:11
  • Nvm I figured it out. Thanks for all the help! – swrap Nov 27 '15 at 01:23