《精通并发与Netty》学习笔记(14 - 解决TCP粘包拆包(二)Netty自定义协议解决粘包拆包)
一、Netty粘包和拆包解决方案
Netty提供了多个解码器,可以进行分包的操作,分别是:
* LineBasedFrameDecoder (换行)
LineBasedFrameDecoder是回车换行解码器,如果用户发送的消息以回车换行符作为消息结束的标识,则可以直接使用Netty的LineBasedFrameDecoder对消息进行解码,只需要在初始化Netty服务端或者客户端时将LineBasedFrameDecoder正确的添加到ChannelPipeline中即可,不需要自己重新实现一套换行解码器。
LineBasedFrameDecoder的工作原理是它依次遍历ByteBuf中的可读字节,判断看是否有“\n”或者“\r\n”,如果有,就以此位置为结束位置,从可读索引到结束位置区间的字节就组成了一行。它是以换行符为结束标志的解码器,支持携带结束符或者不携带结束符两种解码方式,同时支持配置单行的最大长度。如果连续读取到最大长度后仍然没有发现换行符,就会抛出异常,同时忽略掉之前读到的异常码流。防止由于数据报没有携带换行符导致接收到ByteBuf无限制积压,引起系统内存溢出。
* DelimiterBasedFrameDecoder(添加特殊分隔符报文来分包)
DelimiterBasedFrameDecoder是分隔符解码器,用户可以指定消息结束的分隔符,它可以自动完成以分隔符作为码流结束标识的消息的解码。
回车换行解码器实际上是一种特殊的DelimiterBasedFrameDecoder解码器。
* FixedLengthFrameDecoder(使用定长的报文来分包)
FixedLengthFrameDecoder是固定长度解码器,它能够按照指定的长度对消息进行自动解码,开发者不需要考虑TCP的粘包/拆包等问题,非常实用。
对于定长消息,如果消息实际长度小于定长,则往往会进行补位操作,它在一定程度上导致了空间和资源的浪费。但是它的优点也是非常明显的,编解码比较简单,因此在实际项目中仍然有一定的应用场景。
* LengthFieldBasedFrameDecoder (自定义解码器跟编码器)
本文介绍的重点LengthFieldBasedFrameDecoder,一般包含了消息头(head)、消息体(body):消息头是固定的长度,一般有有以下信息 -> 是否压缩(zip)、消息类型(type or cmdid)、消息体长度(body length);消息体长度不是固定的,其大小由消息头记载,一般记载业务交互信息。
netty对应来说就是编码器(Encoder)跟解码器(Decoder),一般其中会有一个基本消息类对外输出
二、实例演示
首先编写自定义协议类:
package com.spring.netty.handler2; /**
* 自定义Person协议
*/
public class PersonProtocol {
private int length;
private byte[] content; public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
}
新建服务端代码:
package com.spring.netty.handler2; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; public class MyServer {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(); try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).
childHandler(new MyServerInitializer()); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package com.spring.netty.handler2; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; public class MyServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new MyPersonDecoder());
pipeline.addLast(new MyPersonEncoder()); pipeline.addLast(new MyServerHandler());
}
}
package com.spring.netty.handler2; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder; import java.util.List; /**
* Person解码器
*/
public class MyPersonDecoder extends ReplayingDecoder<Void> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
System.out.println("MyPersonDecoder decode invoked!"); int length = in.readInt();
byte[] content = new byte[length];
in.readBytes(content); PersonProtocol personProtocol = new PersonProtocol();
personProtocol.setLength(length);
personProtocol.setContent(content);
out.add(personProtocol);
}
}
package com.spring.netty.handler2; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; /**
* 编码器
*/
public class MyPersonEncoder extends MessageToByteEncoder<PersonProtocol> {
@Override
protected void encode(ChannelHandlerContext ctx, PersonProtocol msg, ByteBuf out) throws Exception {
System.out.println("MyPersonEncoder encode invoked!"); out.writeInt(msg.getLength());
out.writeBytes(msg.getContent());
}
}
package com.spring.netty.handler2; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import java.nio.charset.Charset;
import java.util.UUID; public class MyServerHandler extends SimpleChannelInboundHandler<PersonProtocol> {
private int count;
@Override
protected void channelRead0(ChannelHandlerContext ctx, PersonProtocol msg) throws Exception {
int length = msg.getLength();
byte[] content = msg.getContent();
System.out.println("服务端接收到的数据:");
System.out.println("长度:"+length);
System.out.println("内容:"+new String(content, Charset.forName("utf-8")));
System.out.println("服务端接收到的消息数量:"+(++this.count));
//服务端向客户端返回uuid
String responseMessage = UUID.randomUUID().toString();
int responseLength = responseMessage.getBytes("utf-8").length;
byte[] responseContent = responseMessage.getBytes("utf-8"); PersonProtocol personProtocol = new PersonProtocol();
personProtocol.setLength(length);
personProtocol.setContent(content);
ctx.writeAndFlush(responseContent);
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
编写客户端程序:
package com.spring.netty.handler2; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; public class MyClient {
public static void main(String[] args) throws Exception {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new MyPersonDecoder());
pipeline.addLast(new MyPersonEncoder()); pipeline.addLast(new MyClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
}
package com.spring.netty.handler2; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import java.nio.charset.Charset; public class MyClientHandler extends SimpleChannelInboundHandler<PersonProtocol> {
private int count; @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for(int i=0;i<10;++i){
String messageToBeSent = "sent from client";
byte[] content = messageToBeSent.getBytes(Charset.forName("utf-8"));
int length = messageToBeSent.getBytes(Charset.forName("utf-8")).length; PersonProtocol personProtocol = new PersonProtocol();
personProtocol.setLength(length);
personProtocol.setContent(content); ctx.writeAndFlush(personProtocol);
}
} //从服务器端接收数据
@Override
protected void channelRead0(ChannelHandlerContext ctx, PersonProtocol msg) throws Exception {
int length = msg.getLength();
byte[] content = msg.getContent();
System.out.println("客户端接收到的数据:");
System.out.println("长度:"+length);
System.out.println("内容:"+new String(content, Charset.forName("utf-8")));
System.out.println("客户端接收到的消息数量:"+(++this.count));
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
分别启动服务端和客户端查看效果:
服务端效果如图:

客户端效果如图:

《精通并发与Netty》学习笔记(14 - 解决TCP粘包拆包(二)Netty自定义协议解决粘包拆包)的更多相关文章
- SAS学习笔记14 利用SAS绘制地图(二)
笔记9讲过利用SAS绘制地图,这次接着讲 用中国各地(不含港澳台)的平均湿度数据来绘制地图 在地图上标出地名 宏%maplabel有9个参数,依次为:地图文件名.包含区域名称的数据集文件.输出的注释数 ...
- 并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理
1. 概述 1.1 什么是线程池 与jdbc连接池类似,在创建线程池或销毁线程时,会消耗大量的系统资源,因此在java中提出了线程池的概念,预先创建好固定数量的线程,当有任务需要线程去执行时,不用再去 ...
- Netty学习笔记-入门版
目录 Netty学习笔记 前言 什么是Netty IO基础 概念说明 IO简单介绍 用户空间与内核空间 进程(Process) 线程(thread) 程序和进程 进程切换 进程阻塞 文件描述符 文件句 ...
- SQL反模式学习笔记14 关于Null值的使用
目标:辨别并使用Null值 反模式:将Null值作为普通的值,反之亦然 1.在表达式中使用Null: Null值与空字符串是不一样的,Null值参与任何的加.减.乘.除等其他运算,结果都是Null: ...
- Ext.Net学习笔记14:Ext.Net GridPanel Grouping用法
Ext.Net学习笔记14:Ext.Net GridPanel Grouping用法 Ext.Net GridPanel可以进行Group操作,例如: 如何启用Grouping功能呢?只需要在Grid ...
- Netty学习笔记(二) 实现服务端和客户端
在Netty学习笔记(一) 实现DISCARD服务中,我们使用Netty和Python实现了简单的丢弃DISCARD服务,这篇,我们使用Netty实现服务端和客户端交互的需求. 前置工作 开发环境 J ...
- Netty 学习笔记(1)通信原理
前言 本文主要从 select 和 epoll 系统调用入手,来打开 Netty 的大门,从认识 Netty 的基础原理 —— I/O 多路复用模型开始. Netty 的通信原理 Netty 底层 ...
- golang学习笔记14 golang substring 截取字符串
golang学习笔记14 golang substring 截取字符串golang 没有java那样的substring函数,但支持直接根据 index 截取字符串mystr := "hel ...
- mybatis学习笔记(14)-查询缓存之中的一个级缓存
mybatis学习笔记(14)-查询缓存之中的一个级缓存 标签: mybatis mybatis学习笔记14-查询缓存之中的一个级缓存 查询缓存 一级缓存 一级缓存工作原理 一级缓存測试 一级缓存应用 ...
- Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )
!/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...
随机推荐
- c++类的基本形式(一个简单类的简单sample,命名空间)
有人把类说成是占用固定大小内存块的别名,其定义时不占用空间 #include<iostream> #include<string> using namespace std; c ...
- P3474 [POI2008]KUP-Plot purchase
思路:单调栈 提交:>5次 错因:单调栈写法有问题+前缀和写错 题解: 若有\(>=k\ \&\&\ <=2\times k\)的点,显然直接选他就行了. 否则,我们 ...
- 004_linux驱动之_class_create创建一个设备类
(一)解析:class_create函数和class_destroy函数 创建一个类 和 删除一个类. (二)class_create函数原型 struc ...
- VueCropper 图片裁剪
基于vue的图片裁剪vue-cropper 简小咖 关注 0.2 2018.12.12 15:42 字数 164 阅读 3900评论 1喜欢 3 vue-cropper官网http://xyxiao ...
- SP1716 GSS3(线段树+矩阵乘法)
Code: #include <bits/stdc++.h> #define N 50001 #define ll long long #define lson now<<1 ...
- 51nod 1020
求 $n$ 个数的排列中逆序数为 $k$ 的排列数$f[n][k]$ 表示 $n$ 个数的排列中逆序数为 $k$ 的排列数$f[n][k] = \sum_{i = 0}^{n - 1} f[n - 1 ...
- xgzc— math 专题训练(一)
Lucas定理 当\(p\)是质数时,有\((^n_m)\equiv(^{n/p}_{m/p}) * (^{n\%p}_{m\%p}) \pmod{p}\) 狄利克雷卷积 定义:\((f*g)(n)= ...
- 一图了解DLL和SYS的区别
The following diagram shows the device node, kernel-mode device stack, and the user-mode device stac ...
- WEB测试重点及视频教程
WEB测试重点如下: 1.WEB测试基础-2.理解网络协议-3.HTTP协议详解-4.WEB前段分析-5WEB安全性测试-6.WEB兼容性及可用性测试. 1.通常需要承受长时间的大量操作,因此web项 ...
- 使用setUncaughtExceptionHandler在线程外面捕获异常
package com.dwz.concurrency.chapter11; /** * Thread的run方法是不能throw出异常的,只能在日志或者console中打印出来 */ public ...