【Java高级】Java中NIO非阻塞socket通信

来源:互联网 发布:中级程序员资格考试 编辑:程序博客网 时间:2024/05/17 23:39

传统阻塞IO

   
在传统IO通信中,可以分析创建服务器的每个具体步骤。首先创建ServerSocket

ServerSocket server=new ServerSocket(10000);
然后接受新的连接请求 
Socket newConnection=server.accept();//对于accept方法的调用将造成阻塞,直到ServerSocket接受到一个连接请求为止。一旦连接请求被接受,服务器可以读客户socket中的请求。InputStream in = newConnection.getInputStream();InputStreamReader reader = new InputStreamReader(in);BufferedReader buffer = new BufferedReader(reader);Request request = new Request();while(!request.isComplete()) {String line = buffer.readLine();request.addLine(line);}
        这样的操作有两个问题,首先BufferedReader类的readLine()方法在其缓冲区未满时会造成线程阻塞,只有一定数据填满了缓冲区或者客户关闭了套接字,方法才会返回。其次,它回产生大量的垃圾,BufferedReader创建了缓冲区来从客户套接字读入数据,但是同样创建了一些字符串存储这些数据。虽然BufferedReader内部提供了StringBuffer处理这一问题,但是所有的String很快变成了垃圾需要回收。
        同样的问题在发送响应代码中也存在

Response response = request.generateResponse();OutputStream out = newConnection.getOutputStream();InputStream in = response.getInputStream();int ch;while(-1 != (ch = in.read())) {out.write(ch);}newConnection.close();
        类似的,读写操作被阻塞而且向流中一次写入一个字符会造成效率低下,所以应该使用缓冲区,但是一旦使用缓冲,流又会产生更多的垃圾。传统的解决方法通常在Java中处理阻塞I/O要用到线程(大量的线程)。一般是实现一个线程池用来处理请求,但线程使得服务器可以处理多个连接,但是它们也同样引发了许多问题。每个线程拥有自己的栈空间并且占用一些CPU时间,耗费很大,而且很多时间是浪费在阻塞的I/O操作上,没有有效的利用CPU。


非阻塞NIO

1. Buffer
        传统的I/O不断的浪费对象资源(通常是String)。新I/O通过使用Buffer读写数据避免了资源浪费。Buffer对象是线性的,有序的数据集合,它根据其类别只包含唯一的数据类型。
java.nio.Buffer 类描述 
java.nio.ByteBuffer 包含字节类型。 可以从ReadableByteChannel中读在 WritableByteChannel中写 
java.nio.MappedByteBuffer 包含字节类型,直接在内存某一区域映射 
java.nio.CharBuffer 包含字符类型,不能写入通道 
java.nio.DoubleBuffer 包含double类型,不能写入通道 
java.nio.FloatBuffer 包含float类型 
java.nio.IntBuffer 包含int类型 
java.nio.LongBuffer 包含long类型 
java.nio.ShortBuffer 包含short类型 
        可以通过调用allocate(int capacity)方法或者allocateDirect(int capacity)方法分配一个Buffer。特别的,你可以创建MappedBytesBuffer通过调用FileChannel.map(int mode,long position,int size)。直接(direct)buffer在内存中分配一段连续的块并使用本地访问方法读写数据。非直接(nondirect)buffer通过使用Java中的数组访问代码读写数据。有时候必须使用非直接缓冲例如使用任何的wrap方法(如ByteBuffer.wrap(byte[]))在Java数组基础上创建buffer。
2. 字符编码
        向ByteBuffer中存放数据涉及到两个问题:字节的顺序和字符转换。ByteBuffer内部通过ByteOrder类处理了字节顺序问题,但是并没有处理字符转换。事实上,ByteBuffer没有提供方法读写String。
Java.nio.charset.Charset处理了字符转换问题。它通过构造CharsetEncoder和CharsetDecoder将字符序列转换成字节和逆转换。
3. 通道(Channel)
       你可能注意到现有的java.io类中没有一个能够读写Buffer类型,所以NIO中提供了Channel类来读写Buffer。通道可以认为是一种连接,可以是到特定设备,程序或者是网络的连接。
       GatheringByteChannel可以从使用一次将多个Buffer中的数据写入通道,相反的,ScatteringByteChannel则可以一次将数据从通道读入多个Buffer中。你还可以设置通道使其为阻塞或非阻塞I/O操作服务。
为了使通道能够同传统I/O类相容,Channel类提供了静态方法创建Stream或Reader
4. Selector
        在过去的阻塞I/O中,我们一般知道什么时候可以向stream中读或写,因为方法调用直到stream准备好时返回。但是使用非阻塞通道,我们需要一些方法来知道什么时候通道准备好了。在NIO包中,设计Selector就是为了这个目的。SelectableChannel可以注册特定的事件,而不是在事件发生时通知应用,通道跟踪事件。然后,当应用调用Selector上的任意一个selection方法时,它查看注册了的通道看是否有任何感兴趣的事件发生。
        并不是所有的通道都支持所有的操作。SelectionKey类定义了所有可能的操作位,将要用两次。首先,当应用调用SelectableChannel.register(Selector sel,int op)方法注册通道时,它将所需操作作为第二个参数传递到方法中。然后,一旦SelectionKey被选中了,SelectionKey的readyOps()方法返回所有通道支持操作的数位的和。SelectableChannel的validOps方法返回每个通道允许的操作。注册通道不支持的操作将引发IllegalArgumentException异常。下表列出了SelectableChannel子类所支持的操作。
ServerSocketChannel OP_ACCEPT 
SocketChannel OP_CONNECT, OP_READ, OP_WRITE 
DatagramChannel OP_READ, OP_WRITE 
Pipe.SourceChannel OP_READ 
Pipe.SinkChannel OP_WRITE

下面是一个nio的demo

SumServer.java

public class SumServer {private ByteBuffer _buffer = ByteBuffer.allocate(8);private IntBuffer _intBuffer = _buffer.asIntBuffer();private SocketChannel _clientChannel = null;private ServerSocketChannel _serverChannel = null;public void start() {try {openChannel();waitForConnection();} catch (IOException e) {System.err.println(e.toString());}}private void openChannel() throws IOException {_serverChannel = ServerSocketChannel.open();_serverChannel.socket().bind(new InetSocketAddress(10000));_serverChannel.configureBlocking(false);// 设置成为非阻塞模式System.out.println("服务器通道已经打开");}// private void waitForConnection() throws IOException {// while (true) {// _clientChannel = _serverChannel.accept();// if (_clientChannel != null) {// System.out.println("新的连接加入");// processRequest();// _clientChannel.close();// }// System.out.println("buttom");// }// }private void waitForConnection() throws IOException {Selector acceptSelector = SelectorProvider.provider().openSelector();/* * 在服务器套接字上注册selector并设置为接受accept方法的通知。 * 这就告诉Selector,套接字想要在accept操作发生时被放在ready表 上,因此,允许多元非阻塞I/O发生。 */SelectionKey acceptKey = _serverChannel.register(acceptSelector,SelectionKey.OP_ACCEPT);int keysAdded = 0;/* select方法在任何上面注册了的操作发生时返回 */while ((keysAdded = acceptSelector.select()) > 0) {// 某客户已经准备好可以进行I/O操作了,获取其ready键集合System.out.println("wait---1");Set readyKeys = acceptSelector.selectedKeys();Iterator i = readyKeys.iterator();// 遍历ready键集合,并处理加法请求while (i.hasNext()) {System.out.println("wait---2");SelectionKey sk = (SelectionKey) i.next();i.remove();System.out.println("wait---3");ServerSocketChannel nextReady = (ServerSocketChannel) sk.channel();System.out.println("wait---4");// 接受加法请求并处理它_clientChannel = nextReady.accept().socket().getChannel();processRequest();System.out.println("wait---5");_clientChannel.close();System.out.println("waitup");}System.out.println("wait");}}private void processRequest() throws IOException {_buffer.clear();_clientChannel.read(_buffer);int result = _intBuffer.get(0) + _intBuffer.get(1);_buffer.flip();_buffer.clear();_intBuffer.put(0, result);_clientChannel.write(_buffer);}public static void main(String[] args) {new SumServer().start();}} 

SumClient.java

public class SumClient {private ByteBuffer _buffer = ByteBuffer.allocate(8);private IntBuffer _intBuffer;private SocketChannel _channel;public SumClient() {_intBuffer = _buffer.asIntBuffer();} // SumClient constructorpublic int getSum(int first, int second) {int result = 0;try {_channel = connect();sendSumRequest(first, second);result = receiveResponse();} catch (IOException e) {System.err.println(e.toString());} finally {if (_channel != null) {try {_channel.close();} catch (IOException e) {}}}return result;}private SocketChannel connect() throws IOException {InetSocketAddress socketAddress = new InetSocketAddress("localhost",10000);return SocketChannel.open(socketAddress);}private void sendSumRequest(int first, int second) throws IOException {_buffer.clear();_intBuffer.put(0, first);_intBuffer.put(1, second);_channel.write(_buffer);System.out.println("发送加法请求 " + first + "+" + second);}private int receiveResponse() throws IOException {_buffer.clear();_channel.read(_buffer);return _intBuffer.get(0);}public static void main(String[] args) {SumClient sumClient = new SumClient();System.out.println("加法结果为 :" + sumClient.getSum(200, 124));for (int i = 0; i < 10; i++) {SumClient sumClient2 = new SumClient();System.out.println("加法结果为 :" + sumClient2.getSum(0, i));}}}


1 0
原创粉丝点击