简体   繁体   中英

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). If I change the multicast address to an existing address like 10.10.10.10 then I see the UDP packet in wireshark.

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.

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). If I remove the loopback adapter then it work. On another system there is a VMware adapter which can produce an equals problem.

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 :

Local Network Control Block (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. 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. Multicast routers should not forward any multicast datagram with destination addresses in this range, regardless of its TTL.

Source: IANA - IPv4 Multicast Address Space Registry

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 .

Assuming that the IP of the interface you want to send on is 10.10.10.1, you would do the following:

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) );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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