netty权威指南学习笔记四——TCP粘包/拆包之粘包问题解决
发生了粘包,我们需要将其清晰的进行拆包处理,这里采用LineBasedFrameDecoder来解决
LineBasedFrameDecoder的工作原理是它依次遍历ByteBuf中的可读字节,判断看是否有“\n”或“\r\n”,如果有,就以此为结束位置,从可读索引到结束位置区间的字节就组成一行,它是以换行为结束标志的编码器,支持携带结束符或者不携带结束符两种方式,同时支持配置单行最大长度,如果连续读取到的最大长度后仍没有发现换行符,就会抛出异常,同时忽略掉之前读到的异常码流。
StringDecoder的功能非常简单,就是将接收到的对象转换为字符串,然后继续调用后面的Handler.
LineBasedFrameDecoder+StringDecoder组合就是按行切换的文本解码器。
主要思路是改造客户端和服务端的处理IO的类之前添加相应解码器,解码器之后才去调用IO处理类的Handler。
客户端改造的代码部分
TheClientServer
在处理IO的类initChannel()方法中调用处理IO类前添加相关解码的方法
1 public class ClientChildHandler extends ChannelInitializer<SocketChannel>{ 2 @Override 3 protected void initChannel(SocketChannel socketChannel) throws Exception { 4 socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024))//添加解码器,遍历ByteBuf并组行 5 .addLast(new StringDecoder())//添加解码器,将接收到的对象转换为字符串 6 .addLast(new TimeClientHandler()); 7 } 8 }
同时在接收请求或相应的channelRead(ChannelHandlerContext ctx, Object msg)方法中进行相关改造,直接用String接收即可,这是因为,在这之前的解码器已经将发送过来的ByteBuf类型进行读取后并转换为String格式了
TimeClientHandler
1 @Override 2 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 3 /* ByteBuf byteBuf = (ByteBuf) msg; 4 byte[] req = new byte[byteBuf.readableBytes()]; 5 byteBuf.readBytes(req); 6 String body = new String(req,"utf-8");*/ 7 String body = (String) msg; 8 System.out.println("Now is:"+body+";The client count is:"+ ++count); 9 }
服务端改造的代码部分
TimeServer
1 // IO处理类的初始化 2 private class ChildHandler extends ChannelInitializer { 3 @Override 4 protected void initChannel(Channel channel) throws Exception { 5 channel.pipeline() 6 .addLast(new LineBasedFrameDecoder(1024)) 7 .addLast(new StringDecoder()) 8 .addLast(new TimeServerHandler()); 9 } 10 }
TimeServerHandler
1 @Override 2 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 3 /* ByteBuf byteBuf = (ByteBuf) msg;//将请求转为ByteBuf缓冲区 4 byte[] req = new byte[byteBuf.readableBytes()];//获取byteBuf的可读字节数 5 byteBuf.readBytes(req);//将缓冲区字节数组复制到req数组中 6 String body = new String(req,"utf-8")//转换为字符串 7 //改造去掉客户端传递过来的换行符号,模拟故障造成粘包问题 8 .substring(0,req.length-System.lineSeparator().length());*/ 9 String body = (String) msg; 10 System.out.println("the time server receive order:"+body+"the count is:"+ ++count); 11 // 处理IO内容 12 String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) 13 ?new Date(System.currentTimeMillis()).toString():"BAD ORDER"; 14 currentTime = currentTime+System.getProperty("line.separator"); 15 ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());//返回客户端的消息转化为ByteBuf对象 16 ctx.write(resp);//将待应答消息放入缓冲数组中 17 }
其余代码与上一小节完全相同
运行结果
客户端
1 11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2 2 11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16 3 11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8 4 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:1 5 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:2 6 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:3 7 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:4 8 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:5 9 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:6 10 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:7 11 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:8 12 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:9 13 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:10 14 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:11 15 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:12 16 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:13 17 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:14 18 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:15 19 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:16 20 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:17 21 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:18 22 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:19 23 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:20 24 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:21 25 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:22 26 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:23 27 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:24 28 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:25 29 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:26 30 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:27 31 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:28 32 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:29 33 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:30 34 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:31 35 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:32 36 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:33 37 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:34 38 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:35 39 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:36 40 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:37 41 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:38 42 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:39 43 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:40 44 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:41 45 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:42 46 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:43 47 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:44 48 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:45 49 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:46 50 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:47 51 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:48 52 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:49 53 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:50 54 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:51 55 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:52 56 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:53 57 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:54 58 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:55 59 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:56 60 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:57 61 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:58 62 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:59 63 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:60 64 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:61 65 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:62 66 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:63 67 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:64 68 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:65 69 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:66 70 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:67 71 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:68 72 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:69 73 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:70 74 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:71 75 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:72 76 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:73 77 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:74 78 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:75 79 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:76 80 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:77 81 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:78 82 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:79 83 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:80 84 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:81 85 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:82 86 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:83 87 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:84 88 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:85 89 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:86 90 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:87 91 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:88 92 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:89 93 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:90 94 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:91 95 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:92 96 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:93 97 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:94 98 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:95 99 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:96 100 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:97 101 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:98 102 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:99 103 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:100
服务端
1 11:55:37.276 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@73046a4d 2 the time server receive order:QUERY TIME ORDERthe count is:1 3 the time server receive order:QUERY TIME ORDERthe count is:2 4 the time server receive order:QUERY TIME ORDERthe count is:3 5 the time server receive order:QUERY TIME ORDERthe count is:4 6 the time server receive order:QUERY TIME ORDERthe count is:5 7 the time server receive order:QUERY TIME ORDERthe count is:6 8 the time server receive order:QUERY TIME ORDERthe count is:7 9 the time server receive order:QUERY TIME ORDERthe count is:8 10 the time server receive order:QUERY TIME ORDERthe count is:9 11 the time server receive order:QUERY TIME ORDERthe count is:10 12 the time server receive order:QUERY TIME ORDERthe count is:11 13 the time server receive order:QUERY TIME ORDERthe count is:12 14 the time server receive order:QUERY TIME ORDERthe count is:13 15 the time server receive order:QUERY TIME ORDERthe count is:14 16 the time server receive order:QUERY TIME ORDERthe count is:15 17 the time server receive order:QUERY TIME ORDERthe count is:16 18 the time server receive order:QUERY TIME ORDERthe count is:17 19 the time server receive order:QUERY TIME ORDERthe count is:18 20 the time server receive order:QUERY TIME ORDERthe count is:19 21 the time server receive order:QUERY TIME ORDERthe count is:20 22 the time server receive order:QUERY TIME ORDERthe count is:21 23 the time server receive order:QUERY TIME ORDERthe count is:22 24 the time server receive order:QUERY TIME ORDERthe count is:23 25 the time server receive order:QUERY TIME ORDERthe count is:24 26 the time server receive order:QUERY TIME ORDERthe count is:25 27 the time server receive order:QUERY TIME ORDERthe count is:26 28 the time server receive order:QUERY TIME ORDERthe count is:27 29 the time server receive order:QUERY TIME ORDERthe count is:28 30 the time server receive order:QUERY TIME ORDERthe count is:29 31 the time server receive order:QUERY TIME ORDERthe count is:30 32 the time server receive order:QUERY TIME ORDERthe count is:31 33 the time server receive order:QUERY TIME ORDERthe count is:32 34 the time server receive order:QUERY TIME ORDERthe count is:33 35 the time server receive order:QUERY TIME ORDERthe count is:34 36 the time server receive order:QUERY TIME ORDERthe count is:35 37 the time server receive order:QUERY TIME ORDERthe count is:36 38 the time server receive order:QUERY TIME ORDERthe count is:37 39 the time server receive order:QUERY TIME ORDERthe count is:38 40 the time server receive order:QUERY TIME ORDERthe count is:39 41 the time server receive order:QUERY TIME ORDERthe count is:40 42 the time server receive order:QUERY TIME ORDERthe count is:41 43 the time server receive order:QUERY TIME ORDERthe count is:42 44 the time server receive order:QUERY TIME ORDERthe count is:43 45 the time server receive order:QUERY TIME ORDERthe count is:44 46 the time server receive order:QUERY TIME ORDERthe count is:45 47 the time server receive order:QUERY TIME ORDERthe count is:46 48 the time server receive order:QUERY TIME ORDERthe count is:47 49 the time server receive order:QUERY TIME ORDERthe count is:48 50 the time server receive order:QUERY TIME ORDERthe count is:49 51 the time server receive order:QUERY TIME ORDERthe count is:50 52 the time server receive order:QUERY TIME ORDERthe count is:51 53 the time server receive order:QUERY TIME ORDERthe count is:52 54 the time server receive order:QUERY TIME ORDERthe count is:53 55 the time server receive order:QUERY TIME ORDERthe count is:54 56 the time server receive order:QUERY TIME ORDERthe count is:55 57 the time server receive order:QUERY TIME ORDERthe count is:56 58 the time server receive order:QUERY TIME ORDERthe count is:57 59 the time server receive order:QUERY TIME ORDERthe count is:58 60 the time server receive order:QUERY TIME ORDERthe count is:59 61 the time server receive order:QUERY TIME ORDERthe count is:60 62 the time server receive order:QUERY TIME ORDERthe count is:61 63 the time server receive order:QUERY TIME ORDERthe count is:62 64 the time server receive order:QUERY TIME ORDERthe count is:63 65 the time server receive order:QUERY TIME ORDERthe count is:64 66 the time server receive order:QUERY TIME ORDERthe count is:65 67 the time server receive order:QUERY TIME ORDERthe count is:66 68 the time server receive order:QUERY TIME ORDERthe count is:67 69 the time server receive order:QUERY TIME ORDERthe count is:68 70 the time server receive order:QUERY TIME ORDERthe count is:69 71 the time server receive order:QUERY TIME ORDERthe count is:70 72 the time server receive order:QUERY TIME ORDERthe count is:71 73 the time server receive order:QUERY TIME ORDERthe count is:72 74 the time server receive order:QUERY TIME ORDERthe count is:73 75 the time server receive order:QUERY TIME ORDERthe count is:74 76 the time server receive order:QUERY TIME ORDERthe count is:75 77 the time server receive order:QUERY TIME ORDERthe count is:76 78 the time server receive order:QUERY TIME ORDERthe count is:77 79 the time server receive order:QUERY TIME ORDERthe count is:78 80 the time server receive order:QUERY TIME ORDERthe count is:79 81 the time server receive order:QUERY TIME ORDERthe count is:80 82 the time server receive order:QUERY TIME ORDERthe count is:81 83 the time server receive order:QUERY TIME ORDERthe count is:82 84 the time server receive order:QUERY TIME ORDERthe count is:83 85 the time server receive order:QUERY TIME ORDERthe count is:84 86 the time server receive order:QUERY TIME ORDERthe count is:85 87 the time server receive order:QUERY TIME ORDERthe count is:86 88 the time server receive order:QUERY TIME ORDERthe count is:87 89 the time server receive order:QUERY TIME ORDERthe count is:88 90 the time server receive order:QUERY TIME ORDERthe count is:89 91 the time server receive order:QUERY TIME ORDERthe count is:90 92 the time server receive order:QUERY TIME ORDERthe count is:91 93 the time server receive order:QUERY TIME ORDERthe count is:92 94 the time server receive order:QUERY TIME ORDERthe count is:93 95 the time server receive order:QUERY TIME ORDERthe count is:94 96 the time server receive order:QUERY TIME ORDERthe count is:95 97 the time server receive order:QUERY TIME ORDERthe count is:96 98 the time server receive order:QUERY TIME ORDERthe count is:97 99 the time server receive order:QUERY TIME ORDERthe count is:98 100 the time server receive order:QUERY TIME ORDERthe count is:99 101 the time server receive order:QUERY TIME ORDERthe count is:100
这样,就解决了粘包的问题。
本博主支持并坚持原创,本博客文章将以原创为主。