Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Unable to receive multicast data

Tags:

java

I am trying to test a multicast client server application but my client is unable to receive any data. I read through the tutorials and cant find any way. Can you help me out here.

Client code

MulticastSocket socket = new MulticastSocket("9000");
socket.setInterface(InetAddress.getLocalHost());
socket.joinGroup("237.0.0.1");

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

Server Code

MulticastSocket socket = new MulticastSocket("9000");
socket.setInterface(InetAddress.getLocalHost());

socket.joinGroup("237.0.0.1");

byte index = 0;
while(true)
{
    byte[] bt = new byte[100];
    Arrays.fill(bt, (byte)index++);
    DatagramPacket packet = new DatagramPacket(bt, 100,"237.0.0.1", "9000");
    socket.send(packet);
    System.out.println("sent 100 bytes");
    Thread.sleep(10*1000);
}

I am thinking the problem is with the way I am setting the interface.

Could someone help me out here and clear what am I missing to understand.

like image 248
Sid Avatar asked Oct 09 '22 23:10

Sid


1 Answers

Your code does not compile on my system, but when I did the following changes (below) it works as expected:

  • Port number should be an int

    MulticastSocket socket = new MulticastSocket(9000);
    
  • Ip addresses should be specified as an InetAddress using:

    InetAddress.getByName("237.0.0.1")
    

Other then that, check that your firewall isn't blocking the traffic.


Full example (with the compile-fixes above):

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

    final InetAddress group = InetAddress.getByName("237.0.0.1");
    final int port = 9000;

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                MulticastSocket socket = new MulticastSocket(port);
                socket.setInterface(InetAddress.getLocalHost());
                socket.joinGroup(group);

                DatagramPacket packet = new DatagramPacket(new byte[100], 100);
                while(true) {
                    socket.receive(packet);
                    System.out.println("Got packet " + 
                            Arrays.toString(packet.getData()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                MulticastSocket socket = new MulticastSocket(port);
                socket.setInterface(InetAddress.getLocalHost());
                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();
            }
        }
    }).start();
}

Output:

sent 100 bytes
Got packet [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sent 100 bytes
Got packet [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
sent 100 bytes
Got packet [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 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