ketan
ketan

Reputation: 2904

Java Multicast receiver not working

I want to capture packets from one Multicast Group and port. First I checked using tcpdump command that data gets captured or not.

Command -

tcpdump -i <interface name> dst <multicast ip>

I created one program in java which will join this multicast group and prints the data on console.

receiver -

public class MulticastClient {

    public static void main(String args[]) throws IOException {
        MulticastSocket socket = new MulticastSocket(<port number here>);
        //socket.setInterface(InetAddress.getByName("<local interface ip address>")); // This line makes sense or not...don't know
        socket.joinGroup(InetAddress.getByName("<multicast ip here>"));

        while (true) {
            byte ab[] = new byte[583];
            DatagramPacket packet = new DatagramPacket(ab, ab.length);
            socket.receive(packet);
            System.out.println("Got packet " + Arrays.toString(ab));
        }
    }
}

This program is not able to capture the packets from given multicast group and port.

For testing, I created one MulticastSockerServer which will send data on one multicast group and port.

sender -

public class MulticastServer {

    public static void main(String[] args) throws Exception {

        final InetAddress group = InetAddress.getByName("<multicast group ip>");
        final int port = <port number>;

        try {
            MulticastSocket socket = new MulticastSocket(9000);
            //socket.setInterface(InetAddress.getByName("<local interface ip address>"));
            socket.joinGroup(group);

            byte[] bt = new byte[100];
            byte index = 0;
            while (true) {
                Arrays.fill(bt, (byte) index++);
                socket.send(new DatagramPacket(bt, 100, group, port));
                System.out.println("sent 100 bytes");
                Thread.sleep(1 * 1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

If I run this two program then it will works as I expected, data get captured at receiver side. Then why the receiver only is not able to capture data from different multicast group ip and port?

Any suggestions will be appropriated.

Upvotes: 2

Views: 1104

Answers (1)

ketan
ketan

Reputation: 2904

I got clarity and I found why java client not getting data. Actually the tcpdump command is -

tcpdump -i <interface name> dst <multicast ip>

This command will capture any packets (i.e. No matter it is UDP or PGM) which has destination address is given by command (multicast ip).

Java Client will only capture UDP Packets not PGM Packets. On my local network, only PGM packets are available so tcpdump command will give us data but Java client will not receive any data.

If you want to capture PGM Packets then you need to use one of the below -

  1. Use JnetPcap library to capture/read any (i.e. UDP or PGM) packets.
  2. Use Javapgm library to capture pgm packets only.
  3. Use dumpcap to capture packets and store it in a pcap file.

Hope it helps.

Upvotes: 1

Related Questions