我正在使用 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);