多处摘抄或手打,为了十积分厚着脸皮标为原创,惭愧惭愧~
参考地址:
文案部分:
百度百科:https://baike.baidu.com/item/Netty/10061624?fr=aladdin
NIO:https://blog.csdn.net/skiof007/article/details/52873421
代码部分:
https://blog.csdn.net/hangge110/article/details/51613988
更多参考:
各个对象以及常量的作用对照源码:https://blog.csdn.net/CoffeeAndIce/article/details/78987542
源代码学习1:https://www.cnblogs.com/katsura/p/5991428.html
源代码学习2:https://www.cnblogs.com/stevenczp/p/7581940.html
Netty
也就是说,Netty 是一个基于NIO的客户、服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、
服务端应用。Netty相当于简化和流线化了网络应用的编程开发过程,例如:基于TCP和UDP的socket服务开发。
NIO基本概念:点击打开链接
IO的方式通常分为几种,同步阻塞的BIO、同步非阻塞的NIO、异步非阻塞的AIO......
DEMO(手打https://blog.csdn.net/hangge110/article/details/51613988):
1、服务端
package com.zx.test; import com.lk.netty.mulchat.dome.ServerIniterHandler; import com.lk.netty.mulchat.dome.ServerMain; 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 org.omg.CORBA.PRIVATE_MEMBER; /** * 服务端生产者对象 * This is an netty Server * @author ZX * @date 2018年6月7日 * Netty中,通讯的双方建立连接后,会把数据按照ByteBuf的方式进行传输, * 例如http协议中,就是通过HttpRequestDecoder对ByteBuf数据流进行处理,转换成http的对象。 * 深入学习: * https://www.cnblogs.com/katsura/p/5991428.html * https://www.cnblogs.com/stevenczp/p/7581940.html */ public class ServerTest { /** * 服务端口 */ private int port=9999; /** * 开启服务的方法 */ public void StartNetty(){ /**创建两个EventLoop的组,EventLoop 这个相当于一个处理线程, 是Netty接收请求和处理IO请求的线程。不理解的话可以百度NIO图解*/ EventLoopGroup acceptor = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { //1、创建启动类 ServerBootstrap bootstrap = new ServerBootstrap(); //2、配置启动参数等 /**设置循环线程组,前者用于处理客户端连接事件,后者用于处理网络IO(server使用两个参数这个) *public ServerBootstrap group(EventLoopGroup group) *public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) */ bootstrap.group(acceptor,worker); /**设置选项 * 参数:Socket的标准参数(key,value),可自行百度 * eg: * bootstrap.option(ChannelOption.SO_BACKLOG, 1024); *bootstrap.option(ChannelOption.SO_KEEPALIVE, true); * */ bootstrap.option(ChannelOption.SO_BACKLOG, 1024); //用于构造socketchannel工厂 bootstrap.channel(NioServerSocketChannel.class); /** * 传入自定义客户端Handle(服务端在这里搞事情) */ bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { // 注册handler ch.pipeline().addLast(new SimpleServerHandler()); } }); // 绑定端口,开始接收进来的连接 ChannelFuture f = bootstrap.bind(port).sync(); // 等待服务器 socket 关闭 。 f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { acceptor.shutdownGracefully(); worker.shutdownGracefully(); } } public static void main(String[] args) { new ServerTest().StartNetty(); } }
2、服务端处理类
package com.zx.test; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; public class SimpleServerHandler extends ChannelInboundHandlerAdapter { /** * 本方法用于读取客户端发送的信息 * @param ctx * @param msg * @throws Exception */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("SimpleServerHandler.channelRead"); ByteBuf result = (ByteBuf) msg; byte[] result1 = new byte[result.readableBytes()]; // msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中 result.readBytes(result1); String resultStr = new String(result1); // 接收并打印客户端的信息 System.out.println("Client said:" + resultStr); // 释放资源,这行很关键 result.release(); // 向客户端发送消息 String response = "hello client!"; // 在当前场景下,发送的数据必须转换成ByteBuf数组 ByteBuf encoded = ctx.alloc().buffer(4 * response.length()); encoded.writeBytes(response.getBytes()); ctx.write(encoded); ctx.flush(); } /** * 本方法用作处理异常 * @param ctx * @param cause * @throws Exception */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 当出现异常就关闭连接 cause.printStackTrace(); ctx.close(); } /** * 信息获取完毕后操作 * @param ctx * @throws Exception */ @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } }
3、客户端
package com.zx.test; import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; /** * 客户端消费者对象 * This is an netty Client * @author ZX * @date 2018年6月7日 */ public class ClientTest { public void connect(String host, int port) throws Exception { EventLoopGroup worker = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); /** *EventLoop的组 */ b.group(worker); /** * 用于构造socketchannel工厂 */ b.channel(NioSocketChannel.class); /**设置选项 * 参数:Socket的标准参数(key,value),可自行百度 保持呼吸,不要断气! * */ b.option(ChannelOption.SO_KEEPALIVE, true); /** * 自定义客户端Handle(客户端在这里搞事情) */ b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new SimpleClientHandler()); } }); /** 开启客户端监听*/ ChannelFuture f = b.connect(host, port).sync(); /**等待数据直到客户端关闭*/ f.channel().closeFuture().sync(); } finally { worker.shutdownGracefully(); } } public static void main(String[] args) throws Exception { ClientTest client=new ClientTest(); client.connect("127.0.0.1", 9999); } }
4、客户端处理类
package com.zx.test; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; public class SimpleClientHandler extends ChannelInboundHandlerAdapter { /** * 本方法用于接收服务端发送过来的消息 * @param ctx * @param msg * @throws Exception */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("SimpleClientHandler.channelRead"); ByteBuf result = (ByteBuf) msg; byte[] result1 = new byte[result.readableBytes()]; result.readBytes(result1); System.out.println("Server said:" + new String(result1)); result.release(); } /** * 本方法用于处理异常 * @param ctx * @param cause * @throws Exception */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 当出现异常就关闭连接 cause.printStackTrace(); ctx.close(); } /** * 本方法用于向服务端发送信息 * @param ctx * @throws Exception */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { String msg = "hello Server!"; ByteBuf encoded = ctx.alloc().buffer(4 * msg.length()); encoded.writeBytes(msg.getBytes()); ctx.write(encoded); ctx.flush(); } }
5、运行结果:
服务端:
SimpleServerHandler.channelRead
Client said:hello Server!
客户端:
SimpleClientHandler.channelRead
Server said:hello client!