connectionhandler.java

来自「一步 教你学会java 完成一个完整的SWT系统 是本人学习java」· Java 代码 · 共 54 行

JAVA
54
字号
/** *  Example code for JavaOne hands-on J2SE lab * *  Concurrency utilities (JSR-166) example **/package threadpool;import java.io.*;import java.net.*;import java.util.concurrent.*;public class ConnectionHandler implements Runnable {  private final Socket socket;  private final int connectionID;  /**   *  Constructor   *   * @param socket The socket on which incoming data will arrive   **/  public ConnectionHandler(Socket socket, int connectionID) {    this.socket = socket;    this.connectionID = connectionID;  }  /**   *  run method to do the work of the handler   **/  public void run() {    System.out.println("Connection " + connectionID + ", started");    try {      InputStream is = socket.getInputStream();      //  Loop to do something with the socket here      while (true) {        byte[] inData = new byte[100];          /*  If the number of bytes read is less than zero then the connection         *  has been terminated so we end the thread         */        if (is.read(inData) < 0)          break;        System.out.print("[" + connectionID + "]: " + new String(inData));      }    } catch (IOException ioe) {      // Ignore    }    System.out.println("Connection " + connectionID + ", ended");  }}

⌨️ 快捷键说明

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