发生了粘包,我们需要将其清晰的进行拆包处理,这里采用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. 学习黑马教学视频SSM整合中Security遇到的问题org.springframework.security.access.AccessDeniedException: Access is denied

    问题已解决. 总结: 报错:org.springframework.security.access.AccessDeniedException: Access is denied 当您遇到同样问题时, ...

  2. 设计模式课程 设计模式精讲 15-2 桥接模式Coding

    1 代码演练 1.1 代码演练1 1.2 代码演练2   1 代码演练 1.1 代码演练1 需求: 打印出从银行获取的账号类 优点: a 假如我只用用一个银行接口 去获取账号的内容,银行实现类要有定期 ...

  3. LNMP一键安装包 PHP自动升级脚本

    LNMP一键安装包 PHP自动升级脚本 2011年03月15日 上午 | 作者:VPS侦探 前一段时间完成了lnmp一键安装包的PHP自动升级脚本,今天发布出来,如果想升级PHP版本的lnmp用户可以 ...

  4. hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  5. 每天一点点之vue框架 watch监听变量(深度监听)

    <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type="text& ...

  6. Linux和云供应商Red Hat被IBM以34亿美元的价格收购

    导读 今天的主题包括IBM以340亿美元收购Red Hat,人性化是使人工智能成功的关键.两家公司于10月28日宣布,IBM正以340亿美元的价格收购Linux和云技术供应商Red Hat,以期改变云 ...

  7. Linux centos7 awk工具

    一.awk介绍 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk含盖sed所有功能,把文件逐行的读入,以空格为默认分隔 ...

  8. Gradient descend 梯度下降法和归一化、python中的实现(未完善)

    梯度下降法是优化函数参数最常用.简单的算法 通常就是将一组输入样本的特征$x^i$传入目标函数中,如$f(x) = wx + b$,再计算每个样本通过函数预测的值$f(x^i)$与其真实值(标签)$y ...

  9. Java安全中的“大坑”,跨平台真“浮云”

    Java安全HttpDB 最近在做一个开源项目HttpDB,它的目标是在互联网中通过JDBC安全的查询数据库,解决云计算报表的数据库访问问题. 数据传输使用AES加密算法,用到了Java提供的安全库j ...

  10. 编写第一个JavaScript程序

    编写第一个程序 在 HTML 页面中嵌入 JavaScript 脚本需要使用 <script> 标签,用户可以在 <script> 标签中直接编写 JavaScript 代码, ...