Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.SocketException: Not a multicast address

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);
like image 326
swrap Avatar asked Nov 26 '15 19:11

swrap


1 Answers

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.

like image 80
Stanislav Avatar answered Sep 27 '22 21:09

Stanislav