netty如何解决粘包和半包


TCP是一个基于“流”的协议,所谓流就像河里的水没有严格的界限。TCP底层并不了解上层业务的具体实现,它会根据TCP缓冲区的具体情况进行划分,所以就可能会出现业务上一个完整的TCP包可能会拆分成多个小包进行发送,也可能多个小包被组装成一个大包进行发送,从而导致了TCP中的粘包和半包问题。

解决方案

  1. 消息定长,每发送一次消息,在接收消息的同时截取固定长度的字节。
  2. 以某种分隔符进行分割。
  3. 把消息封装成消息头和消息体。

netty中的粘包和半包解决方案

消息定长格式解决

通过添加DelimiterBasedFrameDecoder来解决粘包半包问题

b.group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .handler(new LoggingHandler())
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        //socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(10));
                        socketChannel.pipeline().addLast(new StringDecoder());
                        socketChannel.pipeline().addLast(new EchoServerHandler());
                    }
                });
根据特定字符分割解决

通过添加FixedLengthFrameDecoder获取固定长度字符来解决粘包半包问题

 b.group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .handler(new LoggingHandler())
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        //ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        //socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(10));
                        socketChannel.pipeline().addLast(new StringDecoder());
                        socketChannel.pipeline().addLast(new EchoServerHandler());
                    }
                });
根据消息头和消息体封装解决

通过ObjectDecoder和ObjectEncoder来消息头和消息体来解决粘包和半包问题。

转载于:https://my.oschina.net/u/1787735/blog/1930053

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值