首页 > java基础 > 利用NIO非阻塞方式制作浏览器代理

利用NIO非阻塞方式制作浏览器代理

2009年6月28日

下面代码的大致思路是:传入一个socketServer得到的socket的,然后再建立一条到其他代理服务器的socket的,接着就是按照顺序来回倒数据,只要读入的数据不是-1,那么就有可能再读到数据,这2条通路的任何一条通路读入数据为-1,那么就结束。其次还有退出循环的可能就是,在读数据的时候发生异常,有2种情况会产生这种异常,要么浏览器认为没有数据可以读了,要么代理服务器那里认为没有数据可以写过来了,那么它们就会close它们各自的socket,此时我们再尝试读socket,就会报错:socket已经被断开。

  1. import java.io.IOException;
  2. import java.net.InetSocketAddress;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.SocketChannel;
  5. import java.nio.charset.Charset;
  6.  
  7. /**
  8. * 提供一种非阻塞方式的网络连接,性能提高
  9. */
  10. public class SocketNioHandler {
  11.  
  12.     private InetSocketAddress endpoint;
  13.  
  14.     public SocketNioHandler() {
  15.     }
  16.  
  17.     public SocketNioHandler(InetSocketAddress endpoint) {
  18.         this.endpoint = endpoint;
  19.     }
  20.  
  21.     /**
  22.      * 此方法是线程安全的
  23.      */
  24.     @Override
  25.     public boolean process(SocketChannel socket) {
  26.         SocketChannel outSocket = null;
  27.         ByteBuffer buff = ByteBuffer.allocate(1024);
  28.  
  29.         try {
  30.             outSocket = SocketChannel.open(endpoint);
  31.             outSocket.configureBlocking(false);
  32.  
  33.             while (true) {
  34.  
  35.                 if (socket.read(buff) < 0) {
  36.                     break;
  37.                 } else {
  38.                     buff.flip();
  39.                     if (buff.limit() > 10)
  40.                         System.out.println(Charset.defaultCharset()
  41.                                 .decode(buff));
  42.                     buff.rewind();
  43.                     while (buff.remaining() != 0) {
  44.                         outSocket.write(buff);
  45.                     }
  46.                     buff.clear();
  47.                 }
  48.  
  49.                 if (outSocket.read(buff) < 0) {
  50.                     break;
  51.                 } else {
  52.                     buff.flip();
  53.                     while (buff.remaining() != 0) {
  54.                         socket.write(buff);
  55.                     }
  56.                     buff.clear();
  57.                 }
  58.  
  59.                 try {
  60.                     Thread.sleep(1);
  61.                 } catch (InterruptedException e) {
  62.                     ;
  63.                 }
  64.             }
  65.  
  66.         } catch (IOException e) {
  67.             ;
  68.         } finally {
  69.             try {
  70.                 socket.close();
  71.             } catch (IOException e) {
  72.                 e.printStackTrace();
  73.             }
  74.             try {
  75.                 outSocket.close();
  76.             } catch (IOException e) {
  77.                 e.printStackTrace();
  78.             }
  79.  
  80.         }
  81.  
  82.         return true;
  83.     }
  84.  
  85. }

java基础

  1. 目前还没有任何评论.
  1. 目前还没有任何 trackbacks 和 pingbacks.