分享一段Netty最新版本的小程序
依赖
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>5.0.0.Alpha2</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty</artifactId> <version>3.10.6.Final</version> </dependency>
客户端
package netty; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; /** * @author lijinquan * @date 2020/9/10 20:20 */ public class Client { public static void main(String[] args) { //线程池 EventLoopGroup group = new NioEventLoopGroup();//初始化线程池时候可以给构造传参,1/2/3都可以,默认不传为核数*2 Bootstrap bootstrap = new Bootstrap();//辅助类 try { //得到一个future,回调 ChannelFuture fu = bootstrap.group(group) .channel(NioSocketChannel.class)//什么类型通道 .handler(new ClientChannelInitializer()) .connect("127.0.0.1", 6666)//连接谁 .sync();//执行完,才能继续 /** * netty所有调用的方法都是异步的,调用后不管成不成继续执行 */ //回调结果中可以知道连接成功与否,如下的判断语句 fu.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { if (channelFuture.isSuccess()) { System.out.println("客户端====>连上了!"); } } }); fu.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { group.shutdownGracefully();//关闭 } } } class ClientChannelInitializer extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { //初始化管道 socketChannel.pipeline().addLast(new MyClientHandler()); //给管道添加handler } } class MyClientHandler extends ChannelHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { //客户端给服务端发送一个请求 ByteBuf byteBuf = Unpooled.copiedBuffer("aaa".getBytes()); ctx.writeAndFlush(byteBuf);//刷新和关闭 } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); } }
服务端
package netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; 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.util.ReferenceCountUtil; /** * @author lijinquan * @date 2020/9/11 23:52 */ public class Server { public static void main(String[] args) { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(2); ServerBootstrap sb = new ServerBootstrap(); ChannelFuture cf = null; try { cf = sb.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new MyServerChildHandler()); } }).bind(6666).sync(); System.out.println("服务端===>启动了!"); cf.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } } class MyServerChildHandler extends ChannelHandlerAdapter{ //这里的channelRead就是用来读客户端的请求,msg就是bytebuf @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf=null; try { buf=(ByteBuf)msg; // // System.out.println(msg); // System.out.println(buf); // System.out.println(buf.refCnt()); byte[] bytes=new byte[ buf.readableBytes()]; buf.getBytes(buf.readableBytes(),bytes); System.out.println(new String(bytes)); }catch (Exception e){ e.printStackTrace(); }finally { if(buf!=null){ ReferenceCountUtil.release(buf); } } } }
//客户端给服务端发送一个请求 ByteBuf byteBuf = Unpooled.copiedBuffer("aaa".getBytes());
这段给服务端发送的请求,但是客户端能调试断点进去打印出空的东西,找不到原因