chathandler.java

来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 57 行

JAVA
57
字号
import java.io.*;
import java.net.*;

class ChatHandler extends Thread {

  /* The Chat Handler class is called from the Chat Server:
   * one thread for each client coming in to chat.
   */

  private BufferedReader in;
  private PrintWriter out;
  private Socket toClient;
  private String name;

  ChatHandler(Socket s) {
    toClient = s;
  }

  public void run() {
    try {
      /* Create i-o streams through the socket we were
       * given when the thread was instantiated
       * and welcome the new client.
       */

      in = new BufferedReader(new InputStreamReader(
        toClient.getInputStream()));
      out = new PrintWriter(toClient.getOutputStream(), true);
      out.println("*** Welcome to the Chatter ***");
      out.println("Type BYE to end");
      out.print("What is your name? ");
      out.flush();
      String name = in.readLine();
      ChatServer.broadcast(name+" has joined the discussion.",
        "Chatter");

      // Read lines and send them off for broadcasting.
      while (true) {
        String s = in.readLine().trim();

        if (s.equals("BYE")) {
          ChatServer.broadcast(name+" has left the discussion.",
          "Chatter");
          break;
        }
        ChatServer.broadcast(s, name);
      }
      ChatServer.remove(toClient);
      toClient.close();
    }
    catch (Exception e) {
    System.out.println("Chatter error: "+e);
  }
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?