@Override
  public void run() {
    while (true) {
      try {
        DatagramSocket datagramSocket = new DatagramSocket(CharacterUtil.PORT2);
        System.out.println(datagramSocket);
        byte[] buf = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(buf, 1024);
        datagramSocket.receive(datagramPacket);
        String message = new String(buf, 0, datagramPacket.getLength()); // 某个用户发来的信息
        System.out.println(message);
        //				this.socket2 = serverSocket.accept();
        //				InputStream is = socket2.getInputStream();
        //				byte[] buf = new byte[1024];
        //				int length = is.read(buf);
        //				String message = new String(buf, 0, length);

        Map<String, String> map = mainServer.getMap();
        Iterator<String> iter = map.values().iterator();
        while (iter.hasNext()) {
          String ports = iter.next();

          int index = ports.indexOf("_");
          int port = Integer.parseInt(ports.substring(0, index));
          int lastIndex = ports.lastIndexOf("_");
          String address = ports.substring(lastIndex + 1);
          System.out.println(address);

          Socket socket = new Socket(address, port);
          OutputStream os = socket.getOutputStream();
          os.write(message.getBytes());
          os.close();
          socket.close();
        }

        datagramSocket.close(); // 莫忘!

      } catch (SocketException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
Esempio n. 2
0
 public static MapView getMap(short id) {
   return server.getMap(id);
 }