<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### 一.引入依賴 ~~~ <!-- 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); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看