netty 拆包和粘包 (三)
在tcp编程底层都有拆包和粘包的机制
拆包
当发送数据量过大时数据量会分多次发送
以前面helloWord代码为例
package com.liqiang.nettyTest2; public class nettyMain {
public static void main(String[] args) {
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Server server = new Server(8081);
server.start();
}
}).start();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Client client1=new Client("127.0.0.1", 8081);
client1.connection();
client1.sendMsg("In this chapter you general, we recommend Java Concurrency in Practice by Brian Goetz. His book w"
+ "ill give We’ve reached an exciting point—in the next chapter we’ll discuss bootstrapping, the process "
+ "of configuring and connecting all of Netty’s components to bring your learned about threading models in ge"
+ "neral and Netty’s threading model in particular, whose performance and consistency advantages we discuss"
+ "ed in detail In this chapter you general, we recommend Java Concurrency in Practice by Brian Goetz. Hi"
+ "s book will give We’ve reached an exciting point—in the next chapter we’ll discuss bootstrapping, the"
+ " process of configuring and connecting all of Netty’s components to bring your learned about threading "
+ "models in general and Netty’s threading model in particular, whose performance and consistency advantag"
+ "es we discussed in detailIn this chapter you general, we recommend Java Concurrency in Practice by Bri"
+ "an Goetz. His book will give We’ve reached an exciting point—in the next chapter;the counter is: 1 2222"
+ "sdsa ddasd asdsadas dsadasdas");
}
}).start();
}
}
打印
可以发现这里拆分成了2次发送
粘包
当发送数据量过小时会组合成一次发送
package com.liqiang.nettyTest2; public class nettyMain {
public static void main(String[] args) {
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Server server = new Server(8081);
server.start();
}
}).start();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Client client1=new Client("127.0.0.1", 8081);
client1.connection();
for(int i=0;i<100;i++) {
client1.sendMsg("d");
} }
}).start();
}
}
可以发现有时多条发送的数据会组合成一条发送
解决方案
netty提供了解码器来解决拆包和粘包的问题
LineBasedFrameDecoder
通过换行符来区分包
服务器端增加LineBasedFrameDecoder
package com.liqiang.nettyTest2; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.AsciiHeadersEncoder.NewlineType;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder; public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> { private Server server;
public ServerChannelInitializer(Server server) {
this.server=server;
}
@Override
protected void initChannel(SocketChannel channel) throws Exception {
// TODO Auto-generated method stub
channel.pipeline()
.addLast(new LineBasedFrameDecoder(2048))//2048是限制一个包的最大字节数。如果超过将会报异常
.addLast("decoder",new StringDecoder())//接收到数据 自动将将buffer转换为String 避免自己再转
.addLast("encoder",new StringEncoder())//发送数据 可以直接发送String 框架内部转换为buffer传输
.addLast(new ServerHandle(server));
} }
客户端
package com.liqiang.nettyTest2; public class nettyMain {
public static void main(String[] args) {
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Server server = new Server(8081);
server.start(); }
}).start();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Client client1=new Client("127.0.0.1", 8081);
client1.connection();
client1.sendMsg("In this chapter you general, we recommend Java Concurrency in Practice by Brian Goetz. His book w"
+ "ill give We’ve reached an exciting point—in the next chapter we’ll discuss bootstrapping, the process "
+ "of configuring and connecting all of Netty’s components to bring your learned about threading models in ge"
+ "neral and Netty’s threading model in particular, whose performance and consistency advantages we discuss"
+ "ed in detail In this chapter you general, we recommend Java Concurrency in Practice by Brian Goetz. Hi"
+ "s book will give We’ve reached an exciting point—in the next chapter we’ll discuss bootstrapping, the"
+ " process of configuring and connecting all of Netty’s components to bring your learned about threading "
+ "models in general and Netty’s threading model in particular, whose performance and consistency advantag"
+ "es we discussed in detailIn this chapter you general, we recommend Java Concurrency in Practice by Bri"
+ "an Goetz. His book will give We’ve reached an exciting point—in the next chapter;the counter is: 1 2222"
+ "sdsa ddasd asdsadas dsadasdas"+System.getProperty("line.separator"));//System.getProperty("line.separator") 兼容linux和windows换行符的区别
}
}).start();
}
}
如果发送数据忘记拼换行符 服务器将不会有任何打印
DelimiterBasedFrameDecoder
通过指定符号区分
服务器端
package com.liqiang.nettyTest2; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.AsciiHeadersEncoder.NewlineType;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder; public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> { private Server server;
public ServerChannelInitializer(Server server) {
this.server=server;
}
@Override
protected void initChannel(SocketChannel channel) throws Exception {
// TODO Auto-generated method stub
channel.pipeline()
.addLast(new DelimiterBasedFrameDecoder(2048,Unpooled.copiedBuffer("$$__".getBytes())))//$__为包的分隔符2048是限制一个包的最大字节数。如果超过将会报异常
.addLast("decoder",new StringDecoder())//接收到数据 自动将将buffer转换为String 避免自己再转
.addLast("encoder",new StringEncoder())//发送数据 可以直接发送String 框架内部转换为buffer传输
.addLast(new ServerHandle(server));
} }
客户端
package com.liqiang.nettyTest2; public class nettyMain {
public static void main(String[] args) {
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Server server = new Server(8081);
server.start(); }
}).start();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Client client1=new Client("127.0.0.1", 8081);
client1.connection();
client1.sendMsg("In this chapter you general, we recommend Java Concurrency in Practice by Brian Goetz. His book w"
+ "ill give We’ve reached an exciting point—in the next chapter we’ll discuss bootstrapping, the process "
+ "of configuring and connecting all of Netty’s components to bring your learned about threading models in ge"
+ "neral and Netty’s threading model in $$__particular, whose performance and consistency advantages we discuss"
+ "ed in detail In this chapter you general, we recommend Java Concurrency in Practice by Brian Goetz. Hi"
+ "s book will give We’ve reached an exciting point—in the next chapter we’ll discuss bootstrapping, the"
+ " process of configuring and connecting all $$__of Netty’s components to bring your learned about threading "
+ "models in general and Netty’s threading model in particular, whose performance and consistency advantag"
+ "es we discussed in detailIn this chapter you general, we recommend Java Concurrency in Practice by Bri"
+ "an Goetz. His book will give We’ve reached an exciting point—in the next chapter;the counter is: 1 2222"
+ "sdsa ddasd asdsadas dsadasdas$__");
}
}).start();
}
}
根据$__数据被拆分成了三个包
netty 拆包和粘包 (三)的更多相关文章
- netty权威指南学习笔记三——TCP粘包/拆包之粘包现象
TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...
- Netty系列(四)TCP拆包和粘包
Netty系列(四)TCP拆包和粘包 一.拆包和粘包问题 (1) 一个小的Socket Buffer问题 在基于流的传输里比如 TCP/IP,接收到的数据会先被存储到一个 socket 接收缓冲里.不 ...
- Netty处理TCP拆包、粘包
Netty实践(二):TCP拆包.粘包问题-学海无涯 心境无限-51CTO博客 http://blog.51cto.com/zhangfengzhe/1890577 2017-01-09 21:56: ...
- Netty—TCP的粘包和拆包问题
一.前言 虽然TCP协议是可靠性传输协议,但是对于TCP长连接而言,对于消息发送仍然可能会发生粘贴的情形.主要是因为TCP是一种二进制流的传输协议,它会根据TCP缓冲对包进行划分.有可能将一个大数据包 ...
- 深入了解Netty【八】TCP拆包、粘包和解决方案
1.TCP协议传输过程 TCP协议是面向流的协议,是流式的,没有业务上的分段,只会根据当前套接字缓冲区的情况进行拆包或者粘包: 发送端的字节流都会先传入缓冲区,再通过网络传入到接收端的缓冲区中,最终由 ...
- 【Netty】TCP粘包和拆包
一.前言 前面已经基本上讲解完了Netty的主要内容,现在来学习Netty中的一些可能存在的问题,如TCP粘包和拆包. 二.粘包和拆包 对于TCP协议而言,当底层发送消息和接受消息时,都需要考虑TCP ...
- Netty 中的粘包和拆包
Netty 底层是基于 TCP 协议来处理网络数据传输.我们知道 TCP 协议是面向字节流的协议,数据像流水一样在网络中传输那何来 "包" 的概念呢? TCP是四层协议不负责数据逻 ...
- netty 解决TCP粘包与拆包问题(二)
TCP以流的方式进行数据传输,上层应用协议为了对消息的区分,采用了以下几种方法. 1.消息固定长度 2.第一篇讲的回车换行符形式 3.以特殊字符作为消息结束符的形式 4.通过消息头中定义长度字段来标识 ...
- Netty的TCP粘包/拆包(源码二)
假设客户端分别发送了两个数据包D1和D2给服务器,由于服务器端一次读取到的字节数是不确定的,所以可能发生四种情况: 1.服务端分两次读取到了两个独立的数据包,分别是D1和D2,没有粘包和拆包. 2.服 ...
随机推荐
- Android定时任务
前言 我们在平常的开发中可能会遇到一些需求,比如说,每日定时提醒,定时更新数据等等,反正就是周期性任务,碰到这类需求,我们就可以叫做定时任务.以前我们可以通过使用线程Handler来实现,现在既然是在 ...
- EJB是什么鸟东西?
到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:"把 ...
- PCB 3D PCB 后续改进与扩展功能一些想法
再次感受到WelGl实现3D效果的震撼, 一.目前功能: Gerber与钻孔 解析 并转为3D实景图,用户360度操控 二.后续改进扩展功能: 1.增加ODB++解析 2. 3D 尺寸标注(外形尺寸, ...
- Python27天 反射 ,isinstance与ssubclass 内置方法
所学内容 反射 1.hasattr ( 判断一个属性在对象里有没有 ) -------------------- [对象,字符串属性]本质是:# 判断 ' name ' in obj.__dict__ ...
- JavaScript中的+= 是什么?
+=表示相加并赋值,“i+=5”与“i=i+5”是等效的. 类似的运算符还有-=,*=,/=. i-=5等价于i=i-5; i*=5等价于i=i*5: i/=5等价于i=i/5.
- mybatis的二级缓存
在mybatis主配置文件里configuration标签里添加 <settings> <setting name="cacheEnabled" value=&q ...
- CAS配置(1)SSL证书配置
一.配置源码 源码配置稍后提供 二.系统环境安装 安装JDK配置,版本>=1.7 环境变量配置(参考): JAVA_HOME=C:\Program Files x86)\Java\jdk1.7. ...
- BZOJ 4800 折半暴搜
思路: 把它拆成两半 分别搜一发 两部分分别排好序 用two-pointers扫一遍 就可以了. (读入也要用long long) //By SiriusRen #include <cstdi ...
- Foeach 时修改集合的值报错
就是"集合已修改:可能无法执行枚举操作 foreach" 啥的, 不让我改 百度到Foreach是只读的,只供取值用,无法进行新增,修改,删除(仅引用,实际待验证) 解决办法:将F ...
- Java基础13一异常
1.异常概念 异常是程序在运行期发生的不正常的事件,它会打断指令的正常执行流程. 设计良好的程序应该在异常发生时提供处理这些不正常事件的方法,使程序不会因为异常的发生而阻断或产生不可预见的结果. Ja ...