⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiserver1.java

📁 java网络编程
💻 JAVA
字号:
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 multiServer1 extends Thread{

  static int PORT=4000;
  private Socket s=null;
  //private int count=0;
  static ThreadPool tp;
  private String threadName;

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

  public void setThreadName(String threadName){
    this.threadName=threadName;
  }

  public static void main(String[] args) {
    tp=new ThreadPool();
    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();
        //multiServer1 t = new multiServer1(s);
        //System.out.println("s="+s.toString());
        multiServer1 task=new multiServer1(s);
        PoolableThread pt=tp.runTask(task);
        task.setThreadName(pt.getName());
        //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{
      PrintStream output;
      output=new PrintStream(s.getOutputStream());
      output.println("Hello from "+threadName+".");
      output.flush();
      s.close();
    }catch (Exception e){
    }
  }

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

⌨️ 快捷键说明

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