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.
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
intIp addresses should be specified as an
InetAddressusing:Other then that, check that your firewall isn't blocking the traffic.
Full example (with the compile-fixes above):
Output: