Multicast in java

I am trying to write a simple multicast trial.

I used the standard code (sender and receiver).

I tried several different standard pieces of code. it seems that the receiving code is stuck at reception (as if it was not receiving anything).

take side:

        byte[] b = new byte[3];
    DatagramPacket dgram = new DatagramPacket(b, b.length);
    MulticastSocket socket =
      new MulticastSocket(4545); // must bind receive side
    socket.joinGroup(InetAddress.getByName("226.100.100.125"));

    while(true) {
      socket.receive(dgram); // blocks until a datagram is received
      System.err.println("Received " + dgram.getLength() +
        " bytes from " + dgram.getAddress());
      dgram.setLength(b.length); // must reset length field!
    }

sending side:

      DatagramSocket socket = new DatagramSocket();

  byte[] b = new byte[]{(byte)1,(byte)5,(byte)3};
  DatagramPacket dgram;

  dgram = new DatagramPacket(b, b.length,
    InetAddress.getByName("226.100.100.125"), 4545);

  System.err.println("Sending " + b.length + " bytes to " +
    dgram.getAddress() + ':' + dgram.getPort());
  while(true) {
    System.err.print(".");
    socket.send(dgram);
    Thread.sleep(1000);
  }

What is wrong with my code? * I also tried many different IPs *

thanks for the help.

+3
source share
5 answers

IP-, localhost. , , , . , IP-. - 233.x.x.x - 239.x.x.x.

, , , , localhost. , , .

+2

239.0.0.0/8 . , ip , .

RFC, :

http://tools.ietf.org/html/rfc2365

... :

            DatagramPacket p = ...
            MulticastSocket s = new MulticastSocket(LISTENPORT);
            InetAddress group = InetAddress.getByName(LISTENIP);
            s.joinGroup(group);
            s.send(p);
            s.leaveGroup(group);
+2

( ) . , , , . ( - ), , .

, : http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xml, , 226 . RFC:

  • IANA

    IANA .

- .

+1

You should not use the same port, try a different port and create one socket for receiving, and the second for sending.

0
source

When this happens in my Linux boxes, I check that

1) there is a route for 224.0.0.0/4 on the correct interfaces 2) the source IP address corresponds to one of the routes for this interface

# 2 is the most sticky in my lab. If my eth1 has only a route for 10.77.4.0/24, and some field passes from 10.78.5.15, then linux discards it as a "Martian packet."

0
source

Source: https://habr.com/ru/post/1793719/


All Articles