隨筆-88  評論-77  文章-48  trackbacks-0

          ??? /**
          ???? * 方法名稱:runServer<p>
          ???? * 方法功能:運行代理服務器<p>
          ???? * 參數說明: <p>
          ???? * 返回:void <p>
          ???? * 作者:李明
          ???? * 日期:2006年3月9日
          ???? * @throws IOException
          ???? */
          ??? public void runServer() throws IOException
          ??? {
          ??????? // 創建代理服務器監聽,端口默認為9999(可以通過屬性文檔修改其他端口)
          ??????? ServerSocket ss = new ServerSocket(localport);
          ??????? final com.zte.ums.zxnm01.cpe.common.http.ProxyUtil proxyUtil = new com.zte.ums.zxnm01.cpe.common.http.ProxyUtil();
          ??????? final byte[] request = new byte[1024];
          ??????? byte[] reply = new byte[4096];

          ??????? while(true)
          ??????? {
          ??????????? Socket client = null, server = null;
          ??????????? try
          ??????????? {
          ??????????????? // 等待客戶連接
          ??????????????? client = ss.accept();
          ??????????????? log.info("*******Client logon*******");
          ??????????????? log.info(client);
          ??????????????? final InputStream streamFromClient = client.getInputStream();
          ??????????????? final OutputStream streamToClient = client.getOutputStream();

          ??????????????? // 連接真實的服務器,如果不能連接成功,將向客戶發送錯誤信息,斷開本次連接
          ??????????????? // 并且繼續等待連接.
          ??????????????? try
          ??????????????? {
          ??????????????????? server = new Socket(host, remoteport);
          ??????????????? }
          ??????????????? catch(IOException e)
          ??????????????? {
          ??????????????????? PrintWriter out = new PrintWriter(streamToClient);
          ??????????????????? out.print("Proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e + "\n");
          ??????????????????? out.flush();
          ??????????????????? client.close();
          ??????????????????? continue;
          ??????????????? }

          ??????????????? // 得到連接服務器的輸入輸出流.
          ??????????????? final InputStream streamFromServer = server.getInputStream();
          ??????????????? final OutputStream streamToServer = server.getOutputStream();

          ??????????????? // 構建一個單獨線程讀客戶請求并傳給服務器,此線程為異步.
          ??????????????? Thread t = new Thread()
          ??????????????? {
          ??????????????????? public void run()
          ??????????????????? {
          ??????????????????????? int bytesRead;
          ??????????????????????? String context = null;
          ??????????????????????? try
          ??????????????????????? {
          ??????????????????????????? while((bytesRead = streamFromClient.read(request)) != -1)
          ??????????????????????????? {
          ??????????????????????????????? streamToServer.write(request, 0, bytesRead);
          ??????????????????????????????? context = new String(request).trim();
          ??????????????????????????????? streamToServer.flush();
          ??????????????????????????? }
          ??????????????????????????? // 打印客戶瀏覽器發來的信息
          ??????????????????????????? log.info("#################S##############");
          ??????????????????????????? log.info(context);
          ??????????????????????????? log.info("#################E##############");
          ??????????????????????????? // 解析發來的信息,獲取請求的主機地址和端口.
          ??????????????????????????? proxyUtil.setUrl(context);
          ??????????????????????????? host = proxyUtil.getHost();
          ??????????????????????????? remoteport = proxyUtil.getPort();
          ??????????????????????????? log.info("host : '" + host + "'" + " port : '" + remoteport + "'");

          ??????????????????????? }
          ??????????????????????? catch(IOException e)
          ??????????????????????? {
          ??????????????????????????? // e.printStackTrace();
          ??????????????????????? }

          ??????????????????????? // 關閉請求服務器連接,屏蔽無意義的異常,減少代理服務器負擔.
          ??????????????????????? try
          ??????????????????????? {
          ??????????????????????????? streamToServer.close();
          ??????????????????????? }
          ??????????????????????? catch(IOException e)
          ??????????????????????? {
          ??????????????????????????? // e.printStackTrace();
          ??????????????????????? }
          ??????????????????? }
          ??????????????? };

          ??????????????? // 開啟客戶端到服務器請求線程
          ??????????????? t.start();

          ??????????????? // 讀服務器的請求,成功后返回信息給客戶端.
          ??????????????? int bytesRead;
          ??????????????? try
          ??????????????? {
          ??????????????????? while((bytesRead = streamFromServer.read(reply)) != -1)
          ??????????????????? {
          ??????????????????????? streamToClient.write(reply, 0, bytesRead);
          ??????????????????????? streamToClient.flush();
          ??????????????????? }
          ??????????????? }
          ??????????????? catch(IOException e)
          ??????????????? {
          ??????????????????? // e.printStackTrace();
          ??????????????? }

          ??????????????? // 關閉客戶連接.
          ??????????????? streamToClient.close();
          ??????????? }
          ??????????? catch(IOException e)
          ??????????? {
          ??????????????? System.err.println(e);
          ??????????? }
          ??????????? finally
          ??????????? {
          ??????????????? try
          ??????????????? {
          ??????????????????? if(server != null)
          ??????????????????? {
          ??????????????????????? server.close();
          ??????????????????? }
          ??????????????????? if(client != null)
          ??????????????????? {
          ??????????????????????? client.close();
          ??????????????????? }
          ??????????????? }
          ??????????????? catch(IOException e)
          ??????????????? {
          ??????????????????? e.printStackTrace();
          ??????????????? }
          ??????????? }
          ??????? }
          ??? }

          posted on 2006-04-28 11:24 崛起的程序員 閱讀(250) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 杨浦区| 扎兰屯市| 扎赉特旗| 嘉峪关市| 富蕴县| 兴海县| 秦皇岛市| 隆安县| 新邵县| 穆棱市| 新宾| 汶上县| 美姑县| 福泉市| 勐海县| 墨竹工卡县| 荥阳市| 含山县| 醴陵市| 得荣县| 新邵县| 庆安县| 尚义县| 庆城县| 连州市| 常熟市| 广河县| 湘潭县| 新干县| 松溪县| 南涧| 开平市| 安福县| 天津市| 岑溪市| 特克斯县| 和平县| 漠河县| 襄樊市| 锦屏县| 新源县|