发生了粘包,我们需要将其清晰的进行拆包处理,这里采用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. Linux centosVMware shell编程 for循环、while循环、break跳出循环、continue结束本次循环、exit退出整个脚本

    一.for循环 语法:for 变量名 in 条件; do …; done 案例1 #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] ec ...

  2. js给元素添加样式[addClass][hasClass]

    function addClass(el, className) { if (hasClass(el, className)) { return } let newClass = el.classNa ...

  3. redis介绍及搭建

    redis介绍 Redis最适合所有数据in-momory的场景,虽然Redis也提供持久化功能,但实际更多的是一个disk-backed(以写入磁盘的方式进行同步,实现持久化)的功能,跟传统意义上的 ...

  4. JS array delete splice 区别

    Delete in this case will only set the element as undefined: > myArray =['a','b','c','d'] >dele ...

  5. 135、Java中的静态块,构造方法和构造块

    01.代码如下: package TIANPAN; class Book { static String msg; // static属性,暂不封装 public Book() { // 构造方法 S ...

  6. window和document的区别理解,bom和dom的区别理解

    Window对象: 是整个BOM的核心,所有对象和集合都以某种方式回接到window对象.Window对象表示整个浏览器窗口,但不必表示其中包含的内容. Document对象: 实际上是window对 ...

  7. linux修改键盘按键

    我的电脑:Fedora-19 $ uname -a Linux localhost.localdomain 3.11.10-200.fc19.i686 #1 SMP Mon Dec 2 20:48:2 ...

  8. java小项目之:抽奖系统!java初学者必备(内附源码)

    [Java]Java摇奖源码,Java抽奖源码,Java随机抽奖源码 任务描述 本次任务要求为某商场开发一套幸运抽奖系统,客户必须首先注册成为该商场会员,会员登录成功后,就可以参加抽奖活动了.注册 用 ...

  9. 「SCOI2010」幸运数字

    传送门 Luogu 解题思路 首先构造出所有的幸运数字. 然后考虑一个幸运数字会产生多少贡献. 对于一个数 \(x\),它在区间 \([l,r]\) 内的倍数的个数为 \(\lfloor \frac{ ...

  10. MAC电脑如何播放.SWF文件

    很简单,不需要专门的播放器,只需要将.swf文件直接拖拽到浏览器页面就可以播放了. 亲测safari , 谷歌chrome浏览器,火狐浏览器 ,都是可以的 下面是图示 step1 电脑上找到swf文件 ...