5

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sid
  • 139
  • 1
  • 7

1 Answers1

4

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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2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6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
^C
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • Thanks. For the compilation issue, I tried post as short code as possible and made some mistakes. – Sid Sep 19 '11 at 10:25
  • But when I am trying to do IGMP snooping the log shows its trying to join group 224.0.0.51 instead of the one mentioned in the server – Sid Sep 19 '11 at 10:29
  • I found the issue. The issue was by default the interface address was set to the first ethernet card. Instead of that we need to set the interface as the address of the ethernet card for which multicast is enabled – Sid Sep 19 '11 at 13:54
  • 1
    Hi Sid. I'm having a similar issue to yours. I was wondering how you went about setting the interface to the address for which the multicast is enabled. Is this done through Java or through the operating system itself? – Justin Mar 01 '13 at 18:18