隨筆-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 崛起的程序員 閱讀(251) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 公安县| 新邵县| 新源县| 南通市| 买车| 罗源县| 华容县| 孝义市| 夹江县| 湟中县| 乐东| 磴口县| 固始县| 仁寿县| 崇左市| 凤庆县| 合山市| 许昌县| 兴化市| 河津市| 沙湾县| 天水市| 来宾市| 古浪县| 广东省| 阿拉善左旗| 临颍县| 太谷县| 会同县| 黄山市| 云林县| 牟定县| 满洲里市| 九龙县| 陈巴尔虎旗| 伊川县| 德昌县| 大洼县| 建宁县| 红安县| 固始县|