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.服 ...
随机推荐
- 将byte[]转为各种进制的字符串
/** * 将byte[]转为各种进制的字符串 * @param bytes byte[] * @param radix 基数可以转换进制的范围(2-36),从Chara ...
- CSS3 中弹性盒模型--容器的属性
1.display : flex | inline-flex注意,设为 Flex 布局以后,子元素的float.clear和vertical-align属性 将失效. 2.flex-direction ...
- golang struct里面的字段,或者slice排序
accounts := []users.Account{}Admin.DB.Preload("CurrencyObj").Where("member_id = ?&quo ...
- java编程基础篇---------> 编写一个程序,从键盘输入三个整数,求三个整数中的最小值。
编写一个程序,从键盘输入三个整数,求三个整数中的最小值. 关键:声明变量temp 与各数值比较. package Exam01; import java.util.Scanner; public ...
- WEB笔记-3、盒子模型+定位+显示
3.1 盒子模型 边距控制 margin/padding:上 右 下 左: padding:内容和边距之间的空间 margin:”盒子“外撑开的空间,两个相邻标签外边距会出现重叠和累加的现象, ...
- 蘑菇街TeamTalk应用安卓源码
该源码是蘑菇街TeamTalk应用源码,该产品目标用户为中小型企业用户,支持单聊和群聊,提供文字.表情和图片的富文本实时聊天功能 详细说明:http://android.662p.com/thread ...
- VHDL之User-defined data types
VHDL allows the user to define own data types. 1 user-defined integer types -- This is indeed the pr ...
- CXF-JAX-WS开发(一)入门案例
一.Web Service 1.定义 W3C定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作. 2.作用 多系统间数据通信 二.CXF是什么? CXF是目 ...
- Canopy聚类算法分析
原文链接:http://blog.csdn.net/yclzh0522/article/details/6839643 Canopy聚类算法是可以并行运行的算法,数据并行意味着可以多线程进 ...
- mysql手册操作
1.show table status 显示表状态 2.VERSION() 版本:CURRENT_DATE 当前日期: NOW() 当前时间:USER 当前用户 3.GRANT A ...