关键词:
目录
一、消息粘包和消息半包的概述
1.1、消息粘包
- 当缓冲区足够大,由于网络不稳定种种原因,可能会有多条消息从通道读入缓冲区,此时如果无法分清数据包之间的界限,就会导致粘包问题。
1.2、消息半包
- 若消息没有接收完,缓冲区就被填满了,会导致从缓冲区取出的消息不完整,即半包的现象。
二、粘包现象代码示例
2.1、粘包现象服务端示例代码
-
服务端代码
package com.example.nettytest.netty.day5; import io.netty.bootstrap.ServerBootstrap; 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.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; /** * @description: Netty粘包现象演示服务端 * 消息粘包:当缓冲区足够大,由于网络不稳定种种原因,可能会有多条消息从通道读入缓冲区, * 此时如果无法分清数据包之间的界限,就会导致粘包问题; * @author: xz */ @Slf4j public class NettyServerTest public static void main(String[] args) new NettyServerTest().start(); void start() NioEventLoopGroup boss = new NioEventLoopGroup(1); NioEventLoopGroup worker = new NioEventLoopGroup(); try ServerBootstrap serverBootstrap = new ServerBootstrap() .channel(NioServerSocketChannel.class) .group(boss, worker) .childHandler(new ChannelInitializer<SocketChannel>() @Override protected void initChannel(SocketChannel ch) throws Exception 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");
2.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 lombok.extern.slf4j.Slf4j; /** * @description: Netty粘包现象演示客户端 * @author: xz */ @Slf4j public class NettyClientTest public static void main(String[] args) new NettyClientTest().start(); void start() NioEventLoopGroup worker = new NioEventLoopGroup(); try Bootstrap bootstrap = new Bootstrap() .channel(NioSocketChannel.class) .group(worker) .handler(new ChannelInitializer<SocketChannel>() @Override protected void initChannel(SocketChannel ch) throws Exception log.debug("connetted》》》》》》》》》》》》》》》"); ch.pipeline().addLast(new ChannelInboundHandlerAdapter() //会在连接channel建立成功后,触发active事件 @Override public void channelActive(ChannelHandlerContext ctx) throws Exception log.debug("遍历 sending 每次发送16个字节》》》》》》》》》》》》》》》"); for (int i = 0; i < 10; i++) //设置缓冲区大小16个字节 ByteBuf buffer = ctx.alloc().buffer(16); buffer.writeBytes(new byte[]1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); ctx.writeAndFlush(buffer); ); ); ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync(); channelFuture.channel().closeFuture().sync(); catch (InterruptedException e) log.error("client error", e); finally worker.shutdownGracefully();
2.3、分别启动服务端,客户端,查看服务端结果输出
- 先启动服务端
- 再启动客户端
- 再次查看服务端
注:可下图输出结果可知:服务器端的某次输出,可以看到一次就接收了 160 个字节,而非分 10 次接收。
三、半包现象代码示例
3.1、半包现象服务端示例代码
-
为现象明显,服务端添加一下接收缓冲区,其它代码不变
serverBootstrap.option(ChannelOption.SO_RCVBUF, 10);
-
服务端完整示例代码
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.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; /** * @description: Netty半包现象演示服务端 * 消息半包:若消息没有接收完,缓冲区就被填满了,会导致从缓冲区取出的消息不完整,即半包的现象。 * @author: xz */ @Slf4j public class NettyServerTest1 public static void main(String[] args) new NettyServerTest().start(); void start() NioEventLoopGroup boss = new NioEventLoopGroup(1); NioEventLoopGroup worker = new NioEventLoopGroup(); try ServerBootstrap serverBootstrap = new ServerBootstrap() .channel(NioServerSocketChannel.class) //服务端添加接收缓冲区,大小设置10 .option(ChannelOption.SO_RCVBUF, 10) .group(boss, worker) .childHandler(new ChannelInitializer<SocketChannel>() @Override protected void initChannel(SocketChannel ch) throws Exception ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); ch.pipeline().addLast(new ChannelInboundHandlerAdapter() @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======================");
3.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 lombok.extern.slf4j.Slf4j; /** * @description: Netty半包现象演示客户端 * @author: xz */ @Slf4j public class NettyClientTest1 public static void main(String[] args) new NettyClientTest1().start(); void start() NioEventLoopGroup worker = new NioEventLoopGroup(); try Bootstrap bootstrap = new Bootstrap() .channel(NioSocketChannel.class) .group(worker) .handler(new ChannelInitializer<SocketChannel>() @Override protected void initChannel(SocketChannel ch) throws Exception log.debug("connetted----------------"); ch.pipeline().addLast(new ChannelInboundHandlerAdapter() //会在连接channel建立成功后,触发active事件 @Override public void channelActive(ChannelHandlerContext ctx) throws Exception log.debug("sending----------------"); for (int i = 0; i < 10; i++) ByteBuf buffer = ctx.alloc().buffer(16); buffer.writeBytes(new byte[]1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); ctx.writeAndFlush(buffer)
netty进阶——粘包与半包(短链接方式解决粘包问题)(代码片段) 目录一、短链接方式解决粘包问题(代码示例)1.1、短链接方式解决粘包问题的服务端代码示例1.2、短链接方式解决粘包问题的客户端代码示例1.3、分别启动服务端,客户端,查看服务端结果输出一、短链接方式... 查看详情
netty进阶——粘包与半包(固定长度方式解决粘包问题)(代码片段)
目录一、固定长度方式解决粘包问题(代码示例)1.1、固定长度方式解决粘包问题的服务端代码示例1.2、固定长度方式解决粘包问题的客户端代码示例1.3、分别启动服务端,客户端,查看服务端结果输出一、固定... 查看详情
netty进阶——粘包与半包(固定长度方式解决粘包问题)(代码片段)
目录一、固定长度方式解决粘包问题(代码示例)1.1、固定长度方式解决粘包问题的服务端代码示例1.2、固定长度方式解决粘包问题的客户端代码示例1.3、分别启动服务端,客户端,查看服务端结果输出一、固定... 查看详情
netty进阶——粘包与半包(预设长度方式解决粘包问题)(代码片段)
目录一、预设长度方式解决粘包问题(代码示例)1.1、预设长度方式解决粘包问题的服务端代码示例1.2、预设长度方式解决粘包问题的客户端代码示例1.3、分别启动服务端,客户端,查看服务端结果输出一、预设... 查看详情
netty进阶——粘包与半包(预设长度方式解决粘包问题)(代码片段)
目录一、预设长度方式解决粘包问题(代码示例)1.1、预设长度方式解决粘包问题的服务端代码示例1.2、预设长度方式解决粘包问题的客户端代码示例1.3、分别启动服务端,客户端,查看服务端结果输出一、预设... 查看详情
netty进阶——粘包与半包(固定分隔符方式解决粘包问题)(代码片段)
目录一、固定分隔符方式解决粘包问题(代码示例)1.1、固定分隔符解决粘包问题的服务端代码示例1.2、固定分隔符方式解决粘包问题的客户端代码示例1.3、分别启动服务端,客户端,查看服务端结果输出一、固... 查看详情
netty进阶——粘包与半包(固定分隔符方式解决粘包问题)(代码片段)
目录一、固定分隔符方式解决粘包问题(代码示例)1.1、固定分隔符解决粘包问题的服务端代码示例1.2、固定分隔符方式解决粘包问题的客户端代码示例1.3、分别启动服务端,客户端,查看服务端结果输出一、固... 查看详情
netty进阶——粘包与半包(现象分析)
目录一、粘包1.1、粘包现象1.2、粘包原因二、半包2.1、半包现象2.2、半包原因三、粘包与半包现象的本质原因四、MSS限制的了解五、Nagle算法的了解一、粘包1.1、粘包现象发送abcdef,接收abcdef1.2、粘包原因应用层:接收方... 查看详情
netty进阶——粘包与半包(现象分析)
目录一、粘包1.1、粘包现象1.2、粘包原因二、半包2.1、半包现象2.2、半包原因三、粘包与半包现象的本质原因四、MSS限制的了解五、Nagle算法的了解一、粘包1.1、粘包现象发送abcdef,接收abcdef1.2、粘包原因应用层:接收方... 查看详情
透过现象看本质,我找到了netty粘包与半包的这几种解决方案。(代码片段)
1、粘包与半包啥也不说了,直接上代码是不是有点不太友好,我所谓了,都快过年了,还要啥自行车我上来就是一段代码猛如虎1.1服务器代码publicclassStudyServerstaticfinalLoggerlog=LoggerFactory.getLogger(StudyServer.class);voidstart()NioEventLoopGrou... 查看详情
netty进阶——粘包与半包(滑动窗口)
TCP以一个段(segment)为单位,每发送一个段就需要进行一次确认应答(ack)处理,但如果这么做,缺点是包的往返时间越长性能就越差。为了解决此问题,引入了窗口概念,窗口大小即决定了... 查看详情
netty进阶——粘包与半包(滑动窗口)
TCP以一个段(segment)为单位,每发送一个段就需要进行一次确认应答(ack)处理,但如果这么做,缺点是包的往返时间越长性能就越差。为了解决此问题,引入了窗口概念,窗口大小即决定了... 查看详情
netty03-进阶(代码片段)
三.Netty进阶1.粘包与半包1.1粘包现象服务端代码publicclassHelloWorldServerstaticfinalLoggerlog=LoggerFactory.getLogger(HelloWorldServer.class);voidstart()NioEventLoopGroupboss=newNioEventLoopGroup(1);NioEventLoopGroupworker=newNioEventLoopGroup();tryServerBootstrapserverBo... 查看详情
tcp粘包与半包的核心
进行Socket编程时经常会碰到 TCP的粘包与半包问题,很多时候我们选用netty等框架而不直接采用原生的Socket编程也是因为netty帮我们将该类传输过程中可能出现的问题屏蔽掉了,使我们可以抽出更多精力来关注功能的实现... 查看详情
netty——bytebuffer消息粘包半包示例(代码片段)
目录一、ByteBuffer消息粘包、消息半包的概述二、示例需求三、示例代码一、ByteBuffer消息粘包、消息半包的概述NIO是面向缓冲区进行通信的,不是面向流的。既然是缓冲区,那它一定存在一个固定大小。这样一来通常会遇... 查看详情
netty——bytebuffer消息粘包半包示例(代码片段)
目录一、ByteBuffer消息粘包、消息半包的概述二、示例需求三、示例代码一、ByteBuffer消息粘包、消息半包的概述NIO是面向缓冲区进行通信的,不是面向流的。既然是缓冲区,那它一定存在一个固定大小。这样一来通常会遇... 查看详情
粘包和半包有了解过吗?netty是如何解决这个问题的(代码片段)
...列文章,学习或面试都可以看看(一)什么是粘包、半包在实际的网络开发中或者在面试中,最开始使用TCP协议时经常会碰上粘包和半包的情况,因此我们有必要了解一下什么是粘包,什么是半包,以... 查看详情
粘包和半包有了解过吗?netty是如何解决这个问题的(代码片段)
...列文章,学习或面试都可以看看(一)什么是粘包、半包在实际的网络开发中或者在面试中,最开始使用TCP协议时经常会碰上粘包和半包的情况,因此我们有必要了解一下什么是粘包,什么是半包,以... 查看详情