multiserver.java

来自「java网络编程」· Java 代码 · 共 70 行

JAVA
70
字号
package multithread;

import java.net.*;
import java.io.*;
import java.util.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class multiServer extends Thread{

  static int PORT=4000;
  private Socket s=null;

  public multiServer(Socket s) {
    this.s=s;
  }

  public static void main(String[] args) {
    ServerSocket ss=null;
    try{
      ss = new ServerSocket(PORT);
    }catch (IOException e){
      System.out.println("Unable to bind to port:"+PORT);
      return;
    }

    try{
      Socket s;
      while(true){
        //Socket s = ss.accept();
        s = ss.accept();
        multiServer t = new multiServer(s);
        //System.out.println("s="+s.toString());
        t.start();
        //t.handler();
      }
    }catch (IOException e){
      System.out.println("IO Exception occured.");
    }
  }

  public void handler(){
    try{
      Thread.sleep(4000);
    }catch (InterruptedException e){
    }
    System.out.println("connected from " + s.getInetAddress().toString()+"/"+
                       s.getPort()+"/"+Calendar.getInstance().getTime().toString());
    //System.out.println("this.s="+s.toString());
    try{
      s.close();
    }catch (Exception e){
    }
  }

  public void run(){
    handler();
  }
}

⌨️ 快捷键说明

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