LengthFieldBasedFrameDecoder使用

时间:2025-01-25 17:19:37

LengthFieldBasedFrameDecoder解码器,会读取byteBuf中的前几个字节,获取数据长度。

作用是防止粘包。

如果客户端也是netty,那么可以配合LengthFieldPrepender使用。LengthFieldPrepender会自动在原始数据前面加上数据长度。

().addLast(new LengthFieldBasedFrameDecoder(65535,0,2,0,2));

第二个参数指定lengthFieldOffset,也就是长度字段的偏移距,一般设置为0
第三个参数指定lengthFieldLength,也就是几个字节,2个字节的话,一个frame最多65535个字节,short类型的最大值。
lengthAdjustment, initialBytesToStrip,后面2个参数,lengthAdjustment一般为0,initialBytesToStrip和lengthFieldLength保持一致。

使用示例
服务端

public class EchoServer {
    public static void main(String[] args) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            (bossGroup,workerGroup)
                    .channel()
                    .option(ChannelOption.SO_BACKLOG,100)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS,3000)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            ByteBuf delimiter = ("$_".getBytes());
//                            ByteBuf delimiter0 = ("A".getBytes());
//                            ByteBuf delimiter1 = ("B".getBytes());

//                            ().addLast(new DelimiterBasedFrameDecoder(1024,delimiter,delimiter0,delimiter1));
//                            ().addLast(new FixedLengthFrameDecoder(10));

//                            ().addLast(new StringDecoder());
                            ().addLast(new LengthFieldBasedFrameDecoder(65535,0,2,0,2));
                            ().addLast(new MsgpackDecoder());
                            ().addLast(new LengthFieldPrepender(2));
                            ().addLast(new MsgpackEncoder());

                            ().addLast(new EchoServerHandler());
                        }
                    });
            ChannelFuture channelFuture =(7799).sync();
            ().closeFuture().sync();
        } catch (InterruptedException e) {
            ();
        } finally {
            ();
            ();
        }
    }
}
public class EchoServerHandler extends SimpleChannelInboundHandler<Object> {


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        (o);
//        o +="$_";
//        ByteBuf echo = (());
        (o);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ();
        ();
    }

客户端

public class EchoClient {
    public void connect(int port,String host){
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            (group).channel()
                    .option(ChannelOption.TCP_NODELAY,true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            ByteBuf delimiter = ("$_".getBytes());
//                            ().addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
//                            ().addLast(new StringDecoder());
                            ().addLast(new LengthFieldBasedFrameDecoder(65535,0,2,0,2));
                            ().addLast(new MsgpackDecoder());
                            ().addLast(new LengthFieldPrepender(2));
                            ().addLast(new MsgpackEncoder());

                            ().addLast(new EchoClientHandler());
                        }
                    });
            ChannelFuture channelFuture=(host,port).sync();
            ().closeFuture().sync();

        } catch (InterruptedException e) {
            ();
        }finally {
            ();
        }
    }

    public static void main(String[] args) {
        EchoClient echoClient = new EchoClient();
        (7799,"127.0.0.1");
    }
}
public class EchoClientHandler extends SimpleChannelInboundHandler<Object> {
    public static  final String STRINGTAG="HELLO,这是什";
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        List<UserInfo> userInfos = new ArrayList<>();
        for (int i =0;i<2;i++){
            UserInfo userInfo = new UserInfo();
            ("张三"+i);
            (i);
            (userInfo);
            (userInfo);

        }
        ();
//        (userInfos);
//        for (int i=0;i<10;i++){
//            ((()));
//            (3000);
//            (("么你A好吗$_不是B".getBytes()));
//            ((new byte[1024]));
//        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        (o);
    }
}