ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 一.引入依赖 ~~~ <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java --> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.14.0</version> </dependency> ~~~ ### 二.netty服务器端 ~~~ package com.youge.netty.codec; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.ChannelPipeline; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.protobuf.ProtobufDecoder; import io.netty.handler.codec.protobuf.ProtobufEncoder; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020/12/3 14:02 */ public class NettyServer { public static void main(String[] args) { NioEventLoopGroup bossGroup = new NioEventLoopGroup(); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap serverBootstrap = new ServerBootstrap(); try { serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //在pipeline中加入ProtoBuf解码器 //指定对哪种对象进行解码 pipeline.addLast("encoder",new ProtobufEncoder()); pipeline.addLast("decoder",new ProtobufDecoder(StudentPOJO.Student.getDefaultInstance())); pipeline.addLast(new NettyServerHandler()); } }); System.out.println("启动服务器..."); ChannelFuture channelFuture = serverBootstrap.bind(7002).sync(); channelFuture.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } } ~~~ ### 三.netty服务器端~handler(处理器) ~~~ package com.youge.netty.codec; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.CharsetUtil; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020/12/3 14:10 */ //public class NettyServerHandler extends ChannelInboundHandlerAdapter { public class NettyServerHandler extends SimpleChannelInboundHandler<StudentPOJO.Student> { // @Override // public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // System.out.println("接收到客户端发送过来的数据..."); // // StudentPOJO.Student student= (StudentPOJO.Student) msg; // System.out.println("客户端发送的消息:"+student.getId()+","+student.getName()); // } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { System.out.println("数据接收完毕.."); StudentPOJO.Student student1=StudentPOJO.Student.newBuilder().setId(6).setName("及时雨~~宋江").build(); ctx.writeAndFlush(student1); // ctx.writeAndFlush(Unpooled.copiedBuffer("数据已经收到", CharsetUtil.UTF_8)); } @Override protected void channelRead0(ChannelHandlerContext ctx, StudentPOJO.Student msg) throws Exception { System.out.println("接收到客户端发送过来的数据..."); System.out.println("客户端发送的消息:"+msg.getId()+","+msg.getName()); } } ~~~ ### netty客户端 ~~~ package com.youge.netty.codec; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.protobuf.ProtobufDecoder; import io.netty.handler.codec.protobuf.ProtobufEncoder; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020/12/3 14:14 */ public class NettyClient { public static void main(String[] args) { NioEventLoopGroup clientGroup = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); try { bootstrap.group(clientGroup) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //在pipeline中加入protobuf的编码器 pipeline.addLast("encoder", new ProtobufEncoder()); pipeline.addLast("decoder", new ProtobufDecoder(StudentPOJO.Student.getDefaultInstance())); pipeline.addLast(new NettyClientHandler()); } }); System.out.println("客户端准备就绪"); ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 7002).sync(); channelFuture.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { clientGroup.shutdownGracefully(); } } } ~~~ ### 五.netty客户端~handler(处理器) ~~~ package com.youge.netty.codec; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020/12/3 14:19 */ public class NettyClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("接收到服务器发过来的消息"); // ByteBuf buf= (ByteBuf) msg; // System.out.println(buf.toString(CharsetUtil.UTF_8)); StudentPOJO.Student student = (StudentPOJO.Student) msg; System.out.println("服务器发过来的消息:" + student.getId() + "," + student.getName()); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("发送数据到服务器..."); // ctx.channel().writeAndFlush(Unpooled.copiedBuffer("测试Google protobuf", CharsetUtil.UTF_8)); //发送一个Student对象到服务器 StudentPOJO.Student student = StudentPOJO.Student.newBuilder().setId(5).setName("豹子头 林冲").build(); ctx.channel().writeAndFlush(student); } } ~~~