0

I'm trying to uso MulticastSocket between two process. Server send packet and client need to read it. The server code is:

MulticastSocket multicastSocket = new MulticastSocket();
multicastSocket.setTimeToLive((Integer) config.getValue("MULTICAST_TTL"));
multicastSocket.setLoopbackMode(false);
multicastSocket.setReuseAddress(true);

String msg = "KA";
InetAddress multicastGroup = "225.3.0.1";
int port = 4000;
DatagramPacket pkt = new DatagramPacket(msg.getBytes(), msg.getBytes().length, multicastGroup, port);


while(true) {
     try{
        multicastSocket.send(pkt);
        System.out.println("SPEDITO PACCHETTO: "+pkt.getSocketAddress());
     }catch ( IOException e){
        System.out.println("Errore di comunicazione con la rete multicast. "+e.getMessage());
     }
}

The client code is:

  this.multicastSocket = new MulticastSocket(4000);
  InetAddress multicastGroup = InetAddress.getByName("225.3.0.1");
  this.multicastSocket.joinGroup(multicastGroup);
  DatagramPacket pkt = new DatagramPacket(new byte[512], 512);
    while(true){
            System.out.println("GOING TO READ");
            multicastSocket.receive(pkt);
            System.out.println("READ");
            byte[] b = pkt.getData();
            String msg = new String(b, 0, pkt.getLength());
    }

The problem is that server send packet (send(pkt) returns), instead client not receive packate (receive(pkt) not returns). Where is the problem?

p.s.: Server and client are on same computer, and MULTICAST_TTL is 1.

Alessandro
  • 199
  • 2
  • 16

0 Answers0