隨筆-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);

          ??????????????? // 解析客戶信息,得到遠(yuǎn)程主機和端口。
          ??????????????? 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();
          ??????????? }
          ??????? }

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

          ??????? // 關(guān)閉遠(yuǎ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 崛起的程序員 閱讀(615) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 绵阳市| 长宁区| 仁怀市| 平顶山市| 禹州市| 南通市| 彝良县| 诸城市| 田东县| 阜平县| 丁青县| 武川县| 辽源市| 庄河市| 永州市| 晋宁县| 比如县| 静宁县| 合肥市| 砀山县| 稷山县| 永善县| 广州市| 姜堰市| 积石山| 监利县| 定兴县| 邓州市| 班玛县| 永新县| 青川县| 行唐县| 连云港市| 邻水| 桓仁| 固阳县| 张家界市| 桐梓县| 类乌齐县| 浮山县| 林口县|