---
title: "Java NIO"
author: "lippeng"
date: "2013-07-16T09:06:18Z"
tags: []
categories: ["企业架构"]
slug: "java-nio"
draft: false
lastmod: "2013-07-16T09:06:18Z"
id: 1907273
url: "https://lippeng.iteye.com/blog/1907273"
---

java 1.4之后，有了NIO，很有意思。。。

 

介绍NIO，需要介绍一些基本的概念：

 

Reactor模式

![](http://dl2.iteye.com/upload/attachment/0086/9689/245f9663-9bd0-3dc7-a5ac-04766329b809.jpg)  
 

 

Selector

Selector（选择器）是Java NIO中能够检测一到多个NIO通道，并能够知晓通道是否为诸如读写事件做好准备的组件。

 

这样，一个单独的线程可以管理多个channel，从而管理多个网络连接。

 

这个组件，是与操作系统相关的，充分利用操作系统API，非常本地化的实现。

 

 

Channel

Java NIO的通道类似流，但又有些不同：

 

既可以从通道中读取数据，又可以写数据到通道。但流的读写通常是单向的。

 

通道可以异步地读写。

 

通道中的数据总是要先读到一个Buffer，或者总是要从一个Buffer中写入。

 

 

Buffer

容量（Capacity ） 

缓冲区能够容纳的数据元素的最大数量。这一容量在缓冲区创建时被设定，并且永远不能被改变。 

 

上界（Limit） 

缓冲区的第一个不能被读或写的元素。或者说，缓冲区中现存元素的计数。 

 

位置（Position ） 

下一个要被读或写的元素的索引。位置会自动由相应的 get(  )和put(  )函数更新。 

 

标记（Mark ） 

一个备忘位置。调用 mark(  )来设定 mark =  postion。调用 reset(  )设定 position  = mark。标记在设定前是未定义的(undefined )。 

 

相等的Buffer

![](http://dl2.iteye.com/upload/attachment/0086/9691/ab471d56-3e62-3ca8-a972-b6365804a0de.png)  
 

不相等的Buffer

![](http://dl2.iteye.com/upload/attachment/0086/9693/ace084f4-ef42-3bfc-a654-46480a5be786.png)  
 

推荐一本书：Java NIO，有中文译本，有电子书。见本文附件。

 

看Demo：

 

Server端：

```
package me.hello.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * God Bless You!
 * Author: Fangniude
 * Date: 2013-07-11
 */
public class NioServer {
    //通道管理器
    private Selector selector;

    /**
     * 获得一个ServerSocket通道，并对该通道做一些初始化的工作
     *
     * @param port 绑定的端口号
     * @throws IOException
     */
    public void initServer(int port) throws IOException {
        // 获得一个ServerSocket通道
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        // 设置通道为非阻塞
        serverChannel.configureBlocking(false);
        // 将该通道对应的ServerSocket绑定到port端口
        serverChannel.socket().bind(new InetSocketAddress(port));
        // 获得一个通道管理器
        this.selector = Selector.open();
        //将通道管理器和该通道绑定，并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后，
        //当该事件到达时，selector.select()会返回，如果该事件没到达selector.select()会一直阻塞。
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);

        System.out.println("Server已经启动，监听端口: " + port + "， 等待客户端注册。。。");
    }

    /**
     * 采用轮询的方式监听selector上是否有需要处理的事件，如果有，则进行处理
     *
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public void listen() throws IOException {
        // 轮询访问selector
        while (true) {
            //当注册的事件到达时，方法返回；否则,该方法会一直阻塞
            selector.select();
            // 获得selector中选中的项的迭代器，选中的项为注册的事件
            Iterator ite = this.selector.selectedKeys().iterator();
            while (ite.hasNext()) {
                SelectionKey key = (SelectionKey) ite.next();
                // 删除已选的key,以防重复处理
                ite.remove();
                // 客户端请求连接事件
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.channel();
                    // 获得和客户端连接的通道
                    SocketChannel channel = server.accept();
                    // 设置成非阻塞
                    channel.configureBlocking(false);

//                    //在这里可以给客户端发送信息哦
//                    channel.write(ByteBuffer.wrap(new String("向客户端发送了一条信息").getBytes()));
                    //在和客户端连接成功之后，为了可以接收到客户端的信息，需要给通道设置读的权限。
                    channel.register(this.selector, SelectionKey.OP_READ);

                    System.out.println("有一个客户端注册上来了。。。");
                    System.out.println("Client:" + channel.getRemoteAddress());
                    System.out.println("Server:" + channel.getLocalAddress());

                    System.out.println("\n等待客户端输入。。。");
                } else if (key.isReadable()) { // 获得了可读的事件
                    read(key);
                }

            }

        }
    }

    /**
     * 处理读取客户端发来的信息 的事件
     *
     * @param key
     * @throws IOException
     */
    public void read(SelectionKey key) throws IOException {
        // 服务器可读取消息:得到事件发生的Socket通道
        SocketChannel channel = (SocketChannel) key.channel();
        // 创建读取的缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(100);
        channel.read(buffer);
        byte[] data = buffer.array();
        String msg = new String(data).trim();
        System.out.println("服务端收到信息：" + msg);
        ByteBuffer outBuffer = ByteBuffer.wrap(("Server已收到刚发送的:" + msg).getBytes());
        channel.write(outBuffer);// 将消息回送给客户端

        System.out.println("\n等待客户端输入。。。");
    }

    /**
     * 启动服务端测试
     *
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        NioServer server = new NioServer();
        server.initServer(8000);
        server.listen();
    }

}
```

 

 

Client端：

 

```
package me.hello.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * God Bless You!
 * Author: Fangniude
 * Date: 2013-07-11
 */
public class NioClient {
    //通道管理器
    private Selector selector;

    private BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

    /**
     * 获得一个Socket通道，并对该通道做一些初始化的工作
     *
     * @param ip   连接的服务器的ip
     * @param port 连接的服务器的端口号
     * @throws IOException
     */
    public void initClient(String ip, int port) throws IOException {
        // 获得一个Socket通道
        SocketChannel channel = SocketChannel.open();
        // 设置通道为非阻塞
        channel.configureBlocking(false);
        // 获得一个通道管理器
        this.selector = Selector.open();

        // 客户端连接服务器,其实方法执行并没有实现连接，需要在listen（）方法中调
        //用channel.finishConnect();才能完成连接
        channel.connect(new InetSocketAddress(ip, port));
        //将通道管理器和该通道绑定，并为该通道注册SelectionKey.OP_CONNECT事件。
        channel.register(selector, SelectionKey.OP_CONNECT);

        System.out.println("已经与Server建立连接。。。。");
        System.out.println("\n请输入要发送的信息：");
    }

    /**
     * 采用轮询的方式监听selector上是否有需要处理的事件，如果有，则进行处理
     *
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public void listen() throws IOException {
        // 轮询访问selector
        while (true) {
            selector.select();
            // 获得selector中选中的项的迭代器
            Iterator ite = this.selector.selectedKeys().iterator();
            while (ite.hasNext()) {
                SelectionKey key = (SelectionKey) ite.next();
                // 删除已选的key,以防重复处理
                ite.remove();
                // 连接事件发生
                if (key.isConnectable()) {
                    SocketChannel channel = (SocketChannel) key.channel();
                    // 如果正在连接，则完成连接
                    if (channel.isConnectionPending()) {
                        channel.finishConnect();

                    }
                    // 设置成非阻塞
                    channel.configureBlocking(false);

                    //在这里可以给服务端发送信息哦
                    String input = sin.readLine();
                    channel.write(ByteBuffer.wrap(input.getBytes()));
                    //在和服务端连接成功之后，为了可以接收到服务端的信息，需要给通道设置读的权限。
                    channel.register(this.selector, SelectionKey.OP_READ);

                    // 获得了可读的事件
                } else if (key.isReadable()) {
                    read(key);
                }

            }

        }
    }

    /**
     * 处理读取服务端发来的信息 的事件
     *
     * @param key
     * @throws IOException
     */
    public void read(SelectionKey key) throws IOException {
        // 得到事件发生的Socket通道
        SocketChannel channel = (SocketChannel) key.channel();
        // 创建读取的缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(100);
        channel.read(buffer);
        byte[] data = buffer.array();
        String msg = new String(data).trim();
        System.out.println(msg);

        System.out.println("\n请输入要发送的信息：");
        String input = sin.readLine();
        ByteBuffer outBuffer = ByteBuffer.wrap(input.getBytes());
        channel.write(outBuffer);
    }

    /**
     * 启动客户端测试
     *
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        NioClient client = new NioClient();
        client.initClient("localhost", 8000);
        client.listen();
    }

}
```