Netty 是如何解决 TCP 粘包拆包的?

开发 后端
对于这个数据拆分成大包小包的问题就是我们今天要讲的粘包和拆包的问题。

 我们都知道TCP是基于字节流的传输协议。

那么数据在通信层传播其实就像河水一样并没有明显的分界线,而数据具体表示什么意思什么地方有句号什么地方有分号这个对于TCP底层来说并不清楚。应用层向TCP层发送用于网间传输的、用8位字节表示的数据流,然后TCP把数据流分区成适当长度的报文段,之后TCP把结果包传给IP层,由它来通过网络将包传送给接收端实体的TCP层。

所以对于这个数据拆分成大包小包的问题就是我们今天要讲的粘包和拆包的问题。

1、TCP粘包拆包问题说明

粘包和拆包这两个概念估计大家还不清楚,通过下面这张图我们来分析一下:

假设客户端分别发送两个数据包D1,D2个服务端,但是发送过程中数据是何种形式进行传播这个并不清楚,分别有下列4种情况:

  1.  服务端一次接受到了D1和D2两个数据包,两个包粘在一起,称为粘包;
  2.  服务端分两次读取到数据包D1和D2,没有发生粘包和拆包;
  3.  服务端分两次读到了数据包,第一次读到了D1和D2的部分内容,第二次读到了D2的剩下部分,这个称为拆包;
  4.  服务器分三次读到了数据部分,第一次读到了D1包,第二次读到了D2包的部分内容,第三次读到了D2包的剩下内容。

2、TCP粘包产生原因

我们知道在TCP协议中,应用数据分割成TCP认为最适合发送的数据块,这部分是通过“MSS”(最大数据包长度)选项来控制的,通常这种机制也被称为一种协商机制,MSS规定了TCP传往另一端的最大数据块的长度。

这个值TCP协议在实现的时候往往用MTU值代替(需要减去IP数据包包头的大小20Bytes和TCP数据段的包头20Bytes)所以往往MSS为1460。通讯双方会根据双方提供的MSS值得最小值确定为这次连接的最大MSS值。

tcp为提高性能,发送端会将需要发送的数据发送到缓冲区,等待缓冲区满了之后,再将缓冲中的数据发送到接收方。同理,接收方也有缓冲区这样的机制,来接收数据。

发生粘包拆包的原因主要有以下这些:

  1.  应用程序写入数据的字节大小大于套接字发送缓冲区的大小将发生拆包;
  2.  进行MSS大小的TCP分段。MSS是TCP报文段中的数据字段的最大长度,当TCP报文长度-TCP头部长度>mss的时候将发生拆包;
  3.  应用程序写入数据小于套接字缓冲区大小,网卡将应用多次写入的数据发送到网络上,将发生粘包;
  4.  数据包大于MTU的时候将会进行切片。MTU即(Maxitum Transmission Unit) 最大传输单元,由于以太网传输电气方面的限制,每个以太网帧都有最小的大小64bytes最大不能超过1518bytes,刨去以太网帧的帧头14Bytes和帧尾CRC校验部分4Bytes,那么剩下承载上层协议的地方也就是Data域最大就只能有1500Bytes这个值我们就把它称之为MTU。这个就是网络层协议非常关心的地方,因为网络层协议比如IP协议会根据这个值来决定是否把上层传下来的数据进行分片。

3、如何解决TCP粘包拆包

我们知道tcp是无界的数据流,且协议本身无法避免粘包,拆包的发生,那我们只能在应用层数据协议上,加以控制。通常在制定传输数据时,可以使用如下方法:

  1.  设置定长消息,服务端每次读取既定长度的内容作为一条完整消息;
  2.  使用带消息头的协议、消息头存储消息开始标识及消息长度信息,服务端获取消息头的时候解析出消息长度,然后向后读取该长度的内容;
  3.  设置消息边界,服务端从网络流中按消息边界分离出消息内容。比如在消息末尾加上换行符用以区分消息结束。

当然应用层还有更多复杂的方式可以解决这个问题,这个就属于网络层的问题了,我们还是用java提供的方式来解决这个问题。Spring Boot 学习笔记分享给你,我们先看一个例子看看粘包是如何发生的。

服务端: 

  1. public class HelloWordServer {  
  2.     private int port;  
  3.     public HelloWordServer(int port) {  
  4.         this.port = port;  
  5.     }  
  6.     public void start(){  
  7.         EventLoopGroup bossGroup = new NioEventLoopGroup();  
  8.         EventLoopGroup workGroup = new NioEventLoopGroup();   
  9.         ServerBootstrap server = new ServerBootstrap().group(bossGroup,workGroup)  
  10.                                     .channel(NioServerSocketChannel.class) 
  11.                                     .childHandler(new ServerChannelInitializer());  
  12.         try {  
  13.             ChannelFuture future = server.bind(port).sync();  
  14.             future.channel().closeFuture().sync();  
  15.         } catch (InterruptedException e) {  
  16.             e.printStackTrace();  
  17.         }finally {  
  18.             bossGroup.shutdownGracefully();  
  19.             workGroup.shutdownGracefully();  
  20.         }  
  21.     }   
  22.     public static void main(String[] args) {  
  23.         HelloWordServer server = new HelloWordServer(7788);  
  24.         server.start();  
  25.     }  

服务端Initializer: 

  1. public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {  
  2.     @Override  
  3.     protected void initChannel(SocketChannel socketChannel) throws Exception {  
  4.         ChannelPipeline pipeline = socketChannel.pipeline();  
  5.         // 字符串解码 和 编码  
  6.         pipeline.addLast("decoder", new StringDecoder());  
  7.         pipeline.addLast("encoder", new StringEncoder());  
  8.         // 自己的逻辑Handler  
  9.         pipeline.addLast("handler", new HelloWordServerHandler());  
  10.     }  

服务端handler: 

  1. public class HelloWordServerHandler extends ChannelInboundHandlerAdapter {  
  2.     private int counter;  
  3.     @Override  
  4.     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
  5.         String body = (String)msg;  
  6.         System.out.println("server receive order : " + body + ";the counter is: " + ++counter);  
  7.     }  
  8.     @Override  
  9.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
  10.         super.exceptionCaught(ctx, cause);  
  11.     }  

客户端: 

  1. public class HelloWorldClient {  
  2.     private  int port;  
  3.     private  String address;  
  4.     public HelloWorldClient(int port,String address) {  
  5.         this.port = port;  
  6.         this.address = address;  
  7.     }  
  8.     public void start(){  
  9.         EventLoopGroup group = new NioEventLoopGroup();  
  10.         Bootstrap bootstrap = new Bootstrap();  
  11.         bootstrap.group(group)  
  12.                 .channel(NioSocketChannel.class)  
  13.                 .handler(new ClientChannelInitializer());  
  14.         try {  
  15.             ChannelFuture future = bootstrap.connect(address,port).sync();       
  16.              future.channel().closeFuture().sync();  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace(); 
  19.          }finally {  
  20.             group.shutdownGracefully();  
  21.         }   
  22.     }  
  23.     public static void main(String[] args) {  
  24.         HelloWorldClient client = new HelloWorldClient(7788,"127.0.0.1");  
  25.         client.start();  
  26.     }  

客户端Initializer: 

  1. public class ClientChannelInitializer extends  ChannelInitializer<SocketChannel> {  
  2.     protected void initChannel(SocketChannel socketChannel) throws Exception {  
  3.         ChannelPipeline pipeline = socketChannel.pipeline();  
  4.         pipeline.addLast("decoder", new StringDecoder());  
  5.         pipeline.addLast("encoder", new StringEncoder());  
  6.         // 客户端的逻辑 
  7.         pipeline.addLast("handler", new HelloWorldClientHandler());  
  8.     }  

客户端handler: 

  1. public class HelloWorldClientHandler extends ChannelInboundHandlerAdapter {  
  2.     private byte[] req;  
  3.     private int counter;  
  4.     public BaseClientHandler() {  
  5.         req = ("Unless required by applicable law or agreed to in writing, software\n" +  
  6.                 "  distributed under the License is distributed on an \"AS IS\" BASIS,\n" +  
  7.                 "  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +  
  8.                 "  See the License for the specific language governing permissions and\n" +  
  9.                 "  limitations under the License.This connector uses the BIO implementation that requires the JSSE\n" +  
  10.                 "  style configuration. When using the APR/native implementation, the\n" +  
  11.                 "  penSSL style configuration is required as described in the APR/native\n" +  
  12.                 "  documentation.An Engine represents the entry point (within Catalina) that processes\n" +  
  13.                 "  every request.  The Engine implementation for Tomcat stand alone\n" +  
  14.                 "  analyzes the HTTP headers included with the request, and passes them\n" +  
  15.                 "  on to the appropriate Host (virtual host)# Unless required by applicable law or agreed to in writing, software\n" +  
  16.                 "# distributed under the License is distributed on an \"AS IS\" BASIS,\n" +  
  17.                 "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +  
  18.                 "# See the License for the specific language governing permissions and\n" +  
  19.                 "# limitations under the License.# For example, set the org.apache.catalina.util.LifecycleBase logger to log\n" +  
  20.                 "# each component that extends LifecycleBase changing state:\n" +  
  21.                 "#org.apache.catalina.util.LifecycleBase.level = FINE 
  22.                 ).getBytes(); 
  23.     }  
  24.     @Override  
  25.     public void channelActive(ChannelHandlerContext ctx) throws Exception {  
  26.         ByteBuf message;  
  27.         //将上面的所有字符串作为一个消息体发送出去  
  28.         message = Unpooled.buffer(req.length);  
  29.         message.writeBytes(req);  
  30.         ctx.writeAndFlush(message);  
  31.     }  
  32.     @Override  
  33.     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
  34.         String buf = (String)msg;  
  35.         System.out.println("Now is : " + buf + " ; the counter is : "+ (++counter));  
  36.     }  
  37.     @Override  
  38.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
  39.         ctx.close();  
  40.     }  

运行客户端和服务端我们能看到:

我们看到这个长长的字符串被截成了2段发送,这就是发生了拆包的现象。同样粘包我们也很容易去模拟,我们把BaseClientHandler中的channelActive方法里面的: 

  1. message = Unpooled.buffer(req.length);  
  2. message.writeBytes(req);  
  3. ctx.writeAndFlush(message); 

这几行代码是把我们上面的一长串字符转成的byte数组写进流里发送出去,那么我们可以在这里把上面发送消息的这几行循环几遍这样发送的内容增多了就有可能在拆包的时候把上一条消息的一部分分配到下一条消息里面了,修改如下: 

  1. for (int i = 0; i < 3; i++) {  
  2.     message = Unpooled.buffer(req.length);  
  3.     message.writeBytes(req);  
  4.     ctx.writeAndFlush(message);  

改完之后我们再运行一下,输出太长不好截图,我们在输出结果中能看到循环3次之后的消息服务端收到的就不是之前的完整的一条了,而是被拆分了4次发送。

对于上面出现的粘包和拆包的问题,Netty已有考虑,并且有实施的方案:LineBasedFrameDecoder。另外,微信搜索Java技术栈,在后台回复:面试,可以获取我整理的 Java 系列面试题和答案。

我们重新改写一下ServerChannelInitializer: 

  1. public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {  
  2.     @Override 
  3.     protected void initChannel(SocketChannel socketChannel) throws Exception {  
  4.         ChannelPipeline pipeline = socketChannel.pipeline();  
  5.         pipeline.addLast(new LineBasedFrameDecoder(2048));         
  6.         // 字符串解码 和 编码  
  7.         pipeline.addLast("decoder", new StringDecoder());  
  8.         pipeline.addLast("encoder", new StringEncoder());   
  9.         // 自己的逻辑Handler  
  10.         pipeline.addLast("handler", new BaseServerHandler());  
  11.     }  

新增:pipeline.addLast(new LineBasedFrameDecoder(2048))。同时,我们还得对上面发送的消息进行改造BaseClientHandler: 

  1. public class BaseClientHandler extends ChannelInboundHandlerAdapter {  
  2.     private byte[] req;  
  3.     private int counter;  
  4.     req = ("Unless required by applicable dfslaw or agreed to in writing, software" +  
  5.                 "  distributed under the License is distributed on an \"AS IS\" BASIS," +  
  6.                 "  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." +  
  7.                 "  See the License for the specific language governing permissions and" +  
  8.                 "  limitations under the License.This connector uses the BIO implementation that requires the JSSE" +  
  9.                 "  style configuration. When using the APR/native implementation, the" +  
  10.                 "  penSSL style configuration is required as described in the APR/native" +  
  11.                 "  documentation.An Engine represents the entry point (within Catalina) that processes" +  
  12.                 "  every request.  The Engine implementation for Tomcat stand alone" +  
  13.                 "  analyzes the HTTP headers included with the request, and passes them" +  
  14.                 "  on to the appropriate Host (virtual host)# Unless required by applicable law or agreed to in writing, software" +  
  15.                 "# distributed under the License is distributed on an \"AS IS\" BASIS," +  
  16.                 "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." +  
  17.                 "# See the License for the specific language governing permissions and" +  
  18.                 "# limitations under the License.# For example, set the org.apache.catalina.util.LifecycleBase logger to log" +  
  19.                 "# each component that extends LifecycleBase changing state:" +  
  20.                 "#org.apache.catalina.util.LifecycleBase.level = FINE\n"  
  21.                 ).getBytes();     
  22.     @Override  
  23.     public void channelActive(ChannelHandlerContext ctx) throws Exception {  
  24.         ByteBuf message;  
  25.         message = Unpooled.buffer(req.length);  
  26.         message.writeBytes(req);  
  27.         ctx.writeAndFlush(message);  
  28.     }  
  29.     @Override  
  30.     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
  31.         String buf = (String)msg;  
  32.         System.out.println("Now is : " + buf + " ; the counter is : "+ (++counter));  
  33.     }  
  34.     @Override 
  35.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
  36.         ctx.close();  
  37.     }  

去掉所有的”\n”,只保留字符串末尾的这一个。原因稍后再说。channelActive方法中我们不必再用循环多次发送消息了,只发送一次就好(第一个例子中发送一次的时候是发生了拆包的),然后我们再次运行,大家会看到这么长一串字符只发送了一串就发送完毕。程序输出我就不截图了。下面来解释一下LineBasedFrameDecoder。

LineBasedFrameDecoder的工作原理是它依次遍历ByteBuf 中的可读字节,判断看是否有”\n” 或者” \r\n”,如果有,就以此位置为结束位置,从可读索引到结束位置区间的字节就组成了一行。它是以换行符为结束标志的解码器。支持携带结束符或者不携带结束符两种解码方式,同时支持配置单行的最大长度。如果连续读取到最大长度后仍然没有发现换行符,就会抛出异常,同时忽略掉之前读到的异常码流。这个对于我们确定消息最大长度的应用场景还是很有帮助。

对于上面的判断看是否有”\n” 或者” \r\n”以此作为结束的标志我们可能回想,要是没有”\n” 或者” \r\n”那还有什么别的方式可以判断消息是否结束呢。别担心,Netty对于此已经有考虑,还有别的解码器可以帮助我们解决问题,另外,关注公众号Java技术栈,在后台回复:面试,可以获取我整理的 Java 系列面试题和答案,非常齐全。 

 

责任编辑:庞桂玉 来源: Java技术栈
相关推荐

2020-01-06 15:23:41

NettyTCP粘包

2021-03-09 22:30:47

TCP拆包协议

2019-10-17 11:06:32

TCP粘包通信协议

2020-12-23 07:53:01

TCP通信Netty

2020-10-15 18:31:36

理解Netty编解码

2022-04-28 08:38:09

TCP协议解码器

2019-10-25 00:32:12

TCP粘包Netty

2019-10-24 07:35:13

TCP粘包Netty

2021-01-13 10:18:29

SocketNetty粘包

2019-08-15 07:43:38

TCP网络协议丢包

2020-03-10 08:27:24

TCP粘包网络协议

2022-08-01 07:07:15

粘包半包封装

2021-10-08 09:38:57

NettyChannelHand架构

2020-12-30 09:04:32

Go语言TCPUDP

2020-04-09 11:08:30

PyFlinkJAR依赖

2020-12-29 08:34:08

spring循环依赖开发

2021-01-30 19:35:44

HDFS单点Hadoop

2022-06-08 08:01:20

useEffect数组函数

2018-05-17 09:40:56

区块链身份识别身份验证

2019-11-05 10:55:05

RPM包RPMLinux
点赞
收藏

51CTO技术栈公众号