[toc]
Netty中LineBasedFrameDecoder×××使用与分析:解决TCP粘包问题
上一篇文章《Netty中TCP粘包问题代码示例与分析》演示了使用了时间服务器的例子演示了TCP的粘包问题,这里使用LineBasedFrameDecoder
就是用来解决这个问题的。
不过需要注意的是,LineBasedFrameDecoder
见名知其义,可见其是与行相关的,而在前面演示TCP粘包问题时,作者是有意在发送的消息中都加入了换行符,目的也是为了在后面去讲解LineBasedFrameDecoder
的使用,当然也是给出一种简单解决TCP粘包问题的方法,或者是通过一个简单案例来让我们这些学习者有更直观的理解。
代码来自于《Netty权威指南》第4章,不过我依然是做了部分的修改。
服务端代码
TimeServer.java
package cn.xpleaf.netty02;
import io.netty.bootstrap.ServerBootstrap;
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.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
public class TimeServer {
public void bind(int port) throws Exception {
// 配置服务端的NIO线程组
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChildChannelHandler());
// 绑定端口,同步等待成功
ChannelFuture f = b.bind(port).sync();
// 等待服务端监听端口关闭
f.channel().closeFuture().sync();
} finally {
// 优雅退出,释放线程池资源
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 核心在下面两行,加入了LineBasedFrameDecoder和StringDecoder两个×××
// 所以当消息到达我们的业务处理handler即TimerServerHandler,所看到的消息
// 都是前面两个×××经过处理之后的结果
ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new TimeServerHandler());
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
if(args != null && args.length > 0) {
try {
port = Integer.valueOf(port);
} catch (NumberFormatException e) {
// TODO: handle exception
}
}
new TimeServer().bind(port);
}
}
TimeServerHandler.java
package cn.xpleaf.netty02;
import java.sql.Date;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class TimeServerHandler extends ChannelInboundHandlerAdapter {
private int counter = 0;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String) msg;
// counter的作用是标记这是第几次收到客户端的请求
System.out.println("The time server receive order : " + body + " ; the counter is : " + ++counter);
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());
ctx.write(resp);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ctx.close();
}
}
客户端代码
TimeClient.java
package cn.xpleaf.netty02;
import io.netty.bootstrap.Bootstrap;
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.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
public class TimeClient {
public void connect(int port, String host) throws Exception {
// 配置客户端NIO线程组
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 核心在下面两行,加入了LineBasedFrameDecoder和StringDecoder两个×××
// 所以当消息到达我们的业务处理handler即TimerServerHandler,所看到的消息
// 都是前面两个×××经过处理之后的结果
ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new TimeClientHandler());
}
});
// 发起异步连接操作
ChannelFuture f = b.connect(host, port).sync();
// 等待客户端链路关闭
f.channel().closeFuture().sync();
} finally {
// 优雅退出,释放NIO线程组
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
if(args != null && args.length > 0) {
try {
port = Integer.valueOf(port);
} catch (NumberFormatException e) {
// 采用默认值
}
}
new TimeClient().connect(port, "localhost");
}
}
TimeClientHandler.java
package cn.xpleaf.netty02;
import java.util.logging.Logger;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class TimeClientHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = Logger.getLogger(TimeServerHandler.class.getName());
private int counter;
private byte[] req;
public TimeClientHandler() {
req = ("QUERY TIME ORDER" + System.getProperty("line.separator")).getBytes();
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ByteBuf message = null;
for(int i = 0; i < 100; i++) {
message = Unpooled.buffer(req.length);
message.writeBytes(req);
ctx.writeAndFlush(message);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String) msg;
// counter的作用是标记这是第几次收到客户端的请求
System.out.println("Now is : " + body + " ; the counter is : " + ++counter);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.warning("Unexpected exception from downstream : ");
ctx.close();
}
}
测试
服务端运行结果:
The time server receive order : QUERY TIME ORDER ; the counter is : 1
The time server receive order : QUERY TIME ORDER ; the counter is : 2
The time server receive order : QUERY TIME ORDER ; the counter is : 3
...省略多行...
The time server receive order : QUERY TIME ORDER ; the counter is : 98
The time server receive order : QUERY TIME ORDER ; the counter is : 99
The time server receive order : QUERY TIME ORDER ; the counter is : 100
客户端运行结果:
Now is : 2018-02-11 ; the counter is : 1
Now is : 2018-02-11 ; the counter is : 2
Now is : 2018-02-11 ; the counter is : 3
...省略多行...
Now is : 2018-02-11 ; the counter is : 98
Now is : 2018-02-11 ; the counter is : 99
Now is : 2018-02-11 ; the counter is : 100
从输出结果可以看出,这确实解决了TCP的粘包问题。
原理分析
下面的分析来自于书本上,这里也贴出来:
当然,对应到上面这个例子,具体是如何解决TCP粘包问题的呢,下面也来简单分析一下。
首先需要明确的是,TCP依然会有粘包的情况,即TCP的机制依然没有变,而且也不可能变,因为这是TCP/IP协议栈的规定,并且由操作系统实现。
所以不管应用层,我们的程序代码如何改变,本质上,TCP还是会按照上一篇文章中所说的方式去进行包的发送,即客户端中,TCP依然会把netty的多个请求发送到服务端。
只是这时不同的是,发送的消息不是直接先到达我们写的TimeServerHandler处理器,而是先达到LineBasedFrameDecoder
×××,因为该×××就是以行作为分隔符来解析客户端的请求的,所以当它分析客户端的请求时,一旦发现有换行符,它就认为当初Netty的客户端在进行消息的发送时,是以换行符为分隔符的去发送请求的,只是TCP把它的多个请求合并发送了而已,因此它就把每一行解析为客户端的一个请求。那么这时,解析出每行是一个请求后,它就会以一个请求一次的方式去调用下面的Handler,所以就会多次调用我们的业务处理handler,在TimeServerHandler看起来这就相当于是多个请求,而当对数据进行回写的时候,原理也是一样的。
所以可以知道,TCP粘包问题的解决是在应用层的解决,需要服务端和客户端事先约定一种规范,比如这里以换行符作为每个请求的分隔符。
转载于:https://blog.51cto.com/xpleaf/2071083