0 Replies Latest reply: Nov 15, 2012 7:30 PM by 974540 RSS

    MulitcastSocket with same port

    974540
      I am trying to use two separate programs to listen two multicast groups (239.1.1.2:51000 and 239.1.1.3:51000)

      Problem:
      Two programs are received all of messages sent by both groups

      I can solve this problem by comment the following statement on C++ program, but I don't know how to do this on Java

      Program code: (C++)
      //localsock.sin_addr.s_addr = htonl(INADDR_ANY); <--- comment this statement
      localsock.sin_addr.s_addr = inet_addr( m_sMulticastGrp.c_str() );
      localsock.sin_port = htons(m_nPort);

      Program code: (Java)
      ...

      NetworkInterface networkInterface = new NetworkInterface.getByName(args[1]); //args[1] = eth0
      MulticastSocket socket = new MulticastSocket(51000);
      socket.setNetworkInterface(networkInterface);
      socket.joinGroup(args[0]); //args[0] = 239.1.1.2 or 239.1.1.3

      ...

      DatagramPacket packet = new DatagramPacket(buf, buf.length);
      try{
      while (true){
      socket.receive(packet);
      ...
      }
      }catch(IOException ex){
      ...
      }