一 前言
简介:
NIO我们一般认为是New I/O;也称之为Non-block I/O,即非阻塞I/O。NIO提供了与传统BIO模型中的Socket和ServerSocket相对应的SocketChannel和ServerSocketChannel两种不同的套接字通道实现。新增的这两种通道都支持阻塞和非阻塞两种模式
缓冲区 Buffer:
Buffer是一个对象,包含一些要写入或者读出的数据。在NIO库中,所有数据都是用缓冲区处理的。在读取数据时,它是直接读到缓冲区中的;在写入数据时,也是写入到缓冲区中。任何时候访问NIO中的数据,都是通过缓冲区进行操作。缓冲区实际上是一个数组,并提供了对数据结构化访问以及维护读写位置等信息。
具体的缓存区有这些:ByteBuffe、CharBuffer、 ShortBuffer、IntBuffer、LongBuffer、FloatBuffer、DoubleBuffer。他们实现了相同的接口:Buffer。
上代码看注释
二 Java代码
1.服务端
package dcocd.netty.tool.io.nio.server;
/**
* NioServer 服务启动类
*
* @author guohongjun@dcocd.cn
* @Date 2020/5/12 17:48
*/
public class NioServer {
/**
* 默认服务端口
*/
private static int DEFAULT_PORT = 12345;
private static NioServerHandler nioServerHandler;
/**
* 启动 NioServer
*
* @param port 端口
*/
public static synchronized void start(int port) {
if (nioServerHandler != null) {
nioServerHandler.stop();
}
nioServerHandler = new NioServerHandler(port);
new Thread(nioServerHandler, "nio-server-" + port).start();
}
/**
* 以默认端口启动
*/
public static void start() {
start(DEFAULT_PORT);
}
/**
* 启动一个 NioServer 的主线程方法
*
* @param args
*/
public static void main(String[] args) {
start();
}
}
2.服务端处理器
package dcocd.netty.tool.io.nio.server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.nio.charset.Charset;
import java.util.Iterator;
/**
* NIO 服务端
* 创建 NIO 服务端的主要步骤如下:
* 1.打开 ServerSocketChannel,监听客户端连接
* 2.绑定监听端口,设置连接为非阻塞模式
* 3.创建 Reactor 线程,创建多路复用器并启动线程
* 4.将 ServerSocketChannel 注册到 Reactor 线程中的 Selector 上,监听 ACCEPT 事件
* 5.Selector 轮询准备就绪的key
* 6.Selector 监听到新的客户端接入,处理新的接入请求,完成 TCP 三次握手,简历物理链路
* 7.设置客户端链路为非阻塞模式
* 8.将新接入的客户端连接注册到 Reactor 线程的 Selector 上,监听读操作,读取客户端发送的网络消息
* 9.异步读取客户端消息到缓冲区
* 10.对 Buffer 编解码,处理半包消息,将解码成功的消息封装成Task
* 11.将应答消息编码为 Buffer,调用 SocketChannel 的 write 将消息异步发送给客户端
*
* @author guohongjun@dcocd.cn
* @Date 2020/5/12 16:50
*/
public class NioServerHandler implements Runnable {
private static Logger logger = LoggerFactory.getLogger(NioServerHandler.class);
/**
* 选择器
*/
private Selector selector;
/**
* 监听通道
*/
private ServerSocketChannel serverChannel;
/**
* 服务器是否开启
*/
private volatile boolean started;
/**
* 构造方法
*
* @param port 指定要监听的端口号
*/
public NioServerHandler(int port) {
try {
// 创建选择器
selector = Selector.open();
// 打开监听通道
serverChannel = ServerSocketChannel.open();
// 如果为 true,则此通道将被置于阻塞模式;如果为 false,则此通道将被置于非阻塞模式
serverChannel.configureBlocking(false);
// 绑定端口 backlog(请求的队列最大长度)设为 1024
serverChannel.socket().bind(new InetSocketAddress(port), 1024);
// 监听客户端连接请求
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
// 标记服务器已开启
started = true;
logger.info("NioServer is start successful on port " + port);
} catch (IOException e) {
logger.error("NioServerHandler create error on port " + port + " : " + e.getMessage());
}
}
/**
* 停止服务器
*/
public void stop() {
started = false;
}
@Override
public void run() {
// 循环遍历 selector
while (started) {
// 无论是否有读写事件发生,selector每隔1s被唤醒一次
try {
selector.select(1000);
/*// 阻塞,只有当至少一个注册的事件发生的时候才会继续.
selector.select();*/
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
SelectionKey selectionKey = null;
while ((iterator.hasNext())) {
selectionKey = iterator.next();
iterator.remove();
try {
// 创建该密钥的通道,完成TCP三次握手,建立TCP物理链路,完成消息的接收和应答
this.handleInput(selectionKey);
} catch (IOException e) {
logger.error("NioServer and NioClient TCP error : " + e.getMessage());
if (selectionKey != null) {
// 取消此密钥的通道与其选择器注册
selectionKey.cancel();
if (selectionKey.channel() != null) {
// 关闭此密钥的通道
selectionKey.channel().close();
}
}
}
}
} catch (IOException e) {
logger.error("NioServerHandler run() error : " + e.getMessage());
}
}
// selector关闭后会自动释放里面管理的资源
if (selector != null) {
try {
selector.close();
} catch (IOException e) {
logger.error("NioServerHandler run() Selector close error : " + e.getMessage());
}
}
}
/**
* 创建该密钥的通道,完成TCP三次握手,建立TCP物理链路,完成消息的接收和应答
*
* @param selectionKey 密钥
* @throws IOException
*/
private void handleInput(SelectionKey selectionKey) throws IOException {
// 判断密钥是否有效
if (selectionKey.isValid()) {
// 处理新接入的请求消息, 判断此密钥的通道是否准备好接受新套接字连接。
if (selectionKey.isAcceptable()) {
// 获取此密钥的通道
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
// 通过 ServerSocketChannel 的 accept 创建 SocketChannel 实例
// 完成该操作意味着完成TCP三次握手,TCP物理链路正式建立
SocketChannel socketChannel = serverSocketChannel.accept();
// 设置为非阻塞的
socketChannel.configureBlocking(false);
// 注册为读
socketChannel.register(selector, SelectionKey.OP_READ);
}
// 处理读消息
if (selectionKey.isReadable()) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
// 创建ByteBuffer,并开辟一个1M的缓冲区
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
// 读取请求码流,返回读取到的字节数
int readBytes = socketChannel.read(readBuffer);
// 读取字节,对字节进行编解码
if (readBytes > 0) {
// 将缓冲区当前的limit设置为position=0,用于后续对缓冲区的读取操作
readBuffer.flip();
// 根据缓冲区可读字节数创建字节数组
byte[] bytes = new byte[readBuffer.remaining()];
// 将缓冲区可读字节数组复制到新建的数组中
readBuffer.get(bytes);
String message = new String(bytes, Charset.forName("UTF-8"));
logger.info("NioServer received message from NioClient : " + message);
// 发送应答消息
this.handleOutput(socketChannel, message);
// 链路已经关闭,释放资源
} else if (readBytes < 0) {
// 取消此密钥的通道与其选择器注册
selectionKey.cancel();
// 关闭此密钥的通道
socketChannel.close();
} else if (readBytes == 0) {
/*********没有读取到字节 忽略**********/
}
}
}
}
/**
* 异步发送应答消息
*
* @param socketChannel 需要发送应答消息的密钥的通道
* @param response 应答消息
*/
private void handleOutput(SocketChannel socketChannel, String response) throws IOException {
// 将消息编码为字节数组
byte[] bytes = response.getBytes();
// 根据数组容量创建ByteBuffer
ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
// 将字节数组复制到缓冲区
writeBuffer.put(bytes);
// flip 操作,翻转缓冲区
writeBuffer.flip();
logger.info("NioServer send message to NioClient : " + response);
// 发送缓冲区的字节数组
socketChannel.write(writeBuffer);
/*********处理“写半包”的代码**********/
}
}
3.客户端
package dcocd.netty.tool.io.nio.client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* NIO 客户端启动
*
* @author guohongjun@dcocd.cn
* @Date 2020/5/13 10:33
*/
public class NioClient {
private static Logger logger = LoggerFactory.getLogger(NioClient.class);
/**
* 默认 IP 地址
*/
private static String DEFAULT_HOST = "127.0.0.1";
/**
* 默认端口
*/
private static int DEFAULT_PORT = 12345;
/**
* 客户端处理器
*/
private static NioClientHandle nioClientHandle;
/**
* 启动客户端
*
* @param host IP 地址
* @param port 端口
*/
public static synchronized void start(String host, int port) {
if (nioClientHandle != null) {
nioClientHandle.stop();
}
nioClientHandle = new NioClientHandle(host, port);
new Thread(nioClientHandle, "nio-client-" + host + " : " + port).start();
}
/**
* 发送消息
*
* @param message 消息
* @return
* @throws IOException
*/
public static boolean sendMessage(String message) throws IOException {
String auth = "nio-token:";
if (!message.startsWith(auth)) {
logger.warn("Illegal user sending message");
return false;
}
nioClientHandle.sendMessage(message.replace(auth, ""));
return true;
}
/**
* 使用默认的 IP 地址和端口号启动客户端
*/
public static void start() {
start(DEFAULT_HOST, DEFAULT_PORT);
}
public static void main(String[] args) {
start();
}
}
4.客户端处理器
package dcocd.netty.tool.io.nio.client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
/**
* NIO 客户端处理器
*
* @author guohongjun@dcocd.cn
* @Date 2020/5/12 18:06
*/
public class NioClientHandle implements Runnable {
private static Logger logger = LoggerFactory.getLogger(NioClientHandle.class);
/**
* IP 地址
*/
private String host;
/**
* 端口号
*/
private int port;
/**
* 选择器
*/
private Selector selector;
/**
* 监听通道
*/
private SocketChannel socketChannel;
/**
* 客户端是否开启
*/
private volatile boolean started;
/**
* 构造方法
*
* @param host IP 地址
* @param port 端口号
*/
public NioClientHandle(String host, int port) {
this.host = host;
this.port = port;
try {
// 创建选择器
selector = Selector.open();
// 打开通道
socketChannel = SocketChannel.open();
// 如果为 true,则此通道将被置于阻塞模式;如果为 false,则此通道将被置于非阻塞模式
socketChannel.configureBlocking(false);
started = true;
logger.info("NioClient is start successful on port " + port);
} catch (IOException e) {
logger.error("NioClientHandle create error : " + e.getMessage());
// status为0时为正常退出程序,为非0的其他整数,表示非正常退出当前程序,结束当前正在运行中的java虚拟机
System.exit(1);
}
}
public void stop() {
started = false;
}
@Override
public void run() {
try {
this.connection();
} catch (IOException e) {
logger.error("NioClient connect " + host + " : " + port + " error : " + e.getMessage());
// status为0时为正常退出程序,为非0的其他整数,表示非正常退出当前程序,结束当前正在运行中的java虚拟机
System.exit(1);
}
// 循环遍历selector
while (started) {
try {
// 无论是否有读写事件发生,selector每隔1s被唤醒一次
selector.select(1000);
/*// 阻塞,只有当至少一个注册的事件发生的时候才会继续.
selector.select();*/
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
SelectionKey selectionKey = null;
while (iterator.hasNext()) {
selectionKey = iterator.next();
iterator.remove();
try {
// 创建该密钥的通道,完成消息的接收
this.handleInput(selectionKey);
} catch (IOException e) {
logger.error("NioClient read message error : " + e.getMessage());
if (selectionKey != null) {
// 取消此密钥的通道与其选择器注册
selectionKey.cancel();
if (selectionKey.channel() != null) {
// 关闭此密钥的通道
selectionKey.channel().close();
}
}
}
}
} catch (IOException e) {
logger.error("run() error : " + e.getMessage());
// status为0时为正常退出程序,为非0的其他整数,表示非正常退出当前程序,结束当前正在运行中的java虚拟机
System.exit(1);
}
}
// selector关闭后会自动释放里面管理的资源
if (selector != null) {
try {
selector.close();
} catch (IOException e) {
logger.error("NioClient run() Selector close error : " + e.getMessage());
}
}
}
/**
* 链接到服务器
*
* @throws IOException
*/
private void connection() throws IOException {
// 链接到指定 IP 地址的指定端口
boolean connect = socketChannel.connect(new InetSocketAddress(host, port));
// 如果没有链接成功
if (!connect) {
// 监听连接请求
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}
/**
* 创建该密钥的通道,完成消息的接收
*
* @param selectionKey 密钥
* @throws IOException
*/
private void handleInput(SelectionKey selectionKey) throws IOException {
// 判断密钥是否有效
if (selectionKey.isValid()) {
// 获取此密钥的通道
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
// 判断此密钥的通道是否已完成或失败,是否支持套接字连接操作
if (selectionKey.isConnectable()) {
// 判断此密钥的通道是否已完成或失败,是否支持套接字连接操作,不支持则退出当前客户端
if (!socketChannel.finishConnect()) {
// status为0时为正常退出程序,为非0的其他整数,表示非正常退出当前程序,结束当前正在运行中的java虚拟机
System.exit(1);
}
}
// 读消息
if (selectionKey.isReadable()) {
// 创建ByteBuffer,并开辟一个1M的缓冲区
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
// 读取请求码流,返回读取到的字节数
int readBytes = socketChannel.read(readBuffer);
// 读取到字节,对字节进行编解码
if (readBytes > 0) {
// 将缓冲区当前的limit设置为position=0,用于后续对缓冲区的读取操作
readBuffer.flip();
// 根据缓冲区可读字节数创建字节数组
byte[] bytes = new byte[readBuffer.remaining()];
// 将缓冲区可读字节数组复制到新建的数组中
readBuffer.get(bytes);
String message = new String(bytes, Charset.forName("UTF-8"));
logger.info("NioClient received message from NioServer : " + message);
} else if (readBytes == 0) {
/*********没有读取到字节 忽略**********/
} else if (readBytes < 0) {
// 取消此密钥的通道与其选择器注册
selectionKey.cancel();
// 关闭此密钥的通道
selector.close();
}
}
}
}
/**
* 消息的发送
*
* @param socketChannel 监听通道
* @param message 消息
* @throws IOException
*/
private void handleOutput(SocketChannel socketChannel, String message) throws IOException {
// 将消息编码为字节数组
byte[] bytes = message.getBytes(Charset.forName("UTF-8"));
// 根据数组容量创建 ByteBuffer
ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
//将字节数组复制到缓冲区
writeBuffer.put(bytes);
// flip 操作,翻转缓冲区
writeBuffer.flip();
// 发送缓冲区的字节数组
socketChannel.write(writeBuffer);
logger.info("NioClient send message to NioServer : " + message);
/*********处理“写半包”的代码**********/
}
/**
* 发送消息
*
* @param message 消息
* @throws IOException
*/
public void sendMessage(String message) throws IOException {
// 注册选择器
socketChannel.register(selector, SelectionKey.OP_READ);
// 消息的发送
this.handleOutput(socketChannel, message);
}
}
5.测试类
package dcocd.netty.tool.io.nio;
import dcocd.netty.tool.io.nio.client.NioClient;
import dcocd.netty.tool.io.nio.server.NioServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Scanner;
/**
* @author guohongjun@dcocd.cn
* @Date 2020/5/13 10:49
*/
public class NioTest {
private static Logger logger = LoggerFactory.getLogger(NioTest.class);
public static void main(String[] args) {
NioServer.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
logger.error("Thread sleep error : " + e.getMessage());
}
NioClient.start();
while (true) {
try {
System.out.println("请输入请求消息:");
Scanner scanner = new Scanner(System.in);
NioClient.sendMessage(scanner.nextLine());
} catch (IOException e) {
logger.error("NioClient sendMessage error : " + e.getMessage());
}
}
}
}