Netty拆包粘包
在基于流的传输里比如TCP/IP,接收到的数据会先被存储到一个socket接收缓冲里。不幸的是,基于流的传输并不是一个数据包队列,而是一个字节队列。即使你发送了2个独立的数据包,操作系统也不会作为2个消息处理而仅仅是作为一连串的字节而言。因此这是不能保证你远程写入的数据就会准确地读取。
参考资料:http://ifeve.com/netty5-user-guide
常用的拆包粘包主要有3种方式:
- 1、消息定长,例如每个报文的大小固定为200个字节,如果不够,空位补空格。
- 2、在包尾部增加特殊字符串进行分割,例如加回车等
- 3、 将消息分为消息头和消息体,在消息头中包含表示消息总长度的字段,然后进行业务逻辑的处理
(1) 在包尾部增加特殊字符串进行分割在上一年博客中同string类型传输一起做了代码演示
https://blog.youkuaiyun.com/qq_28240551/article/details/82393565
关键点为:
bootstrap.group(boss, worker)
//指定channel类型
.channel(NioServerSocketChannel.class)
//handler会在初始化时就执行,而childHandler会在客户端成功connect后才执行
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ByteBuf byteBuf = Unpooled.copiedBuffer("$_".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new StringEncoder());
socketChannel.pipeline().addLast(new ServerHandler());
}
})
//设置tcp缓冲区大小
.option(ChannelOption.SO_BACKLOG, 128)
//设置发送缓冲区大小
.option(ChannelOption.SO_SNDBUF, 1024 * 32)
//设置接收缓冲区大小
.option(ChannelOption.SO_RCVBUF, 1024 * 32)
//设置是否保存长连接
.childOption(ChannelOption.SO_KEEPALIVE, true);
(2)关于消息头和消息体的方法,这里不做演示了,工作中用到可以搜一下使用例子
(3)这里再演示一下报文定长的方式进行数据传输,(总体看使用频率,分割>定长>报文)
关键点为
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(worker)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(5));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new StringEncoder());
socketChannel.pipeline().addLast(new ClientHandler());
}
})
.option(ChannelOption.SO_KEEPALIVE,true);
server
package com.nettyFixed;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/**
* Created by BaiTianShi on 2018/9/5.
*/
public class Server {
private int port;
public Server(int port) {
this.port = port;
}
public void run() {
//用来接收连接事件组
EventLoopGroup boss = new NioEventLoopGroup();
//用来处理接收到的连接事件处理组
EventLoopGroup worker = new NioEventLoopGroup();
//server配置辅助类
ServerBootstrap bootstrap = new ServerBootstrap();
try {
//将连接接收组与事件处理组连接,当server的boss接收到连接收就会交给worker处理
bootstrap.group(boss, worker)
//指定channel类型
.channel(NioServerSocketChannel.class)
//handler会在初始化时就执行,而childHandler会在客户端成功connect后才执行
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(5));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new StringEncoder());
socketChannel.pipeline().addLast(new ServerHandler());
}
})
//设置tcp缓冲区大小
.option(ChannelOption.SO_BACKLOG, 128)
//设置发送缓冲区大小
.option(ChannelOption.SO_SNDBUF, 1024 * 32)
//设置接收缓冲区大小
.option(ChannelOption.SO_RCVBUF, 1024 * 32)
//设置是否保存长连接
.childOption(ChannelOption.SO_KEEPALIVE, true);
//注意。此处option()是提供给NioServerSocketChannel用来接收进来的连接,也就是boss线程
//childOption是提供给有福管道serverChannel接收到的连接,也就是worker线程,在这个例子中也就是NioServerSocketChannel
//异步绑定端口,可以绑定多个端口
ChannelFuture fu1 = bootstrap.bind(port).sync();
ChannelFuture fu2 = bootstrap.bind(8766).sync();
//异步检查是否关闭
fu1.channel().closeFuture().sync();
fu2.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
worker.shutdownGracefully();
boss.shutdownGracefully();
}
}
public static void main(String[] args) {
Server server = new Server(8765);
server.run();
}
}
client
package com.nettyFixed;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/**
* Created by BaiTianShi on 2018/9/5.
*/
public class Client {
private String ip;
private int port;
public Client(String ip, int port) {
this.ip = ip;
this.port = port;
}
public void run(){
//客户端用来连接服务端的连接组
EventLoopGroup worker= new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(worker)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(5));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new StringEncoder());
socketChannel.pipeline().addLast(new ClientHandler());
}
})
.option(ChannelOption.SO_KEEPALIVE,true);
try {
//可以进多个端口同时连接
ChannelFuture fu1 = bootstrap.connect(ip,port).sync();
ChannelFuture fu2 = bootstrap.connect(ip,8766).sync();
fu1.channel().writeAndFlush("aaaaaaa");
fu2.channel().writeAndFlush("1111222");
fu1.channel().closeFuture().sync();
fu2.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
worker.shutdownGracefully();
}
}
public static void main(String[] args) {
Client cl = new Client("127.0.0.1",8765);
cl.run();
}
}
server端控制台
client控制台
‘’