I am developing a chat application where Android clients will exchange their IP's using multicasting(UDP).
Every device will send its ip to multiple clients(all the devices running this app) in one separate Thread. There will be another receiver thread which will listen to these multicast packets. Here is my code.
//Multicasting code.
DatagramSocket socket = new DatagramSocket(9898);
byte buff[] = ip.getBytes();
DatagramPacket packet = new DatagramPacket(buff, buff.length, InetAddress.getByName("224.0.0.1"),9999);
socket.send(packet);
socket.close();
//Receiver code
MulticastSocket socket = new MulticastSocket(9999);
InetAddress group = InetAddress.getByName("224.0.0.1");
socket.joinGroup(group);
DatagramPacket packet;
byte[] buf = new byte[256];
byte b = 'x'; //just a separator for time being
Arrays.fill(buf,b);
packet = new DatagramPacket(buf, buf.length);
String received= "";
while(received!=null)
{
socket.receive(packet);
received = new String(packet.getData());
received = received.substring(0,received.indexOf('x'));
this.setIp(received);
System.out.println("Address: " + received);
}
socket.leaveGroup(group);
socket.close();
The problem is every device prints its own address. It seems it never listens to other multicast packages(I mean it should print other ip's as well). I also get a below log, not sure if that's related.
11-04 23:56:17.985: I/OSNetworkSystem(603): mcastAddDropMembership interfaceIndex=0
Any help will be appreciated.