コード例 #1
0
  /** 把当前客户端信息 推送到其他客户端 */
  private void dispatch(SocketChannel client, String info) throws IOException {

    Socket s = client.socket();
    String name =
        "["
            + s.getInetAddress().toString().substring(1)
            + ":"
            + Integer.toHexString(client.hashCode())
            + "]";
    if (!clientsMap.isEmpty()) {
      for (Map.Entry<String, SocketChannel> entry : clientsMap.entrySet()) {
        SocketChannel temp = entry.getValue();
        if (!client.equals(temp)) {
          sendBuffer.clear();
          sendBuffer.put((name + ":" + info).getBytes());
          sendBuffer.flip();
          // 输出到通道
          temp.write(sendBuffer);
        }
      }
    }
    clientsMap.put(name, client);
  }