java.net.SocketException:不是多播地址

我正在使用MulticastSocket,当我尝试加入一个组时,当我在“localhost”ip上运行组时,它永远不会工作.但是,我发现这篇文章http://lycog.com/programming/multicast-programming-java/表明范围应该在224.0.0.1和239.255.255.254之间.当我从该IP中创建一个InetAddress并加入该组时,它就可以工作了.请解释为什么这是必要的.

例:

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

//抛出

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

有效的示例:

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

解决方法:

一切都与标准有关.关于多播地址,请参阅wiki article的简短片段:

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.

此外,多声道套接字在javadoc中几乎相同

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.

所以,是的,当您尝试加入具有超出此范围的组地址的多播组(如localhost 127.0.0.1)时,您会收到此异常.

上一篇:中缀到后缀的链式实现 c++


下一篇:SynchronousQueue源码解析(非公平模式)