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中服务端进行了相应的改造优化,线程池的引入,使得我们在应对大量客户端请求的时候不 ...
随机推荐
- 线程安全Collections.synchronizedList
ollections.synchronizedList引发的线程安全问题 有些容器是线程安全的(Vector,ConcurrentLinkedQueue等),有些则不是(list等),利用类 似 pr ...
- redis服务器性能优化
1.系统内存OOM优化 vm.overcommit_memory Redis会占用非常大内存,所以通常需要关闭系统的OOM,方法为将“/proc/sys/vm/overcommit_memory”的值 ...
- Py西游攻关之基础数据类型(二)-列表
Py西游攻关之基础数据类型 - Yuan先生 https://www.cnblogs.com/yuanchenqi/articles/5782764.html 五 List(列表) OK,现在我们知 ...
- Centos7 配置subversion
CentOS7:配置SVN服务器 Posted on 2016-11-10 15:17 eastson 阅读(4266) 评论(0) 编辑 收藏 1. 安装 CentOS通过yum安装subversi ...
- 巧用DOS命令合并多个文本文件的内容
假设,在网上下载了一本小说.这本小说是由100多个文本文件组成的.这个时候,将这100多个文本文件的内容全部合并到一个文本文件中,阅读起来就会显得很方便 (1)首先,使用本书中“批量按序更改文 ...
- python的线性代数
估计线性模型中的系数:a=np.linalg.lstsq(x,b),有b=a*x 求方阵的逆矩阵np.linalg.inv(A) 求广义逆矩阵:np.linalg.pinv(A) 求矩阵的行列式:np ...
- CXL联盟正式成立:成员均是行业巨头
导读 今天,阿里巴巴.思科.戴尔EMC.Facebook.Google.HPE.华为.Intel.微软(按英文首字母排序)联合宣布,CXL联盟(Compute Express Link Consort ...
- Java-android使用GridView布局的电子相册&服务器获取图片
转 http://www.tuicool.com/articles/B7JNv2 电子相册的思路: 1.先是考虑布局,我用的是GridView布局 2.GridView中又该怎么显示图片,其实我的这 ...
- input文件类型上传,或者作为参数拼接的时候注意的问题!
1.ajax请求参数如果为文本类型,直接拼接即可.如果为file类型就需要先获取文件信息 2.获取文件信息: HTML代码: <div class="form-group"& ...
- ActivePerl 安装
下载 https://www.activestate.com/products/activeperl/downloads/ 链接:https://pan.baidu.com/s/1IXPdYFd5bD ...