发生了粘包,我们需要将其清晰的进行拆包处理,这里采用LineBasedFrameDecoder来解决

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

StringDecoder的功能非常简单,就是将接收到的对象转换为字符串,然后继续调用后面的Handler.

LineBasedFrameDecoder+StringDecoder组合就是按行切换的文本解码器。

主要思路是改造客户端和服务端的处理IO的类之前添加相应解码器,解码器之后才去调用IO处理类的Handler。

客户端改造的代码部分

TheClientServer

在处理IO的类initChannel()方法中调用处理IO类前添加相关解码的方法

 public class ClientChildHandler extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024))//添加解码器,遍历ByteBuf并组行
.addLast(new StringDecoder())//添加解码器,将接收到的对象转换为字符串
.addLast(new TimeClientHandler());
}
}

同时在接收请求或相应的channelRead(ChannelHandlerContext ctx, Object msg)方法中进行相关改造,直接用String接收即可,这是因为,在这之前的解码器已经将发送过来的ByteBuf类型进行读取后并转换为String格式了

TimeClientHandler

     @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
/* ByteBuf byteBuf = (ByteBuf) msg;
byte[] req = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(req);
String body = new String(req,"utf-8");*/
String body = (String) msg;
System.out.println("Now is:"+body+";The client count is:"+ ++count);
}

服务端改造的代码部分

TimeServer

 //    IO处理类的初始化
private class ChildHandler extends ChannelInitializer {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline()
.addLast(new LineBasedFrameDecoder(1024))
.addLast(new StringDecoder())
.addLast(new TimeServerHandler());
}
}

TimeServerHandler

    @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
/* ByteBuf byteBuf = (ByteBuf) msg;//将请求转为ByteBuf缓冲区
byte[] req = new byte[byteBuf.readableBytes()];//获取byteBuf的可读字节数
byteBuf.readBytes(req);//将缓冲区字节数组复制到req数组中
String body = new String(req,"utf-8")//转换为字符串
//改造去掉客户端传递过来的换行符号,模拟故障造成粘包问题
.substring(0,req.length-System.lineSeparator().length());*/
String body = (String) msg;
System.out.println("the time server receive order:"+body+"the count is:"+ ++count);
// 处理IO内容
String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
?new Date(System.currentTimeMillis()).toString():"BAD ORDER";
currentTime = currentTime+System.getProperty("line.separator");
ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());//返回客户端的消息转化为ByteBuf对象
ctx.write(resp);//将待应答消息放入缓冲数组中
}

其余代码与上一小节完全相同

运行结果

客户端

 11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:1
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:2
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:3
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:4
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:5
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:6
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:7
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:8
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:9
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:10
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:11
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:12
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:13
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:14
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:15
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:16
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:17
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:18
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:19
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:20
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:21
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:22
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:23
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:24
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:25
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:26
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:27
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:28
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:29
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:30
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:31
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:32
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:33
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:34
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:35
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:36
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:37
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:38
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:39
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:40
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:41
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:42
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:43
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:44
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:45
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:46
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:47
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:48
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:49
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:50
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:51
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:52
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:53
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:54
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:55
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:56
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:57
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:58
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:59
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:60
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:61
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:62
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:63
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:64
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:65
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:66
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:67
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:68
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:69
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:70
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:71
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:72
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:73
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:74
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:75
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:76
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:77
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:78
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:79
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:80
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:81
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:82
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:83
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:84
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:85
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:86
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:87
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:88
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:89
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:90
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:91
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:92
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:93
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:94
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:95
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:96
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:97
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:98
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:99
Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:100

服务端

 11:55:37.276 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@73046a4d
the time server receive order:QUERY TIME ORDERthe count is:1
the time server receive order:QUERY TIME ORDERthe count is:2
the time server receive order:QUERY TIME ORDERthe count is:3
the time server receive order:QUERY TIME ORDERthe count is:4
the time server receive order:QUERY TIME ORDERthe count is:5
the time server receive order:QUERY TIME ORDERthe count is:6
the time server receive order:QUERY TIME ORDERthe count is:7
the time server receive order:QUERY TIME ORDERthe count is:8
the time server receive order:QUERY TIME ORDERthe count is:9
the time server receive order:QUERY TIME ORDERthe count is:10
the time server receive order:QUERY TIME ORDERthe count is:11
the time server receive order:QUERY TIME ORDERthe count is:12
the time server receive order:QUERY TIME ORDERthe count is:13
the time server receive order:QUERY TIME ORDERthe count is:14
the time server receive order:QUERY TIME ORDERthe count is:15
the time server receive order:QUERY TIME ORDERthe count is:16
the time server receive order:QUERY TIME ORDERthe count is:17
the time server receive order:QUERY TIME ORDERthe count is:18
the time server receive order:QUERY TIME ORDERthe count is:19
the time server receive order:QUERY TIME ORDERthe count is:20
the time server receive order:QUERY TIME ORDERthe count is:21
the time server receive order:QUERY TIME ORDERthe count is:22
the time server receive order:QUERY TIME ORDERthe count is:23
the time server receive order:QUERY TIME ORDERthe count is:24
the time server receive order:QUERY TIME ORDERthe count is:25
the time server receive order:QUERY TIME ORDERthe count is:26
the time server receive order:QUERY TIME ORDERthe count is:27
the time server receive order:QUERY TIME ORDERthe count is:28
the time server receive order:QUERY TIME ORDERthe count is:29
the time server receive order:QUERY TIME ORDERthe count is:30
the time server receive order:QUERY TIME ORDERthe count is:31
the time server receive order:QUERY TIME ORDERthe count is:32
the time server receive order:QUERY TIME ORDERthe count is:33
the time server receive order:QUERY TIME ORDERthe count is:34
the time server receive order:QUERY TIME ORDERthe count is:35
the time server receive order:QUERY TIME ORDERthe count is:36
the time server receive order:QUERY TIME ORDERthe count is:37
the time server receive order:QUERY TIME ORDERthe count is:38
the time server receive order:QUERY TIME ORDERthe count is:39
the time server receive order:QUERY TIME ORDERthe count is:40
the time server receive order:QUERY TIME ORDERthe count is:41
the time server receive order:QUERY TIME ORDERthe count is:42
the time server receive order:QUERY TIME ORDERthe count is:43
the time server receive order:QUERY TIME ORDERthe count is:44
the time server receive order:QUERY TIME ORDERthe count is:45
the time server receive order:QUERY TIME ORDERthe count is:46
the time server receive order:QUERY TIME ORDERthe count is:47
the time server receive order:QUERY TIME ORDERthe count is:48
the time server receive order:QUERY TIME ORDERthe count is:49
the time server receive order:QUERY TIME ORDERthe count is:50
the time server receive order:QUERY TIME ORDERthe count is:51
the time server receive order:QUERY TIME ORDERthe count is:52
the time server receive order:QUERY TIME ORDERthe count is:53
the time server receive order:QUERY TIME ORDERthe count is:54
the time server receive order:QUERY TIME ORDERthe count is:55
the time server receive order:QUERY TIME ORDERthe count is:56
the time server receive order:QUERY TIME ORDERthe count is:57
the time server receive order:QUERY TIME ORDERthe count is:58
the time server receive order:QUERY TIME ORDERthe count is:59
the time server receive order:QUERY TIME ORDERthe count is:60
the time server receive order:QUERY TIME ORDERthe count is:61
the time server receive order:QUERY TIME ORDERthe count is:62
the time server receive order:QUERY TIME ORDERthe count is:63
the time server receive order:QUERY TIME ORDERthe count is:64
the time server receive order:QUERY TIME ORDERthe count is:65
the time server receive order:QUERY TIME ORDERthe count is:66
the time server receive order:QUERY TIME ORDERthe count is:67
the time server receive order:QUERY TIME ORDERthe count is:68
the time server receive order:QUERY TIME ORDERthe count is:69
the time server receive order:QUERY TIME ORDERthe count is:70
the time server receive order:QUERY TIME ORDERthe count is:71
the time server receive order:QUERY TIME ORDERthe count is:72
the time server receive order:QUERY TIME ORDERthe count is:73
the time server receive order:QUERY TIME ORDERthe count is:74
the time server receive order:QUERY TIME ORDERthe count is:75
the time server receive order:QUERY TIME ORDERthe count is:76
the time server receive order:QUERY TIME ORDERthe count is:77
the time server receive order:QUERY TIME ORDERthe count is:78
the time server receive order:QUERY TIME ORDERthe count is:79
the time server receive order:QUERY TIME ORDERthe count is:80
the time server receive order:QUERY TIME ORDERthe count is:81
the time server receive order:QUERY TIME ORDERthe count is:82
the time server receive order:QUERY TIME ORDERthe count is:83
the time server receive order:QUERY TIME ORDERthe count is:84
the time server receive order:QUERY TIME ORDERthe count is:85
the time server receive order:QUERY TIME ORDERthe count is:86
the time server receive order:QUERY TIME ORDERthe count is:87
the time server receive order:QUERY TIME ORDERthe count is:88
the time server receive order:QUERY TIME ORDERthe count is:89
the time server receive order:QUERY TIME ORDERthe count is:90
the time server receive order:QUERY TIME ORDERthe count is:91
the time server receive order:QUERY TIME ORDERthe count is:92
the time server receive order:QUERY TIME ORDERthe count is:93
the time server receive order:QUERY TIME ORDERthe count is:94
the time server receive order:QUERY TIME ORDERthe count is:95
the time server receive order:QUERY TIME ORDERthe count is:96
the time server receive order:QUERY TIME ORDERthe count is:97
the time server receive order:QUERY TIME ORDERthe count is:98
the time server receive order:QUERY TIME ORDERthe count is:99
the time server receive order:QUERY TIME ORDERthe count is:100

这样,就解决了粘包的问题。

netty权威指南学习笔记四——TCP粘包/拆包之粘包问题解决的更多相关文章

  1. netty权威指南学习笔记三——TCP粘包/拆包之粘包现象

    TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...

  2. netty权威指南学习笔记六——编解码技术之MessagePack

    编解码技术主要应用在网络传输中,将对象比如BOJO进行编解码以利于网络中进行传输.平常我们也会将编解码说成是序列化/反序列化 定义:当进行远程跨进程服务调用时,需要把被传输的java对象编码为字节数组 ...

  3. netty权威指南学习笔记二——netty入门应用

    经过了前面的NIO基础知识准备,我们已经对NIO有了较大了解,现在就进入netty的实际应用中来看看吧.重点体会整个过程. 按照权威指南写程序的过程中,发现一些问题:当我们在定义handler继承Ch ...

  4. netty权威指南学习笔记五——分隔符和定长解码器的应用

    TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,通常采用以下4中方式: 消息长度固定,累计读取到长度综合为定长LEN的报文后,就认为读取到了一个完整的消息,将计数器置位,重新开始读取下一 ...

  5. netty权威指南学习笔记八——编解码技术之JBoss Marshalling

    JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...

  6. netty权威指南学习笔记一——NIO入门(3)NIO

    经过前面的铺垫,在这一节我们进入NIO编程,NIO弥补了原来同步阻塞IO的不足,他提供了高速的.面向块的I/O,NIO中加入的Buffer缓冲区,体现了与原I/O的一个重要区别.在面向流的I/O中,可 ...

  7. netty权威指南学习笔记一——NIO入门(1)BIO

    公司的一些项目采用了netty框架,为了加速适应公司开发,本博主认真学习netty框架,前一段时间主要看了看书,发现编程这东西,不上手还是觉得差点什么,于是为了加深理解,深入学习,本博主还是决定多动手 ...

  8. netty权威指南学习笔记七——编解码技术之GoogleProtobuf

    首先我们来看一下protobuf的优点: 谷歌长期使用成熟度高: 跨语言支持多种语言如:C++,java,Python: 编码后消息更小,更利于存储传输: 编解码性能高: 支持不同协议版本的兼容性: ...

  9. netty权威指南学习笔记一——NIO入门(2)伪异步IO

    在上一节我们介绍了四种IO相关编程的各个特点,并通过代码进行复习了传统的网络编程代码,伪异步主要是引用了线程池,对BIO中服务端进行了相应的改造优化,线程池的引入,使得我们在应对大量客户端请求的时候不 ...

随机推荐

  1. ch3 盒模型、定位

    标准盒模型.怪异盒模型 外边距叠加 当两个或者争夺垂直外边距相遇时,他们将形成一个外边距,这个外边距的高度等于两个发生叠加的外边距的高度中的较大者. 当一个元素出现在另一个元素上面时,第一个元素的底外 ...

  2. MySQL 之存储引擎与数据类型与数据约束

    一.存储引擎场景 1.InnoDB 用于事务处理应用程序,支持外键和行级锁.如果应用对事物的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操作除了插入和查询之外,还包括很多更新和删除操作,那 ...

  3. Py西游攻关之基础数据类型(一)-数字字符串字节布尔

    Py西游攻关之基础数据类型 - Yuan先生 https://www.cnblogs.com/yuanchenqi/articles/5782764.html 数据类型 计算机顾名思义就是可以做数学计 ...

  4. 一文解读XaaS (转)

    艾克赛斯???别慌,读完你就知道啦~ 服务和云服务 了解Xaas云服务,不妨从了解服务开始. “服务”在本质上是一种租赁,它对资源的占用方式是“为我所用”而非“为我所有”,对资源的消费模式是按需付费而 ...

  5. python面向对象之元类

    目录 元类 造类 第一阶段 第二阶段 造对象 元类 元类(A) ---> 类(B) ---> 实例(C) 对于实例C而言,它是对象,它的类就是类B 对于类B而言,它其实也是对象,那它的类就 ...

  6. 解题报告:luogu P1115(模板 最大子段和)

    题目链接:P1115 最大子段和 告诉你,这个我调了一天的题是橙题...... 线性容易得到,放篇题解: #include<bits/stdc++.h> using namespace s ...

  7. docker学习笔记-05:DockerFile解析

    一.DockerFile是什么 1.DockerFile是用来构建docker镜像的构建文件,是由一系列参数和命令构成的脚本. 2.构建三步骤: 手动编写一个dockerfile文件,然后直接dock ...

  8. @vue-cli的安装及vue项目创建

    1.安装 Node.js & Vue CLI @vue/cli3,是vue-进行搭建的脚手架项目,它本质上是一个全局安装的 npm 包,通过安装它,可以为终端提供 vue 命令,进行vue项目 ...

  9. 学习 Ansible Playbook,有这篇文章就够了!

    https://mp.weixin.qq.com/s?__biz=MzAwNTM5Njk3Mw==&mid=2247487361&idx=1&sn=b50327df2949e4 ...

  10. PCA算法提取人脸识别特征脸(降噪)

    PCA算法可以使得高维数据(mxn)降到低维,而在整个降维的过程中会丢失一定的信息,也会因此而实现降噪除噪的效果,另外,它通过降维可以计算出原本数据集的主成分分量Wk矩阵(kxn),如果将其作为数据样 ...