<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

    public class ProxyServer
    {

    ??? private static final int PORT = 9999;

    ??? private ByteBuffer buffer = ByteBuffer.allocate(10240);

    ??? private ProxyUtil proxyUtil = new ProxyUtil();

    ??? private static String remoteHost = "";

    ??? private static int remotePort = 80;

    ??? private Logger log = ZXLogger.getLogger(ProxyServer.class.getName());

    ??? //private Log log = LogFactory.getLog(ProxyServer.class);

    ??? public ProxyServer()
    ??? {
    ??????? //PropertyConfigurator.configure("src/log4j.properties");
    ??? }

    ??? /**
    ???? * 方法名稱:start <p>
    ???? * 方法功能:運行代理服務器 <p>
    ???? * 參數說明:<p>
    ???? * 返回:void <p>
    ???? * 作者:李明 <p>
    ???? * 日期:2006年3月23日 <p>
    ???? */
    ??? public void runServer()
    ??? {

    ??????? ServerSocket sSocket = null;
    ??????? ServerSocketChannel ssc = null;

    ??????? // 代理服務器監聽開啟

    ??????? Selector selector = null;
    ??????? try
    ??????? {
    ??????????? ssc = ServerSocketChannel.open();
    ??????????? sSocket = ssc.socket();
    ??????????? sSocket.bind(new InetSocketAddress(PORT));

    ??????????? selector = Selector.open();

    ??????????? System.err.println("Listening Port is " + PORT);

    ??????????? ssc.configureBlocking(false);
    ??????????? ssc.register(selector, SelectionKey.OP_ACCEPT);
    ??????? }
    ??????? catch(ClosedChannelException e1)
    ??????? {
    ??????????? // TODO Auto-generated catch block
    ??????????? e1.printStackTrace();
    ??????? }
    ??????? catch(IOException e1)
    ??????? {
    ??????????? // TODO Auto-generated catch block
    ??????????? e1.printStackTrace();
    ??????? }

    ??????? try
    ??????? {
    ??????????? while(true)
    ??????????? {

    ??????????????? int n = selector.select();

    ??????????????? if(n == 0)
    ??????????????? {
    ??????????????????? continue; // nothing to do
    ??????????????? }

    ??????????????? Set set = selector.selectedKeys();

    ??????????????? Iterator iter = set.iterator();

    ??????????????? while(iter.hasNext())
    ??????????????? {
    ??????????????????? SelectionKey key = (SelectionKey)iter.next();

    ??????????????????? if(key.isAcceptable())
    ??????????????????? {
    ??????????????????????? ServerSocketChannel svrSocketChannel = (ServerSocketChannel)key.channel();
    ??????????????????????? SocketChannel clientChannel = null;
    ??????????????????????? try
    ??????????????????????? {
    ??????????????????????????? clientChannel = svrSocketChannel.accept();
    ??????????????????????? }
    ??????????????????????? catch(IOException e)
    ??????????????????????? {
    ??????????????????????????? // TODO Auto-generated catch block
    ??????????????????????????? if(clientChannel != null)
    ??????????????????????????? {

    ??????????????????????????????? clientChannel.close();
    ??????????????????????????? }
    ??????????????????????????? if(key != null)
    ??????????????????????????? {
    ??????????????????????????????? key.cancel();
    ??????????????????????????? }
    ??????????????????????? }

    ??????????????????????? send(clientChannel);

    ??????????????????? }
    ??????????????????? iter.remove();
    ??????????????????? log.info("************SEND? END*************");
    ??????????????? }
    ??????????? }
    ??????? }
    ??????? catch(IOException e)
    ??????? {
    ??????????? // TODO Auto-generated catch block
    ??????????? log.info("ServerSocketChannel總體迭代發生核心異常\r\n" + e.getMessage());
    ??????????? e.printStackTrace();
    ??????? }

    ??? }

    ??? /**
    ???? * 方法名稱:send <p>
    ???? * 方法功能:發送給客戶信息 <p>
    ???? * 參數說明:SocketChannel, String <p>
    ???? * 返回:void <p>
    ???? * 作者:李明 <p>
    ???? * 日期:2006年3月23日 <p>
    ???? *
    ???? * @param channel
    ???? * @param key
    ???? * @throws IOException
    ???? */
    ??? public void send(SocketChannel clientChannel)
    ??? {

    ??????? SocketChannel remoteSvrChannel = null;

    ??????? buffer.clear();
    ??????? int count;

    ??????? log.info("************SEND START*************");

    ??????? try
    ??????? {
    ??????????? while((count = clientChannel.read(buffer)) > 0)
    ??????????? {
    ??????????????? buffer.flip();

    ??????????????? byte[] bytebuffer = new byte[count];
    ??????????????? buffer.get(bytebuffer, 0, count);

    ??????????????? ByteBuffer bufferWrite = ByteBuffer.wrap(bytebuffer);

    ??????????????? byte[] b = new byte[bufferWrite.limit()];
    ??????????????? bufferWrite.get(b);
    ??????????????? String context = new String(b);

    ??????????????? // 打印客戶請求代理服務器信息
    ??????????????? log.info(context);

    ??????????????? // 解析客戶信息,得到遠程主機和端口。
    ??????????????? proxyUtil.setUrl(context);
    ??????????????? setRemoteHost(proxyUtil.getHost());
    ??????????????? setRemotePort(proxyUtil.getPort());

    ??????????????? log.info("Remote Host " + getRemoteHost());
    ??????????????? log.info("Remote Port " + getRemotePort());

    ??????????????? if(remoteSvrChannel == null)
    ??????????????? {
    ??????????????????? InetSocketAddress inet = new InetSocketAddress(getRemoteHost(), getRemotePort());
    ??????????????????? try
    ??????????????????? {
    ??????????????????????? remoteSvrChannel = SocketChannel.open(inet);
    ??????????????????????? // remoteSvrChannel.configureBlocking(false);
    ??????????????????????? // Socket remoteSvr = remoteSvrChannel.socket();
    ??????????????????????? // remoteSvr.setSoTimeout(1000);
    ??????????????????????? bufferWrite.flip();
    ??????????????????????? remoteSvrChannel.write(bufferWrite);
    ??????????????????????? buffer.clear();
    ??????????????????????? int n;
    ??????????????????????? while((n = remoteSvrChannel.read(buffer)) > 0)
    ??????????????????????? {
    ??????????????????????????? log.info("n=" + n);
    ??????????????????????????? buffer.flip();
    ??????????????????????????? log.info("buffer.limit=" + buffer.limit());
    ??????????????????????????? clientChannel.write(buffer);
    ??????????????????????? }
    ??????????????????? }
    ??????????????????? catch(java.nio.channels.UnresolvedAddressException ex)
    ??????????????????? {
    ??????????????????????? log.info("主機地址訪問無效\r\n" + ex.getMessage());
    ??????????????????? }
    ??????????????????? catch(IOException e)
    ??????????????????? {
    ??????????????????????? // TODO Auto-generated catch block
    ??????????????????????? try
    ??????????????????????? {
    ??????????????????????????? if(remoteSvrChannel != null)
    ??????????????????????????? {
    ??????????????????????????????? remoteSvrChannel.close();
    ??????????????????????????? }
    ??????????????????????? }
    ??????????????????????? catch(IOException e1)
    ??????????????????????? {
    ??????????????????????????? // TODO Auto-generated catch block
    ??????????????????????????? e1.printStackTrace();
    ??????????????????????? }
    ??????????????????? }
    ??????????????? }
    ??????????? }
    ??????? }
    ??????? catch(IOException e1)
    ??????? {
    ??????????? // TODO Auto-generated catch block
    ??????????? try
    ??????????? {
    ??????????????? if(clientChannel != null)
    ??????????????? {
    ??????????????????? clientChannel.close();
    ??????????????? }
    ??????????? }
    ??????????? catch(IOException e)
    ??????????? {
    ??????????????? // TODO Auto-generated catch block
    ??????????????? e.printStackTrace();
    ??????????? }
    ??????? }

    ??????? finally
    ??????? {
    ??????????? try
    ??????????? {
    ??????????????? if(remoteSvrChannel != null)
    ??????????????? {
    ??????????????????? remoteSvrChannel.close();
    ??????????????? }
    ??????????????? if(clientChannel != null)
    ??????????????? {
    ??????????????????? clientChannel.close();
    ??????????????? }
    ??????????? }
    ??????????? catch(IOException e)
    ??????????? {
    ??????????????? // TODO Auto-generated catch block
    ??????????????? e.printStackTrace();
    ??????????? }
    ??????? }

    ??????? // 打印遠程服務器返回給客戶端信息
    //??????? buffer.flip();
    //??????? byte[] b2 = new byte[buffer.limit()];
    //??????? buffer.get(b2);
    //??????? log.info(new String(b2));

    ??????? // 關閉遠程和客戶的連接

    ??? }


    ??? public static String getRemoteHost()
    ??? {
    ??????? return remoteHost;
    ??? }

    ??? public static void setRemoteHost(String remoteHost)
    ??? {
    ??????? ProxyServer.remoteHost = remoteHost;
    ??? }

    ??? public static int getRemotePort()
    ??? {
    ??????? return remotePort;
    ??? }

    ??? public static void setRemotePort(int remotePort)
    ??? {
    ??????? ProxyServer.remotePort = remotePort;
    ??? }

    ??? public ByteBuffer getBuffer()
    ??? {
    ??????? return buffer;
    ??? }

    ??? public void setBuffer(ByteBuffer buffer)
    ??? {
    ??????? this.buffer = buffer;
    ??? }

    ??? /**
    ???? * @param args
    ???? */
    ??? public static void main(String[] args)
    ??? {
    ??????? // TODO Auto-generated method stub
    ??????? ProxyServer p = new ProxyServer();
    ??????? p.runServer();
    ??? }
    }

    posted on 2006-04-28 11:26 崛起的程序員 閱讀(610) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 精品亚洲视频在线观看| 国产亚洲精品国产福利在线观看| 亚洲国产精品尤物YW在线观看| 麻豆一区二区免费播放网站 | 免费能直接在线观看黄的视频| eeuss影院ss奇兵免费com| 亚洲精品无码mⅴ在线观看| 亚洲综合小说久久另类区| 亚洲乳大丰满中文字幕| 亚洲国产精品狼友中文久久久| 成年女人午夜毛片免费视频| **真实毛片免费观看| 亚洲一区免费观看| 很黄很污的网站免费| 三年片免费高清版| 久久国产精品免费一区二区三区| 黄色a级片免费看| 国产精品亚洲专区无码唯爱网| 在线观看日本亚洲一区| 亚洲18在线天美| 国产成人精品亚洲2020| 亚洲制服丝袜一区二区三区| 亚洲精品自拍视频| 亚洲成人免费在线观看| 亚洲日韩乱码久久久久久| 麻豆亚洲AV永久无码精品久久 | 国产免费无码AV片在线观看不卡| 一个人看的www免费高清| 一进一出60分钟免费视频| 午夜不卡AV免费| ssswww日本免费网站片| 一区二区免费电影| 中文字幕a∨在线乱码免费看 | 亚洲午夜福利精品久久| 亚洲裸男gv网站| 亚洲午夜福利AV一区二区无码| 中文字幕在线亚洲精品| 亚洲AV无码日韩AV无码导航| 亚洲国产香蕉碰碰人人| 亚洲国产综合在线| 亚洲精品中文字幕|