隨筆-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>
          ???? * 方法功能:運行代理服務(wù)器 <p>
          ???? * 參數(shù)說明:<p>
          ???? * 返回:void <p>
          ???? * 作者:李明 <p>
          ???? * 日期:2006年3月23日 <p>
          ???? */
          ??? public void runServer()
          ??? {

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

          ??????? // 代理服務(wù)器監(jiān)聽開啟

          ??????? 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總體迭代發(fā)生核心異常\r\n" + e.getMessage());
          ??????????? e.printStackTrace();
          ??????? }

          ??? }

          ??? /**
          ???? * 方法名稱:send <p>
          ???? * 方法功能:發(fā)送給客戶信息 <p>
          ???? * 參數(shù)說明: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);

          ??????????????? // 打印客戶請求代理服務(wù)器信息
          ??????????????? 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();
          ??????????? }
          ??????? }

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

          ??????? // 關(guān)閉遠程和客戶的連接

          ??? }


          ??? 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 崛起的程序員 閱讀(614) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 玛多县| 若羌县| 江山市| 咸阳市| 象州县| 洪湖市| 鸡东县| 轮台县| 密云县| 邵武市| 通渭县| 灯塔市| 济宁市| 赤壁市| 蕉岭县| 宁都县| 武义县| 巴彦淖尔市| 惠安县| 淮北市| 乌兰察布市| 忻城县| 常熟市| 孟村| 盐边县| 武冈市| 镇原县| 冷水江市| 濮阳市| 邯郸县| 东阿县| 阜新市| 遂平县| 高雄市| 仙桃市| 隆德县| 大庆市| 横峰县| 诏安县| 大悟县| 南溪县|