jdk1.4 構建 java多線程,併發設計框架 使用列子(一)

網絡 Socket監聽服務器
  1. import java.io.IOException;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. /**
  5.  * Socket監聽服務器
  6.  * @author guishuanglin 2008-11-3
  7.  *
  8.  */
  9. public class SocketListenServe implements Runnable{
  10.     private ServerSocket ss;
  11.     private boolean listening = true;
  12.     private int port = 10001;
  13.     //最大隊列長度被設置爲 backlog 參數
  14.     private int backlog = 20;
  15.     private Class client;
  16.     //讀取客戶端數據本地緩衝
  17.     private int cacheBtye=1024;
  18.     private int requestCount=100000;
  19.     public SocketListenServe(IProcessClientData pClient) {
  20.         client = pClient.getClass();
  21.     }
  22.     public SocketListenServe(int cacheBtye,IProcessClientData pClient) {
  23.         this.cacheBtye = cacheBtye;
  24.         client = pClient.getClass();
  25.     }
  26.     public SocketListenServe(int port,int backlog,int cacheBtye,IProcessClientData pClient) {
  27.         this.port = port;
  28.         this.backlog = backlog;
  29.         this.cacheBtye = cacheBtye;
  30.         client = pClient.getClass();
  31.         
  32.     }
  33.     public void run() {
  34.         Init();// 初始化
  35.         Listen();// 啓動監聽
  36.     }
  37.     /**
  38.      * 初始化
  39.      * @date 2008-11-3
  40.      */
  41.     public void Init() {
  42.         try {
  43.             ss = new ServerSocket(port, backlog);
  44.             System.out.println("服務已經啓動,開始在端口["+port+"]監聽,等待TCP客戶端連接....");
  45.         } catch (IOException ie) {
  46.             System.out.println("無法啓動監聽,可能由於網絡不通,或端口["+port+"]已被佔用.");
  47.             ie.printStackTrace();
  48.             System.exit(0);
  49.         }
  50.     }
  51.     /**
  52.      * 啓動監聽
  53.      * @date 2008-11-3
  54.      */
  55.     public void Listen() {
  56.         while (listening) {
  57.             try {
  58.                 Socket s = ss.accept();
  59.                 System.out.println("收到客戶端["+ s.getInetAddress().getHostAddress() + "]連接請求.");
  60.                 new Thread(new ProcessRequest(s,this.getRequestCount(),cacheBtye,client)).start();
  61.             } catch (IOException ie) {
  62.                 System.out.println("服務器異常");
  63.                 ie.printStackTrace();
  64.                 
  65.                 //碰到異常關閉服務重新打開
  66.                 this.ReleaseResource();
  67.                 this.Init();
  68.             }catch (Exception ex) {
  69.                 System.out.println("服務器異常");
  70.                 ex.printStackTrace();
  71.                 
  72.                 this.ReleaseResource();
  73.                 this.Init();
  74.             }
  75.         }
  76.     }
  77.     /**
  78.      * 關閉服務實例
  79.      * @date 2008-11-3
  80.      */
  81.     public void ReleaseResource(){
  82.         try {
  83.             if (ss != null && !ss.isClosed())
  84.                 ss.close();
  85.             //關閉服務後睡眠5s.
  86.             Thread.sleep(5000);
  87.         } catch (IOException e) {
  88.             e.printStackTrace();
  89.         } catch (InterruptedException e) {
  90.             e.printStackTrace();
  91.         }
  92.     }
  93.     public int getRequestCount(){
  94.         //重複計數
  95.         if(this.requestCount >1000000){
  96.             this.requestCount=0;
  97.         }
  98.         return this.requestCount++;
  99.     }
  100.     public static void main(String args[]) {
  101.         //ProcessClientDataImpl client = new ProcessClientDataImpl();
  102.         //SocketListenServe servie = new SocketListenServe(client);
  103.     }
  104. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章