Ejemplo n.º 1
0
  /* クライアントの情報を消去するメソッド 『BYE』*/
  void removeClient() throws IOException {
    ChatClientHandler removeHandler = this;
    ChatClientHandler handler = null;

    clients.remove(removeHandler); // クライアントをリストから消去

    post(removeHandler.getClientName() + " disconnected.");

    System.out.println(removeHandler.getClientName() + " disconnected."); // サーバ
  }
Ejemplo n.º 2
0
  /*  並列実行を行うときに実行されるメソッド */
  public void run() {
    try {
      open();

      while (true) {
        String message = receive(); // 受け取ったメッセージをmassageに格納
        // 受け取ったメッセージをスペースで区切って配列に変換
        String[] commands = message.split(" ");
        System.out.print(getClientName() + ": ");

        // コマンド処理
        try {
          /*+++++++システムとユーザに関するコマンド+++++++++++++*/
          if (commands[0].equals("help")) { // 『HELP』
            // コマンドが指定されたらその説明を、指定されなかったら一覧を表示
            try {
              commandHelp(commands[1]);
            } catch (ArrayIndexOutOfBoundsException e) {
              help();
              System.out.println("help :");
            }
          } else if (commands[0].equals("name")) { // 『NAME』
            System.out.print("name: ");
            name(commands[1]);
          } else if (commands[0].equals("whoami")) { // 『WHOAMI』
            System.out.print("whoami: ");
            whoami();
          } else if (commands[0].equalsIgnoreCase("post")) { // 『POST』
            System.out.print("post: ");
            post(commands[1]);
          } else if (commands[0].equals("bye")) { // 『BYE』
            System.out.print("bye: ");
            removeClient();
            close();
          } else {
            System.out.println("コマンドが存在しません。");
            this.send("コマンドが存在しません。(helpでコマンド一覧)");
          }

        } catch (ArrayIndexOutOfBoundsException e) { // 引数の数が間違っているとき
          System.out.println("コマンドの使い方が間違っています。");
          this.send("コマンドの使い方が間違っています。(helpでコマンド一覧)");
        }
      }
    } catch (IOException e) {
      e.printStackTrace();

    } finally {
      try {
        removeClient();
        close();
      } catch (IOException e) {
      }
    }
  }