<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    ??? /**
    ???? * 方法名稱:runServer<p>
    ???? * 方法功能:運行代理服務(wù)器<p>
    ???? * 參數(shù)說明: <p>
    ???? * 返回:void <p>
    ???? * 作者:李明
    ???? * 日期:2006年3月9日
    ???? * @throws IOException
    ???? */
    ??? public void runServer() throws IOException
    ??? {
    ??????? // 創(chuàng)建代理服務(wù)器監(jiān)聽,端口默認(rèn)為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();

    ??????????????? // 連接真實的服務(wù)器,如果不能連接成功,將向客戶發(fā)送錯誤信息,斷開本次連接
    ??????????????? // 并且繼續(xù)等待連接.
    ??????????????? 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;
    ??????????????? }

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

    ??????????????? // 構(gòu)建一個單獨線程讀客戶請求并傳給服務(wù)器,此線程為異步.
    ??????????????? 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();
    ??????????????????????????? }
    ??????????????????????????? // 打印客戶瀏覽器發(fā)來的信息
    ??????????????????????????? log.info("#################S##############");
    ??????????????????????????? log.info(context);
    ??????????????????????????? log.info("#################E##############");
    ??????????????????????????? // 解析發(fā)來的信息,獲取請求的主機地址和端口.
    ??????????????????????????? proxyUtil.setUrl(context);
    ??????????????????????????? host = proxyUtil.getHost();
    ??????????????????????????? remoteport = proxyUtil.getPort();
    ??????????????????????????? log.info("host : '" + host + "'" + " port : '" + remoteport + "'");

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

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

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

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

    ??????????????? // 關(guān)閉客戶連接.
    ??????????????? 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 崛起的程序員 閱讀(256) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: h视频免费高清在线观看| 桃子视频在线观看高清免费完整| 亚洲国产精品日韩在线| 亚洲无线一二三四区手机| 18以下岁毛片在免费播放| 人人爽人人爽人人片av免费| 亚洲国产成人久久| 免费国产a理论片| 亚洲国产成人99精品激情在线| 成年免费大片黄在线观看岛国 | 香蕉97碰碰视频免费| 亚洲成人高清在线观看| 亚洲精品国产精品乱码不卡√| 日日夜夜精品免费视频| 午夜免费1000部| 外国成人网在线观看免费视频| 四虎影视永久在线精品免费| 亚洲愉拍一区二区三区| 亚洲AV成人潮喷综合网| 成人免费无码大片a毛片软件| 亚洲精品视频免费在线观看| 国产免费一区二区三区在线观看| 午夜亚洲乱码伦小说区69堂| 国产亚洲玖玖玖在线观看| 亚洲熟妇av一区二区三区下载| 久久久久亚洲AV无码专区首| 国产亚洲精品久久久久秋霞| 亚洲国产香蕉人人爽成AV片久久| 99蜜桃在线观看免费视频网站| a色毛片免费视频| 日本一区二区三区在线视频观看免费| 亚洲精品456人成在线| 无码乱人伦一区二区亚洲一| 成人免费大片免费观看网站| 两个人的视频www免费| 国产亚洲精品美女久久久久 | 亚洲人成网站在线观看青青| 免费人成视频x8x8入口| 久久久久久99av无码免费网站 | 亚洲天堂一区在线| 亚洲精品色午夜无码专区日韩|