netty权威指南学习笔记四——TCP粘包/拆包之粘包问题解决
发生了粘包,我们需要将其清晰的进行拆包处理,这里采用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粘包/拆包之粘包问题解决的更多相关文章
- netty权威指南学习笔记三——TCP粘包/拆包之粘包现象
TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...
- netty权威指南学习笔记六——编解码技术之MessagePack
编解码技术主要应用在网络传输中,将对象比如BOJO进行编解码以利于网络中进行传输.平常我们也会将编解码说成是序列化/反序列化 定义:当进行远程跨进程服务调用时,需要把被传输的java对象编码为字节数组 ...
- netty权威指南学习笔记二——netty入门应用
经过了前面的NIO基础知识准备,我们已经对NIO有了较大了解,现在就进入netty的实际应用中来看看吧.重点体会整个过程. 按照权威指南写程序的过程中,发现一些问题:当我们在定义handler继承Ch ...
- netty权威指南学习笔记五——分隔符和定长解码器的应用
TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,通常采用以下4中方式: 消息长度固定,累计读取到长度综合为定长LEN的报文后,就认为读取到了一个完整的消息,将计数器置位,重新开始读取下一 ...
- netty权威指南学习笔记八——编解码技术之JBoss Marshalling
JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...
- netty权威指南学习笔记一——NIO入门(3)NIO
经过前面的铺垫,在这一节我们进入NIO编程,NIO弥补了原来同步阻塞IO的不足,他提供了高速的.面向块的I/O,NIO中加入的Buffer缓冲区,体现了与原I/O的一个重要区别.在面向流的I/O中,可 ...
- netty权威指南学习笔记一——NIO入门(1)BIO
公司的一些项目采用了netty框架,为了加速适应公司开发,本博主认真学习netty框架,前一段时间主要看了看书,发现编程这东西,不上手还是觉得差点什么,于是为了加深理解,深入学习,本博主还是决定多动手 ...
- netty权威指南学习笔记七——编解码技术之GoogleProtobuf
首先我们来看一下protobuf的优点: 谷歌长期使用成熟度高: 跨语言支持多种语言如:C++,java,Python: 编码后消息更小,更利于存储传输: 编解码性能高: 支持不同协议版本的兼容性: ...
- netty权威指南学习笔记一——NIO入门(2)伪异步IO
在上一节我们介绍了四种IO相关编程的各个特点,并通过代码进行复习了传统的网络编程代码,伪异步主要是引用了线程池,对BIO中服务端进行了相应的改造优化,线程池的引入,使得我们在应对大量客户端请求的时候不 ...
随机推荐
- C++ — 后缀表达式转表达式树
2018-07-21 16:57:26 update 建立表达式树的基本思路:方法类似由下而上建立堆的思想,所以时间复杂度为O(n),这样算法就会变得很简单,只用考虑处理需要入栈的节点和栈中的节点即可 ...
- [libpng]CMake+VS2015下编译libpng,及使用小例
编译前的工作 在编译libpng前,需要把zlib编译好,并加载到编译环境里. CMake + VS2015 下编译zlib,及使用小例 下载与解压 libpng的官网是 http://www.lib ...
- 如何使用gcc_clang进行C语言的编译_编译的流程是什么?
编译命令 gcc/clang -g -O2 -o -c test test.c -I... -L... -l -g : 输出文件中的调试信息 -O : 对输出文件做出指令优化,默认是O1, O2优化更 ...
- Flask - 数据库相关
1. Flask-SQLAlchemy 1.1 参考: http://flask-sqlalchemy.pocoo.org/2.3/ https://github.com/janetat/flasky ...
- S32K144之FlexMem,FlexNVM,FlexRAM,System RAM, SRAM 区别与联系
参考手册中常常见到有关memory的关键字,如FlexMem,FlexNVM,FlexRAM,System RAM, SRAM,那么它们到底是什么意思呢?有什么区别和联系? 参考资料 [1]S32K1 ...
- hbase meta中分区信息错误的记录
bulk write hbase 时遇到下面的错误: 19/03/20 02:16:02 ERROR LoadIncrementalHFiles: IOException during splitti ...
- 一文解读CDN (转)
如今这个移动互联网时代,越来越多的人使用手机观看视频,丰富自己的娱乐生活. 可是,大家在追剧的时候,有没有想过一个问题——为什么有时候明明自己手机的网速很快,但观看视频时,仍然卡顿? 回答这个问题之前 ...
- [转]ubuntu备份与恢复
在 使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过Windows系统,那么你一定记忆犹新:首先需要找到一个备份工 具(通常都是私有软件),然后重启电脑进入备份工具提供 ...
- 洛谷 P2891 [USACO07OPEN]吃饭Dining
裸的最大流. #include <cstdio> #include <cstring> #include <queue> const int MAXN = 4e3 ...
- 1-Docker学习笔记
docker还是比较容易的,比较蛋疼的就是镜像网络问题,不过也可以配置了镜像加速器(比如阿里云).这里重点记录一下初学docker时遇到的知识点. docker环境变量 docker对环境变量的定义和 ...