TCP是一个基于“流”的协议,所谓流就像河里的水没有严格的界限。TCP底层并不了解上层业务的具体实现,它会根据TCP缓冲区的具体状况进行划分,因此就可能会出现业务上一个完整的TCP包可能会拆分红多个小包进行发送,也可能多个小包被组装成一个大包进行发送,从而致使了TCP中的粘包和半包问题。socket
经过添加DelimiterBasedFrameDecoder来解决粘包半包问题ide
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获取固定长度字符来解决粘包半包问题netty
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来消息头和消息体来解决粘包和半包问题。code