Netty进阶——粘包与半包(固定长度方式解决粘包问题)

Posted 小志的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Netty进阶——粘包与半包(固定长度方式解决粘包问题)相关的知识,希望对你有一定的参考价值。

目录

一、固定长度方式解决粘包问题(代码示例)

1.1、固定长度方式解决粘包问题的服务端代码示例

  • 让所有数据包长度固定(假设长度为 16 字节),服务器端加入

    ch.pipeline().addLast(new FixedLengthFrameDecoder(16));
    
  • 服务端完整代码示例

    package com.example.nettytest.netty.day5;
    
    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.FixedLengthFrameDecoder;
    import io.netty.handler.logging.LogLevel;
    import io.netty.handler.logging.LoggingHandler;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * @description: 固定长度方式解决粘包 问题服务端示例
     * @author: xz
     */
    @Slf4j
    public class NettyFixLengthServer 
        public static void main(String[] args) 
            new NettyFixLengthServer().start();
        
    
        void start() 
            NioEventLoopGroup boss = new NioEventLoopGroup(1);
            NioEventLoopGroup worker = new NioEventLoopGroup();
            try 
                ServerBootstrap serverBootstrap = new ServerBootstrap()
                        .channel(NioServerSocketChannel.class)
                         //调整 netty 的接受缓冲区(byteBuf)
                        .childOption(ChannelOption.RCVBUF_ALLOCATOR,new AdaptiveRecvByteBufAllocator(16,16,16))
                        .group(boss, worker)
                        .childHandler(new ChannelInitializer<SocketChannel>() 
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception 
                                //设置定长解码器,位置必须再LoggingHandler之前,作用让所有数据包长度固定(假设长度为 16 字节)
                                ch.pipeline().addLast(new FixedLengthFrameDecoder(16));
                                ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() 
                                    //会在连接channel建立成功后,触发active事件
                                    @Override
                                    public void channelActive(ChannelHandlerContext ctx) throws Exception 
                                        log.debug("connected>>>>>>>>>>>>>>>> ", ctx.channel());
                                        super.channelActive(ctx);
                                    
                                    @Override
                                    public void channelInactive(ChannelHandlerContext ctx) throws Exception 
                                        log.debug("disconnect>>>>>>>>>>>>>>>> ", ctx.channel());
                                        super.channelInactive(ctx);
                                    
                                );
                            
                        );
                ChannelFuture channelFuture = serverBootstrap.bind(8080);
                log.debug(">>>>>>>>>>>>>>>> binding...", channelFuture.channel());
                channelFuture.sync();
                log.debug(">>>>>>>>>>>>>>>> bound...", channelFuture.channel());
                channelFuture.channel().closeFuture().sync();
             catch (InterruptedException e) 
                log.error("server error", e);
             finally 
                boss.shutdownGracefully();
                worker.shutdownGracefully();
                log.debug(">>>>>>>>>>>>>>>>stoped");
            
        
    
    

1.2、固定长度方式解决粘包问题的客户端代码示例

  • 客户端代码示例

    package com.example.nettytest.netty.day5;
    
    import io.netty.bootstrap.Bootstrap;
    import io.netty.buffer.ByteBuf;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.logging.LogLevel;
    import io.netty.handler.logging.LoggingHandler;
    import lombok.extern.slf4j.Slf4j;
    
    import java.util.Arrays;
    import java.util.Random;
    
    /**
     * @description: 固定长度方式解决粘包 问题客户端示例
     * @author: xz
     */
    @Slf4j
    public class NettyFixLengthClient 
        public static void main(String[] args) 
            send();
        
    
        //剩余位置用下划线填充
        public static byte[] fill10Bytes(char c,int len)
            byte[] bytes = new byte[16];
            Arrays.fill(bytes, (byte) '_');
            for (int i = 0; i < len; i++) 
                bytes[i] = (byte) c;
            
            System.out.println(new String(bytes));
            return bytes;
        
    
        private static void send() 
            NioEventLoopGroup worker = new NioEventLoopGroup();
            try 
                Bootstrap bootstrap = new Bootstrap();
                bootstrap.channel(NioSocketChannel.class);
                bootstrap.group(worker);
                bootstrap.handler(new ChannelInitializer<SocketChannel>() 
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception 
                        log.debug("connetted...");
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() 
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception 
                                log.debug("sending...");
                                //设置ByteBuf
                                ByteBuf buffer = ctx.alloc().buffer();
                                // 发送内容随机的数据包
                                Random r = new Random();
                                char c = '0';
                                for (int i = 0; i < 10; i++) 
                                    //剩余位置用下划线填充方法
                                    byte[] bytes =fill10Bytes(c,r.nextInt(16)+1);
                                    c++;
                                    //写入到ByteBuf
                                    buffer.writeBytes(bytes);
                                
                                ctx.writeAndFlush(buffer);
                            
                        );
                    
                );
                ChannelFuture channelFuture = bootstrap.connect("localhost", 8080).sync();
                channelFuture.channel().closeFuture().sync();
    
             catch (InterruptedException e) 
                log.error("client error", e);
             finally 
                worker.shutdownGracefully();
            
        
    
    

1.3、分别启动服务端,客户端,查看服务端结果输出

  • 先启动服务端

  • 再启动客户端

  • 再次查看服务端
    由输出结果可知,固定长度方式解决粘包问题的缺点:长度定的太大,浪费;长度定的太小,对某些数据包又显得不够。

以上是关于Netty进阶——粘包与半包(固定长度方式解决粘包问题)的主要内容,如果未能解决你的问题,请参考以下文章

Netty进阶——粘包与半包(预设长度方式解决粘包问题)

Netty进阶——粘包与半包(预设长度方式解决粘包问题)

Netty进阶——粘包与半包(固定分隔符方式解决粘包问题)

Netty进阶——粘包与半包(固定分隔符方式解决粘包问题)

Netty进阶——粘包与半包(短链接方式解决粘包问题)

Netty进阶——粘包与半包(短链接方式解决粘包问题)