gpt4 book ai didi

java.net.SocketException : Not a multicast address 异常

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:39 27 4
gpt4 key购买 nike

我正在使用 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 中的说法几乎相同。对于 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.

所以,是的,当您尝试加入组地址超出此范围的多播组时(例如本地主机 127.0.0.1),您会遇到此异常。

关于java.net.SocketException : Not a multicast address 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33946043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com