zoukankan      html  css  js  c++  java
  • Netty使用解码器Decoder解决TCP粘包和拆包问题

    解码器Decoder和ChannelHandler的关系

    netty的解码器通常是继承自ByteToMessageDecoder,而它又是继承自ChannelInboundHandlerAdapter,其实也是一种ChannelHandler和我们自定义的ChannelHandler一样都是来处理进入或者出去的数据。常用的几种解码器有:

    • LineBasedFrameDecoder
    • DelimiterBasedFrameDecoder
    • FixedLengthFrameDecoder

    LineBasedFrameDecoder

    LineBasedFrameDecoder 行解码器,遍历ByteBuf中的可读字节,按行( )处理

    StringDecoder

    StringDecoder将接受的码流转换为字符串

    代码中使用

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            //LineBasedFrameDecoder遍历ByteBuf中的可读字节,按行(
     
    )处理
            pipeline.addLast(new LineBasedFrameDecoder(1024));
            //StringDecoder将接受的码流转换为字符串
            pipeline.addLast(new StringDecoder());
            pipeline.addLast(new NettyServerHandler());
        }
    

    NettyServerHandler处理类中读取,String message = (String) msg;直接转换为String:

        private int count = 0;
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            String message = (String) msg;
            LOGGER.info("client received message {}:{}", ++count, message);
        }
    

    DelimiterBasedFrameDecoder

    DelimiterBasedFrameDecoder,将特定分隔符作为码流结束标志的解码器。

    代码中使用

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            ByteBuf byteBuf = Unpooled.copiedBuffer("$_".getBytes());
            pipeline.addLast(new DelimiterBasedFrameDecoder(1024,true,true,byteBuf));
            pipeline.addLast(new StringDecoder());
            pipeline.addLast(new NettyServerHandler());
        }
    

    FixedLengthFrameDecoder

    FixedLengthFrameDecoder 固定长度解码器,只会读取指定长度的码流。

    代码中使用

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new FixedLengthFrameDecoder(24));
            pipeline.addLast(new StringDecoder());
            pipeline.addLast(new NettyServerHandler());
        }
    
  • 相关阅读:
    【转】ASP.NET页面刷新方法总结
    GridView点击超链接时弹出小窗口
    VS2008增加AJAX Control Toolkit3.5支持(组图)
    水晶报表中"已达到系统管理员配置的最大报表处理作业数限制"错误的处理
    asp.net 在使用Response.Redirect try{}catch{}块失效
    【转】ASP.NET的五大数据控件浅谈
    ASP.NET生成随机密码
    ASP.NET Web应用程序和ASP.NET网站的区别
    使用ArcGIS GP服务之一框架介绍
    mysql索引的类型和优缺点
  • 原文地址:https://www.cnblogs.com/monkjavaer/p/11215837.html
Copyright © 2011-2022 走看看