0

我有一个 UDP 多播服务器在 2 个不同端口上的 2 个套接字上侦听。我实现了从客户端监听这 2 个套接字。但我想确定客户端在哪个套接字上发送数据包。因为我的问题是;如果我在服务器上侦听套接字(9999)并且客户端在套接字(8888)上发送,那么在服务器端我希望它识别传入的数据包来自哪个端口。

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);
    }
  }
}
4

1 回答 1

2

你的代码没有意义。数据包根本没有端口号,直到您将一个端口号放入其中或receive()有,并且充其量只会在两个套接字之间交替读取,每次都阻塞,可能永远阻塞,从一个套接字接收,从而使另一个套接字挨饿一。

每个非阻塞套接字都需要一个接收线程。

于 2013-11-16T00:03:45.060 回答