简体   繁体   English

为什么DatagramSocket不通过网络发送多播地址?

[英]Why a DatagramSocket does not send over the network with multicast address?

The follow code work only locally for me. 以下代码仅适用于我本地。 I can receive it on the same machine in another program. 我可以在另一个程序中的同一台机器上接收它。 I can not see any traffic in the wireshark (on Windows). 我看不到wireshark中的任何流量(在Windows上)。 If I change the multicast address to an existing address like 10.10.10.10 then I see the UDP packet in wireshark. 如果我将组播地址更改为现有地址(如10.10.10.10),那么我会在wireshark中看到UDP数据包。

In wireshark I use the filter udp.port == 5353. I can see some other packets to the multicast address that I thing my wireshark settings are correct. 在wireshark中,我使用过滤器udp.port == 5353.我可以看到一些其他数据包到多播地址,我认为我的wireshark设置是正确的。

The firewall is disabled. 防火墙已禁用。

public static void main( String[] args ) throws Exception {
    byte[] buf = "some data".getBytes();
    DatagramSocket socket = new DatagramSocket();
    InetAddress address = InetAddress.getByName( "224.0.0.251" );
    socket.send( new DatagramPacket( buf, buf.length, address, 5353 ) );
}

EDIT: The cause seems to be a loopback adapter (Microsoft Loopbackadapter für KM-TEST). 编辑:原因似乎是一个环回适配器(MicrosoftLoopbackadapterfürKM-TEST)。 If I remove the loopback adapter then it work. 如果我删除环回适配器然后它工作。 On another system there is a VMware adapter which can produce an equals problem. 在另一个系统上有一个VMware适配器可以产生一个等于问题。

Why is the packet not send to all network adapters? 为什么数据包不会发送到所有网络适配器? How can I send it to the right adapter? 如何将其发送到正确的适配器?

224.0.0/24 is reserved : 224.0.0 / 24 保留

Local Network Control Block (224.0.0/24) 本地网络控制块(224.0.0 / 24)

Addresses in the Local Network Control Block are used for protocol control traffic that is not forwarded off link. 本地网络控制块中的地址用于未通过链路转发的协议控制流量。

You can't use it. 你不能使用它。

@EJP is correct. @EJP是对的。 You cannot use that address as a multicast address. 您不能将该地址用作多播地址。

The range of addresses between 224.0.0.0 and 224.0.0.255, inclusive, is reserved for the use of routing protocols and other low-level topology discovery or maintenance protocols, such as gateway discovery and group membership reporting. 224.0.0.0和224.0.0.255之间的地址范围(包括端点)保留用于路由协议和其他低级拓扑发现或维护协议,例如网关发现和组成员身份报告。 Multicast routers should not forward any multicast datagram with destination addresses in this range, regardless of its TTL. 无论TTL如何,多播路由器都不应转发目标地址在此范围内的任何多播数据报。

Source: IANA - IPv4 Multicast Address Space Registry 来源:IANA - IPv4多播地址空间注册表

In other words, your chosen multicast address should not work, even though it is in the range of multicast addresses. 换句话说,您选择的多播地址不应该工作,即使它在多播地址范围内。

When sending unicast datagrams, the routing tables dictate which network interface is used to send the packet. 发送单播数据报时,路由表指示使用哪个网络接口发送数据包。 For multicast, you need to specify the interface. 对于多播,您需要指定接口。 You can do that with a MulticastSocket . 您可以使用MulticastSocket执行此操作。

Assuming that the IP of the interface you want to send on is 10.10.10.1, you would do the following: 假设您要发送的接口的IP是10.10.10.1,您将执行以下操作:

public static void main( String[] args ) throws Exception {
    byte[] buf = "some data".getBytes();
    MulticastSocket socket = new MulticastSocket();
    socket.setNetworkInterface(NetworkInterface.getByInetAddress(
                                 InetAddress.getByName( "10.10.10.1" )));
    InetAddress address = InetAddress.getByName( "224.0.0.251" );
    socket.send( new DatagramPacket( buf, buf.length, address, 5353) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM