一、IO
简介
IO:每个小朋友配一个老师。每个老师隔段时间询问小朋友是否要上厕所,如果要上,就领他去厕所,100个小朋友就需要100个老师来询问,并且每个小朋友上厕所的时候都需要一个老师领着他去上,这就是IO模型,一个连接对应一个线程。
实现
服务器
package io;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* author:醉意丶千层梦
*/
public class IOServer {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
//创建socket服务,监听8081端口
ServerSocket server=new ServerSocket(8081);
System.out.println("服务器启动!");
int count=0;
while(true){
//获取一个套接字(阻塞)
final Socket socket = server.accept();
System.out.println("欢迎第"+(++count)+"个客户");
newCachedThreadPool.execute(new Runnable() {
@Override
public void run() {
//业务处理
handler(socket);
}
});
}
}
/**
* 读取数据
* @param socket
* @throws Exception
*/
public static void handler(Socket socket){
try {
byte[] bytes = new byte[1024];
InputStream inputStream = socket.getInputStream();
while(true){
//读取数据(阻塞)
int read = inputStream.read(bytes);
if(read != -1){
System.out.println(new String(bytes, 0, read));
}else{
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
System.out.println("socket关闭");
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
客户端
package io;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
/**
* author:醉意丶千层梦
*/
public class IOClient {
public static void main(String[] args) throws IOException {
//发送十次
for (int i=0;i<10;i++){
Socket socket=new Socket("127.0.0.1", 8081);
//写数据
OutputStream os=socket.getOutputStream();
os.write(("醉意丶千层梦"+i).getBytes());
//释放资源
socket.close();
}
}
}
效果
二、NIO
简介
NIO:所有的小朋友都配同一个老师。这个老师隔段时间询问所有的小朋友是否有人要上厕所,然后每一时刻把所有要上厕所的小朋友批量领到厕所,这就是NIO模型,所有小朋友都注册到同一个老师,对应的就是所有的连接都注册到一个线程,然后批量轮询。
实现
服务器
package 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;
/**
* author:醉意丶千层梦
*/
public class NIOServer {
// 通道管理器
private Selector selector;
/**
* 启动服务端测试
*
* @throws IOException
*/
public static void main(String[] args) throws IOException {
NIOServer server = new NIOServer();
server.initServer(8081);
server.listen();
}
/**
* 获得一个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);
}
/**
* 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理
*
* @throws IOException
*/
public void listen() throws IOException {
System.out.println("服务端启动成功!");
// 轮询访问selector
while (true) {
// 当注册的事件到达时,方法返回;否则,该方法会一直阻塞
selector.select();
// 获得selector中选中的项的迭代器,选中的项为注册的事件
Iterator<?> ite = this.selector.selectedKeys().iterator();
while (ite.hasNext()) {
SelectionKey key = (SelectionKey) ite.next();
// 删除已选的key,以防重复处理
ite.remove();
handler(key);
}
}
}
/**
* 处理请求
*
* @param key
* @throws IOException
*/
public void handler(SelectionKey key) throws IOException {
// 客户端请求连接事件
if (key.isAcceptable()) {
handlerAccept(key);
// 获得了可读的事件
} else if (key.isReadable()) {
handelerRead(key);
}
}
/**
* 处理连接请求
*
* @param key
* @throws IOException
*/
public void handlerAccept(SelectionKey key) throws IOException {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
// 获得和客户端连接的通道
SocketChannel channel = server.accept();
// 设置成非阻塞
channel.configureBlocking(false);
// 在这里可以给客户端发送信息哦
System.out.println("新的客户端连接");
// 在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。
channel.register(this.selector, SelectionKey.OP_READ);
}
/**
* 处理读的事件
*
* @param key
* @throws IOException
*/
public void handelerRead(SelectionKey key) throws IOException {
// 服务器可读取消息:得到事件发生的Socket通道
SocketChannel channel = (SocketChannel) key.channel();
// 创建读取的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
int read = channel.read(buffer);
if(read > 0){
byte[] data = buffer.array();
String msg = new String(data).trim();
System.out.println("服务端收到信息:" + msg);
//回写数据
ByteBuffer outBuffer = ByteBuffer.wrap("好的".getBytes());
channel.write(outBuffer);// 将消息回送给客户端
}else{
System.out.println("客户端关闭");
key.cancel();
}
}
}
客户端
package nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
/**
* author:醉意丶千层梦
*/
public class NIOClient {
public static void main(String[] args) throws Exception {
final int count[]=new int[1];
count[0]=1;
for(int i=0;i<5;i++){
new Thread(new Runnable() {
@Override
public void run() {
SocketChannel socketChannel = null;
//发送的数据
String str = "醉意丶千层梦"+count[0]++;
ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());
//接受的数据
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
//建立连接
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
if (!socketChannel.connect(new InetSocketAddress("127.0.0.1", 8081))) {
//等待连接
while (!socketChannel.finishConnect()) {
}
}
//写入数据
socketChannel.write(byteBuffer);
} catch (IOException e) {
e.printStackTrace();
}
//10s后自动断开连接
int time=1;
while (time<10){
time++;
try {
//读取数据
int read=socketChannel.read(buffer);
if(read > 0) {
byte[] data = buffer.array();
String msg = new String(data).trim();
System.out.println("客户端收到信息:" + msg);
}
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Thread.sleep(100);
}
}
}
效果
服务器
客户端
三、Netty
简介
Netty 是一个基于NIO的客户、服务器端编程框架
实现
服务器
package netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.util.Scanner;
/**
* author:醉意丶千层梦
*/
public class NettyServer {
public static void main(String[] args) {
//用于处理服务器端接收客户端连接
NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
//进行网络通信(读写)
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
try {
//辅助工具类,用于服务器通道的一系列配置
ServerBootstrap bootstrap = new ServerBootstrap();
//绑定两个线程组
bootstrap.group(bossGroup,workerGroup)
//设置boss selector建立channel使用的对象
.channel(NioServerSocketChannel.class)
//boss 等待连接的 队列长度
.option(ChannelOption.SO_BACKLOG,1024)
//处理消息对象
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//创建管道
ChannelPipeline pipeline = ch.pipeline();
//解码方式
pipeline.addLast("decoder",new StringDecoder());
//编码方式
pipeline.addLast("encoder",new StringEncoder());
//自定义处理消息对象
pipeline.addLast(new ServerHandler());
}
});
System.out.println("服务器正在启动");
//绑定端口号
ChannelFuture cf = bootstrap.bind(8083).sync();
cf.addListener(cd->{
if(cd.isSuccess()){
System.out.println("启动成功");
}else{
System.out.println("启动失败");
}
});
//服务端给所有客户端发信息
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
String msg = scanner.nextLine();
ServerHandler.sendAll(msg);
}
//阻塞当前线程
cf.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package netty;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* author:醉意丶千层梦
*/
public class ServerHandler extends SimpleChannelInboundHandler<String> {
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
Channel channel = ctx.channel();
System.out.println(channel.remoteAddress()+" == " +msg);
channelGroup.forEach(ch->{
if (channel!=ch) {
ch.writeAndFlush("[ 客户端 ]" + channel.remoteAddress() + "发送了消息 : " + msg + "\n");
}else{
ch.writeAndFlush("[ 我 ] 发送了消息: " + msg + "\n");
}
});
}
//用于服务端发信息给所有客户端
public static void sendAll(String msg){
channelGroup.forEach(channel -> {
channel.writeAndFlush("服务器: "+msg+"\n");
});
}
/**
* 当有新的用户连接触发
* @param ctx
*/
public void channelActive(ChannelHandlerContext ctx){
Channel channel = ctx.channel();
channelGroup.writeAndFlush("[ 客户端 ]"+channel.remoteAddress()+" 上线了 "+sf.format(new Date())+"\n");
//把新来的连接加入
channelGroup.add(channel);
System.out.println(ctx.channel().remoteAddress()+" 上线了" + "\n");
}
/**
* 当用户断开连接触发
* @param ctx
*/
public void channelInactive(ChannelHandlerContext ctx) {
Channel channel = ctx.channel();
channelGroup.writeAndFlush("[ 客户端 ] " +channel.remoteAddress()+ " 下线了"+"\n");
System.out.println(channel.remoteAddress()+" 下线了.\n");
System.out.println("channelGroup size = "+ channelGroup.size());
}
}
客户端
package netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* author:醉意丶千层梦
*/
public class NettyClient {
/**
* 主函数
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
List<ChannelFuture> channelFutures = new ArrayList<ChannelFuture>();
try {
Bootstrap bootstrap = new Bootstrap();
//服务器可以主动断开连接
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
//地址复用
bootstrap.option(ChannelOption.SO_REUSEADDR, true);
bootstrap.group(group).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("decoder",new StringDecoder());
pipeline.addLast("encoder",new StringEncoder());
pipeline.addLast(new ClientHandler());
}
});
final int count[] =new int[1];
count[0]=0;
for(int i=0;i<3;i++){
//添加连接
channelFutures.add(bootstrap.connect("127.0.0.1",8083).sync());
//新建线程模拟多用户
new Thread(new Runnable() {
@Override
public void run() {
int index=count[0]++;
//获取对应管道
Channel channel = channelFutures.get(index).channel();
System.out.println( "======"+channel.localAddress()+"======");
int time=0;
while (time++<3){
//发送数据
String msg =" 醉意丶千层梦 "+(index)+": "+time;
channel.writeAndFlush(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//关闭连接
channel.close();
}
}).start();
}
//阻塞主线程,否则会直接执行finally关闭EventLoopGroup
int time=0;
while (time++<5){
Thread.sleep(1000);
}
} finally {
//关闭EventLoopGroup
group.shutdownGracefully();
}
}
}
package netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
* author:醉意丶千层梦
*/
public class ClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
System.out.println(msg.trim());
}
}
效果
服务器
客户端
四、总结
Netty不好学,但是很好用
五、参考
https://blog.csdn.net/qq_32717909/article/details/91409999
https://blog.csdn.net/qq_40640228/article/details/103433348