0
votes

I have a UDP multicast server listening on 2 sockets on 2 different ports. I achieved listening on these 2 sockets from clients. But i want to identify on which socket a client is sending a packet. Since my problem is that ; on the server if I listen on socket(9999) and if the client is sending on socket(8888) then at the server side I want it to identify the incoming packet is from which port.

public class MulticastReceiver 
{
  public static void main(String[] args)
  {
      MulticastSocket socket = null;
      DatagramPacket packet  = null;

      MulticastSocket soc = null;
      byte[] inBuf = null;
      try
      {
          socket = new MulticastSocket(8888);
          soc = new MulticastSocket(9999);

          InetAddress address = InetAddress.getByName("224.2.2.3");
          socket.joinGroup(address);
          soc.joinGroup(address);

          System.out.println("224.2.2.3 ready to receive packets");
          while(true)
          {
              inBuf=new byte[256];

              packet = new DatagramPacket(inBuf,inBuf.length);
              System.out.println("port is: "+ packet.getAddress() + packet.getPort());
              if(packet.getPort() == 9999) 
              {
                  soc.receive(packet);
              //System.out.println("Data at 224.2.2.3:: " + new String(packet.getData()));
              }
              else
                  socket.receive(packet);

              System.out.println("Data at 224.2.2.3:: " + new String(packet.getData()));
          }
      }
      catch(Exception e)
      {

      }


  }
}





public class MulticastSender {
  public static void main(String[] args) {
    DatagramSocket socket = null;
    DatagramPacket outPacket = null;
    byte[] outBuf;
    final int PORT = 8888;

    try {
      socket = new DatagramSocket();
      long counter = 0;
      String msg;

        msg = "This is multicast! ";

        outBuf = msg.getBytes();

        //Send to multicast IP address and port
        InetAddress address = InetAddress.getByName("224.2.2.3");
        outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);

        socket.send(outPacket);

        System.out.println("Server sends : " + msg);
        try {
          Thread.sleep(500);
        } catch (InterruptedException ie) {
        }

    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}




public class AnotherSender {
  public static void main(String[] args) {
    DatagramSocket socket = null;
    DatagramPacket outPacket = null;
    byte[] outBuf;
    final int PORT = 9999;

    try {
      socket = new DatagramSocket();
      long counter = 0;
      String msg;

        msg = "This is another multicast! " + counter;
        counter++;
        outBuf = msg.getBytes();

        //Send to multicast IP address and port
        InetAddress address = InetAddress.getByName("224.2.2.3");
        outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);

        socket.send(outPacket);

        System.out.println("Server sends : " + msg);
        try {
          Thread.sleep(500);
        } catch (InterruptedException ie) {
        }

    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}
1

1 Answers

2
votes

Your code doesn't make sense. The packet won't have a port number at all until you put one into it or receive() does, and at best this will just read alternately between the two sockets, blocking each time, possibly forever, receiving from one socket, and thus starving the other one.

You need a receiving thread for each non-blocking socket.