单线程NIO
我们先实现单线程的NIO,我们后面把它改成多线程。
如果要实现NIO的网络模型,我们就需要三大组件:Selector,Channel,ByteBuffer
Selector : 用来监听Channel上的某种事件
Channel:一个服务器对应一个Channel,也就是一个管道。
ByteBuffer:指的是字节缓冲区,我们要把数据流读取到ByteBuffer中才能获取并处理它,或者把数据写入ByteBuffer中,才可以将它发送到网络中去。
SelectionKey用来表示事件,还可以表示事件的类型。
好我们来详细说一下实现的流程
服务端:
首先对三大组件要初始化以及port的设置,把ServerSocketChannel管道置为非阻塞状态,并且注册到Selector中,去监听连接事件。
创建一个监听的方法(Listener),监听管道(ServerSocketChannel)发生的事件,如果发生了某种事件,把selector中的监听到的事件放入一个Set集合中,遍历Set集合,把事件处理,删除处理完的事件。
创建一个处理事件的方法(Handle),对事件进行分类判断,如果连接事件(服务器对应的连接事件是accept),那么就把把对应的管道注册到Selctor中去监听这个管道的读事件(也就是客户端个有消息发送给服务端);如果是读事件那么就把发生事件的对应管道的数据读取到ByteBuffer中,然后给服务器进行回复,也就是写事件,写完之后,先把数据放到ByteBuffer中,再把让管道把ByteBuffer写入。这样客户端就能收到消息了。
客户端:
与服务端基本一致,唯一不同的就是监听管道是SocketChannel,监听的连接事件是Connectable,当监听到连接时间时,要判断三次握手是否完成,所以要调用管道的isConnectionPending()方法如果返回为真,就调用管道的finishedConnect()方法完成连接。其余都相同。
这种单线程实现的NIO模式叫做Reactor模式
具体实现:
客户端:
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.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class NIOSingletestCilent {
//服务端的端口号
private int port = 1234;
//三大组件selector channel Bytebuffer
private Selector selector;
private SocketChannel socketChannel ;
private ByteBuffer rcveBuffer;
private ByteBuffer sendBuffer;
private Scanner scanner;
//结构体初始化
public NIOSingletestCilent() throws IOException {
selector = Selector.open();
socketChannel = SocketChannel.open();
rcveBuffer = ByteBuffer.allocate(1024);
sendBuffer = ByteBuffer.allocate(1024);
//把管道置为阻塞,并且把管道注册到selector
socketChannel.configureBlocking(false);
//设置与哪一个ip的哪一个port建立连接
socketChannel.connect(new InetSocketAddress("127.0.0.1",port));
socketChannel.register(selector, SelectionKey.OP_CONNECT);
scanner = new Scanner(System.in);
}
//监听事件
public void listener() throws IOException {
while(true){
int selcet = selector.selectNow();
//如果有事件,那么就读取到Set集合上,并处理后删除。
if(selcet > 0 ){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
if(selectionKeys.isEmpty()) continue;
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey clientEvent = iterator.next();
handle(clientEvent);
iterator.remove();
}
}
}
}
//处理事件
private void handle (SelectionKey selectionKey) {
try {
if (selectionKey.isValid()) {
//如果事件是连接请求事件
if (selectionKey.isConnectable()) {
System.out.println("正在连接服务器");
//如果已经完成了三次握手,那么就完成连接
if (socketChannel.isConnectionPending()) {
socketChannel.finishConnect();
System.out.println("连接成功");
}
//将读事件添加到selector中去。
sendMessage((SocketChannel) selectionKey.channel());
socketChannel.register(selector, SelectionKey.OP_READ);
//如果是读事件
} else if (selectionKey.isReadable()) {
rcveBuffer.clear();
int read = socketChannel.read(rcveBuffer);
if(read > 0) {
String rev = new String(rcveBuffer.array(), 0, read);
System.out.println("服务器的消息为:" + rev);
}
sendMessage((SocketChannel) selectionKey.channel());
}
}
}catch(IOException e){
if(e.getMessage().equals("远程主机强制关闭了一个连接")){
System.out.println("关闭连接");
try {
selector.close();
socketChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
//发送消息
public void sendMessage(SocketChannel socketChannel ) throws IOException {
sendBuffer.clear();
System.out.println("请输入字符");
//读取控制台的输入字符
String send = scanner.next();
//将字符添加到sendBuffer中去写后发送消息并将指针移动到最开始的为止
sendBuffer.put(send.getBytes());
sendBuffer.flip();
socketChannel.write(sendBuffer);
}
public static void main(String[] args) {
try {
NIOSingletestCilent nioSingletestCilent = new NIOSingletestCilent();
nioSingletestCilent.listener();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端:
public class NIOsingletestSerever {
//服务器端口号
private int port = 1234;
//三大组件,selector,channel,buffer;
Selector selector;
ServerSocketChannel serverSocketChannel;
ByteBuffer rev;
ByteBuffer send;
NIOsingletestSerever() throws IOException {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
rev = ByteBuffer.allocate(1024);
send = ByteBuffer.allocate(1024);
//置为非阻塞状态
serverSocketChannel.configureBlocking(false);
//绑定端口号
serverSocketChannel.bind(new InetSocketAddress(port));
//把连接事件注册到监听器selector上
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("server init ");
}
public void listener() throws IOException {
while (true) {
int select = selector.select();
if (select > 0) {
//把事件提取到Set集合中。
Set<SelectionKey> selectionKeys = selector.selectedKeys();
if (selectionKeys.isEmpty()) continue;
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey event = iterator.next();
handle(event);
iterator.remove();
}
}
}
}
private void handle(SelectionKey event) {
SocketChannel client = null;
try {
if (event.isValid()) {
//如果是连接事件
if (event.isAcceptable()) {
client = serverSocketChannel.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
//如果是读事件
} else if (event.isReadable()) {
rev.clear();
client = (SocketChannel) event.channel();
int read = client.read(rev);
if (read > 0) {
String recive = new String(rev.array(), 0, read);
System.out.println("客户端的消息为:" + recive);
}
//回复消息给客户端
send.clear();
String sendMessage = "server receive message";
send.put(sendMessage.getBytes());
send.flip();
client.write(send);
client.register(selector, SelectionKey.OP_READ);
}
}
} catch (IOException e) {
if (e.getMessage().equals("远程主机强制关闭了一个连接") && client != null) {
try {
client.close();
selector.close();
serverSocketChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public static void main(String[] args) {
NIOsingletestSerever niOsingletestSerever = null;
try {
niOsingletestSerever = new NIOsingletestSerever();
;
niOsingletestSerever.listener();
} catch (IOException e) {
e.printStackTrace();
}
}
}
多线程NIO
上面我们完成了单线程的NIO模型,也就是reactor 模式 ,多线程的reactor模式是对单线程reactor模式的改善。
还是在Selector上注册客户端的监听事件,但是处理这些事件,我们交给线程去处理,这样就可以将处理事件的效率大大提升。
所以在这里读取数据和写入数据就不需要主线程去完成了,所以ByteBuffer实在线程里创建。
客户端与服务端与单线程相仿,只是在主线程只处理连接事件,如果时读事件,直接调用线程池中的线程去处理。所以handle中处理读事件的操作就在run方法中实现。因为处理事件一定要有对应的管道信息,也就是Channel,所以要把事件对应的Channel当作参数传入。
服务端:
public class NIOThreadServer {
//多线程型NIO
//端口号
private int port = 7734;
//三大组件
private Selector selector;
private ServerSocketChannel serverSocketChannel;
//线程池
private ExecutorService executorService;
//构造器
NIOThreadServer() throws IOException {
executorService = new ThreadPoolExecutor(5,10,10,
TimeUnit.SECONDS,new ArrayBlockingQueue<>(1000));
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
//将管道置为非阻塞,并且绑定端口号,将管道注入selector
serverSocketChannel.configureBlocking(false);
serverSocketChannel.bind(new InetSocketAddress(port));
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("初始化完成");
}
public void Listener() throws IOException {
while(true){
int select = selector.select();
if(select > 0){
//有事件发生,把事件存储在SET集合中,遍历处理,处理后删除
Set<SelectionKey> selectionKeys = selector.selectedKeys();
if(selectionKeys.isEmpty()) continue;
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey clientEvent = iterator.next();
handle(clientEvent);
iterator.remove();
}
}
}
}
public void handle(SelectionKey clientEvent){
SocketChannel client = null;
try {
if (clientEvent.isValid()) {
//如果是连接事件
if (clientEvent.isAcceptable()) {
client = serverSocketChannel.accept();
client.configureBlocking(false);
client.register(selector,SelectionKey.OP_READ);
}else if(clientEvent.isReadable()){
//线程池操作
client = (SocketChannel) clientEvent.channel();
executorService.submit( new ServerHandler(client));
}
}
}catch(IOException e){
if(e.getMessage().equals("远程主机强制关闭了一个连接")&& client !=null){
System.out.println("远程主机强制关闭了一个连接");
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
}
}
public static void main(String[] args) {
NIOThreadServer nioSingletestCilent= null;
try {
nioSingletestCilent = new NIOThreadServer();
nioSingletestCilent.Listener();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
public class NIOThreadClient {
//服务端的端口号
private int port = 7734;
//三大组件selector channel Bytebuffer
private Selector selector;
private SocketChannel socketChannel ;
private ByteBuffer rcveBuffer;
private ByteBuffer sendBuffer;
private Scanner scanner;
//结构体初始化
NIOThreadClient() throws IOException {
selector = Selector.open();
socketChannel = SocketChannel.open();
rcveBuffer = ByteBuffer.allocate(1024);
sendBuffer = ByteBuffer.allocate(1024);
//把管道置为阻塞,并且把管道注册到selector
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("127.0.0.1",port));
socketChannel.register(selector, SelectionKey.OP_CONNECT);
scanner = new Scanner(System.in);
}
//监听事件
private void listener() throws IOException {
while(true){
int selcet = selector.selectNow();
//如果有事件,那么就读取到Set集合上,并处理后删除。
if(selcet > 0 ){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
if(selectionKeys.isEmpty()) continue;
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey clientEvent = iterator.next();
handle(clientEvent);
iterator.remove();
}
}
}
}
//处理事件
private void handle (SelectionKey selectionKey) {
try {
if (selectionKey.isValid()) {
//如果事件是连接请求事件
if (selectionKey.isConnectable()) {
System.out.println("正在连接服务器");
//如果已经完成了三次握手,那么就完成连接
if (socketChannel.isConnectionPending()) {
socketChannel.finishConnect();
System.out.println("连接成功");
}
//将读事件添加到selector中去。
sendMessage((SocketChannel) selectionKey.channel());
socketChannel.register(selector, SelectionKey.OP_READ);
//如果是读事件
} else if (selectionKey.isReadable()) {
rcveBuffer.clear();
int read = socketChannel.read(rcveBuffer);
if(read > 0) {
String rev = new String(rcveBuffer.array(), 0, read);
System.out.println("服务器的消息为:" + rev);
}
sendMessage((SocketChannel) selectionKey.channel());
}
}
}catch(IOException e){
if(e.getMessage().equals("远程主机强制关闭了一个连接")){
System.out.println("关闭连接");
try {
selector.close();
socketChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
//发送消息
public void sendMessage(SocketChannel socketChannel ) throws IOException {
sendBuffer.clear();
System.out.println("请输入字符");
//读取控制台的输入字符
String send = scanner.next();
//将字符添加到sendBuffer中去写后发送消息并将指针移动到最开始的为止
sendBuffer.put(send.getBytes());
sendBuffer.flip();
socketChannel.write(sendBuffer);
}
public static void main(String[] args) {
NIOThreadClient nioSingletestCilent= null;
try {
nioSingletestCilent = new NIOThreadClient();
nioSingletestCilent.listener();
} catch (IOException e) {
e.printStackTrace();
}
}
}
线程类:
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
public class ServerHandler implements Runnable{
SocketChannel client = null;
ByteBuffer readBuffer;
ByteBuffer writeBuffer;
private Selector selector;
ServerHandler(){
}
ServerHandler(SocketChannel socketChannel) throws IOException {
readBuffer = ByteBuffer.allocate(2048);
writeBuffer = ByteBuffer.allocate(2048);
client = socketChannel;
selector = Selector.open();
}
@Override
public void run() {
//处理读事件
try {
readBuffer.clear();
int read = client.read(readBuffer);
readBuffer.flip();
if(read > 0){
String rev = new String (readBuffer.array(),0,read);
System.out.println("server 收到的客户端消息为" + rev);
}
writeBuffer .clear();
String send = "已收到消息";
writeBuffer.put(send.getBytes());
writeBuffer.flip();
client.write(writeBuffer);
}catch (IOException e){
if(e.getMessage().equals("远程主机强迫关闭了一个现有的连接。") && client != null) {
System.out.println("远程主机强迫关闭了一个现有的连接。关闭资源");
try {
client.close();
} catch (IOException e1) {
// e1.printStackTrace();
}
}
// e.printStackTrace();
return;
}
}
}
主从模式NIO
多线程Reactor模式中还是有一个问题,Selector监听的事件太多了,如果成千上百的客户端发生事件,虽然有多线程的高效处理,但是还是监听不过来那么多事件。所以为了再提高效率,我们把监听也交给线程好了! 这种模式就叫做主从式Reactor
每一个线程池中的线程都有自己的Selector,当处理完连接事件后,那就只能去处理读事件了,所以 new 一个线程,获取该线程的Selector,把管道注册到线程的Selector上。
这样主线程的Selector只监听连接事件,所有的读事件也交给线程去监听,处理也交给线程,那么效率教会大大提高了。
服务端:
public class NIOThreadServer {
//多线程型NIO
//端口号
private int port = 7734;
//三大组件
private Selector selector;
private ServerSocketChannel serverSocketChannel;
//线程池
private ExecutorService executorService;
//构造器
NIOThreadServer() throws IOException {
executorService = new ThreadPoolExecutor(5,10,10,
TimeUnit.SECONDS,new ArrayBlockingQueue<>(1000));
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
//将管道置为非阻塞,并且绑定端口号,将管道注入selector
serverSocketChannel.configureBlocking(false);
serverSocketChannel.bind(new InetSocketAddress(port));
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("初始化完成");
}
public void Listener() throws IOException {
while(true){
int select = selector.select();
if(select > 0){
//有事件发生,把事件存储在SET集合中,遍历处理,处理后删除
Set<SelectionKey> selectionKeys = selector.selectedKeys();
if(selectionKeys.isEmpty()) continue;
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey clientEvent = iterator.next();
handle(clientEvent);
iterator.remove();
}
}
}
}
public void handle(SelectionKey clientEvent){
SocketChannel client = null;
try {
if (clientEvent.isValid()) {
//如果是连接事件
if (clientEvent.isAcceptable()) {
client = serverSocketChannel.accept();
client.configureBlocking(false);
client.register(selector,SelectionKey.OP_READ);
}else if(clientEvent.isReadable()){
//线程池操作
client = (SocketChannel) clientEvent.channel();
executorService.submit( new ServerHandler(client));
}
}
}catch(IOException e){
if(e.getMessage().equals("远程主机强制关闭了一个连接")&& client !=null){
System.out.println("远程主机强制关闭了一个连接");
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
}
}
public static void main(String[] args) {
NIOThreadServer nioSingletestCilent= null;
try {
nioSingletestCilent = new NIOThreadServer();
nioSingletestCilent.Listener();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
public class NIOThreadClient {
//服务端的端口号
private int port = 7734;
//三大组件selector channel Bytebuffer
private Selector selector;
private SocketChannel socketChannel ;
private ByteBuffer rcveBuffer;
private ByteBuffer sendBuffer;
private Scanner scanner;
//结构体初始化
NIOThreadClient() throws IOException {
selector = Selector.open();
socketChannel = SocketChannel.open();
rcveBuffer = ByteBuffer.allocate(1024);
sendBuffer = ByteBuffer.allocate(1024);
//把管道置为阻塞,并且把管道注册到selector
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("127.0.0.1",port));
socketChannel.register(selector, SelectionKey.OP_CONNECT);
scanner = new Scanner(System.in);
}
//监听事件
private void listener() throws IOException {
while(true){
int selcet = selector.selectNow();
//如果有事件,那么就读取到Set集合上,并处理后删除。
if(selcet > 0 ){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
if(selectionKeys.isEmpty()) continue;
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey clientEvent = iterator.next();
handle(clientEvent);
iterator.remove();
}
}
}
}
//处理事件
private void handle (SelectionKey selectionKey) {
try {
if (selectionKey.isValid()) {
//如果事件是连接请求事件
if (selectionKey.isConnectable()) {
System.out.println("正在连接服务器");
//如果已经完成了三次握手,那么就完成连接
if (socketChannel.isConnectionPending()) {
socketChannel.finishConnect();
System.out.println("连接成功");
}
//将读事件添加到selector中去。
sendMessage((SocketChannel) selectionKey.channel());
socketChannel.register(selector, SelectionKey.OP_READ);
//如果是读事件
} else if (selectionKey.isReadable()) {
rcveBuffer.clear();
int read = socketChannel.read(rcveBuffer);
if(read > 0) {
String rev = new String(rcveBuffer.array(), 0, read);
System.out.println("服务器的消息为:" + rev);
}
sendMessage((SocketChannel) selectionKey.channel());
}
}
}catch(IOException e){
if(e.getMessage().equals("远程主机强制关闭了一个连接")){
System.out.println("关闭连接");
try {
selector.close();
socketChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
//发送消息
public void sendMessage(SocketChannel socketChannel ) throws IOException {
sendBuffer.clear();
System.out.println("请输入字符");
//读取控制台的输入字符
String send = scanner.next();
//将字符添加到sendBuffer中去写后发送消息并将指针移动到最开始的为止
sendBuffer.put(send.getBytes());
sendBuffer.flip();
socketChannel.write(sendBuffer);
}
public static void main(String[] args) {
NIOThreadClient nioSingletestCilent= null;
try {
nioSingletestCilent = new NIOThreadClient();
nioSingletestCilent.listener();
} catch (IOException e) {
e.printStackTrace();
}
}
}
线程处理类
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
public class ServerHandler implements Runnable{
SocketChannel client = null;
ByteBuffer readBuffer;
ByteBuffer writeBuffer;
private Selector selector;
ServerHandler(){
}
ServerHandler(SocketChannel socketChannel) throws IOException {
readBuffer = ByteBuffer.allocate(2048);
writeBuffer = ByteBuffer.allocate(2048);
client = socketChannel;
selector = Selector.open();
}
@Override
public void run() {
//处理读事件
try {
readBuffer.clear();
int read = client.read(readBuffer);
readBuffer.flip();
if(read > 0){
String rev = new String (readBuffer.array(),0,read);
System.out.println("server 收到的客户端消息为" + rev);
}
writeBuffer .clear();
String send = "已收到消息";
writeBuffer.put(send.getBytes());
writeBuffer.flip();
client.write(writeBuffer);
}catch (IOException e){
if(e.getMessage().equals("远程主机强迫关闭了一个现有的连接。") && client != null) {
System.out.println("远程主机强迫关闭了一个现有的连接。关闭资源");
try {
client.close();
} catch (IOException e1) {
// e1.printStackTrace();
}
}
// e.printStackTrace();
return;
}
}
}