I'm trying to code a service that every 10 seconds sends a message to multicast group. The sender is the following:
public void send(){
int port = (Integer) config.getValue("MULTICAST_PORT");
MulticastSocket users = new MulticastSocket(port);
users.setTimeToLive((Integer) config.getValue("MULTICAST_TTL"));
users.setLoopbackMode(false);
InetAddress multicastGroup = InetAddress.getByName((String) config.getValue("MULTICAST_IP"));
String x = "0";
DatagramPacket pkt = new DatagramPacket(x.getBytes(), x.getBytes().length, multicastGroup, port);
while(true) {
System.out.println("Send Pkt");
users.send(pkt);
Thread.sleep(10000);
}
}
The receiver have to read the packet and reply.
MulticastSocket multiServer = new MulticastSocket((Integer) config.getValue("MULTICAST_PORT"));
InetAddress multicastGroup = InetAddress.getByName((String) config.getValue("MULTICAST_IP"));
multiServer.joinGroup(multicastGroup);
while(true){
System.out.println("WAIT KeepAlive Packet");
server.receive(pkt);
DataInputStream in = new DataInputStream(new ByteArrayInputStream(
pkt.getData(), pkt.getOffset(), pkt.getLength()
));
if(in.readInt() == 0){ //0 means "OK"
ObjectSocket skt = new ObjectSocket(InetAddress.getByName(
(String) config.getValue("SERVER_HOSTNAME")),
Integer.parseInt((String) config.getValue("SERVER_PORT")));
try{
skt.writeObject(new PacketMessage(new SimpleMessage((String) config.getValue("USER"), (String) config.getValue("OAUTH")), PacketMessage.MessageType.KEEPALIVE));
} catch (UnregisteredConfigNameException e){
return;
}
}
The output is, for sender:
send Pkt
[10 sec sleep]
send Pkt
(many times...)
for receiver is:
WAIT KeepAlive Packet
Both functions are running on same machine, using two different processes. The MULTICAST_IP is the same (verified). I don't understand why receive()
not returns.
MULTICAST_PORT values are different in reference of function executed but this should not be problem. No exceptions are thrown.