五、Netty5解决TCP粘包问题
来源:互联网 发布:eddie griffin 知乎 编辑:程序博客网 时间:2024/06/16 03:00
我们在前面的Demo中并没有考虑到读半包问题,这在功能测试中往往没有问题,但是一旦压力上来,或者发送大报文之后,就会存在粘包和拆包问题,如果代码没有考虑,往往就会出现解码错位或者错误,导致程序不能正常运行。下面使用netty的半包解码器来解决TCP粘包和拆包问题。
工具:IntelliJ IDEA 2016.2.2(64)
netty版本:netty-all-5.0.0.Alpha1
服务端程序:
package TCPTimeServer;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;/** * Created by L_kanglin on 2017/6/4. */public class TCPTimeServer { public void bind(int port){ // 配置服务端的NIO线程组 EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); // 设置线程组及Socket参数 b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new ChildChannelHandler()); // 绑定端口,同步等待成功 ChannelFuture f = b.bind(port).sync(); System.out.println("服务已经启动,端口:" + port); f.channel().closeFuture().sync(); } catch (Exception e) { } finally { // 退出释放线程池资源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); System.out.println("服务销毁!"); } } public class ChildChannelHandler extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { // 以下两行代码为了解决半包读问题 ch.pipeline().addLast(new LineBasedFrameDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new TimerServerHandler()); } } public static void main(String[] args) { int port = 8080; if (null != args && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (Exception e) { // 采用默认值 } } new TCPTimeServer().bind(port); }}
package TCPTimeServer;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import java.net.InetAddress;import java.util.Date;/** * Created by L_kanglin on 2017/6/4. */public class TimerServerHandler extends ChannelHandlerAdapter { private int counter; public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String body = (String) msg; 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); } public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { ctx.close(); }}
客户端程序:
package TCPTimeClient;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;/** * Created by L_kanglin on 2017/6/4. */public class TCPTimeClient { public void connect(int port, String host) { // 配置客户端的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 public void initChannel(SocketChannel ch) throws Exception { // 以下两行代码为了解决半包读问题 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(); } catch (Exception e) { } finally { // 退出,释放NIO线程组 group.shutdownGracefully(); } } public static void main(String[] args) { int port = 8080; if (null != args && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (Exception e) { } } new TCPTimeClient().connect(port, "127.0.0.1"); }}
package TCPTimeClient;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import java.util.logging.Logger;/** * Created by L_kanglin on 2017/6/5. */public class TimeClientHandler extends ChannelHandlerAdapter { private static final Logger logger = Logger.getLogger(TimeClientHandler.class.getName()); private int counter; private byte[] req; public TimeClientHandler() { req = ("QUERY TIME ORDER" + System.getProperty("line.separator")).getBytes(); } public void channelActive(ChannelHandlerContext ctx) { ByteBuf message = null; for (int i = 0; i < 50; i++) { message = Unpooled.buffer(req.length); message.writeBytes(req); ctx.writeAndFlush(message); } } public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String body = (String) msg; System.out.println("Now is:" + body + "; the counter is:" + (++counter)); } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { logger.warning("Unexcepted exception from downstream:" + cause.getMessage()); ctx.close(); }}
服务端运行程序如下:
服务已经启动,端口:8080The time server receive order:QUERY TIME ORDER;the counter is:1The time server receive order:QUERY TIME ORDER;the counter is:2The time server receive order:QUERY TIME ORDER;the counter is:3The time server receive order:QUERY TIME ORDER;the counter is:4The time server receive order:QUERY TIME ORDER;the counter is:5The time server receive order:QUERY TIME ORDER;the counter is:6The time server receive order:QUERY TIME ORDER;the counter is:7The time server receive order:QUERY TIME ORDER;the counter is:8The time server receive order:QUERY TIME ORDER;the counter is:9The time server receive order:QUERY TIME ORDER;the counter is:10The time server receive order:QUERY TIME ORDER;the counter is:11The time server receive order:QUERY TIME ORDER;the counter is:12The time server receive order:QUERY TIME ORDER;the counter is:13The time server receive order:QUERY TIME ORDER;the counter is:14The time server receive order:QUERY TIME ORDER;the counter is:15The time server receive order:QUERY TIME ORDER;the counter is:16The time server receive order:QUERY TIME ORDER;the counter is:17The time server receive order:QUERY TIME ORDER;the counter is:18The time server receive order:QUERY TIME ORDER;the counter is:19The time server receive order:QUERY TIME ORDER;the counter is:20The time server receive order:QUERY TIME ORDER;the counter is:21The time server receive order:QUERY TIME ORDER;the counter is:22The time server receive order:QUERY TIME ORDER;the counter is:23The time server receive order:QUERY TIME ORDER;the counter is:24The time server receive order:QUERY TIME ORDER;the counter is:25The time server receive order:QUERY TIME ORDER;the counter is:26The time server receive order:QUERY TIME ORDER;the counter is:27The time server receive order:QUERY TIME ORDER;the counter is:28The time server receive order:QUERY TIME ORDER;the counter is:29The time server receive order:QUERY TIME ORDER;the counter is:30The time server receive order:QUERY TIME ORDER;the counter is:31The time server receive order:QUERY TIME ORDER;the counter is:32The time server receive order:QUERY TIME ORDER;the counter is:33The time server receive order:QUERY TIME ORDER;the counter is:34The time server receive order:QUERY TIME ORDER;the counter is:35The time server receive order:QUERY TIME ORDER;the counter is:36The time server receive order:QUERY TIME ORDER;the counter is:37The time server receive order:QUERY TIME ORDER;the counter is:38The time server receive order:QUERY TIME ORDER;the counter is:39The time server receive order:QUERY TIME ORDER;the counter is:40The time server receive order:QUERY TIME ORDER;the counter is:41The time server receive order:QUERY TIME ORDER;the counter is:42The time server receive order:QUERY TIME ORDER;the counter is:43The time server receive order:QUERY TIME ORDER;the counter is:44The time server receive order:QUERY TIME ORDER;the counter is:45The time server receive order:QUERY TIME ORDER;the counter is:46The time server receive order:QUERY TIME ORDER;the counter is:47The time server receive order:QUERY TIME ORDER;the counter is:48The time server receive order:QUERY TIME ORDER;the counter is:49The time server receive order:QUERY TIME ORDER;the counter is:50
客户端运行程序如下:
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:1Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:2Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:3Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:4Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:5Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:6Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:7Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:8Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:9Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:10Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:11Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:12Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:13Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:14Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:15Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:16Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:17Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:18Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:19Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:20Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:21Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:22Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:23Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:24Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:25Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:26Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:27Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:28Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:29Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:30Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:31Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:32Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:33Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:34Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:35Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:36Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:37Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:38Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:39Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:40Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:41Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:42Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:43Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:44Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:45Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:46Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:47Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:48Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:49Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:50
模拟TCP粘包和半包场景,采用简单的压力测试,通信链路建立成功之后,客户端连续发送100条消息给服务端,然后查看服务端和客户端的运行结果。
LineBasedFrameDecoder和StringDecoder的原理分析:
LineBasedFrameDecoder的工作原理是它依次遍历ByteBuf中的可读字节,判断看是否有“\n”或者“\r\n”,如果有,就以此位置为结束位置,从可读索引到结束位置区间的字节就组成了一行。它是以换行符为结束标志的解码器,支持携带结束符或者不携带结束符两种解码方式,同时支持配置单行的最大长度。如果连续读取到最大长度后仍然没有发现换行符,就会抛出异常,同时忽略掉之前读到的异常码流。
StringDecoder就是将接收到的对象转换成字符串,然后继续调用后面的handler。LineBasedFrameDecoder+StringDecoder组合就是按行切换的文本解码器,它被设计用来支持TCP的粘包和拆包。
阅读全文
0 0
- 五、Netty5解决TCP粘包问题
- Netty5中使用LineBasedFrameDecoder解决TCP粘包问题
- Netty5入门学习笔记002-TCP粘包/拆包问题的解决之道(上)
- Netty5入门学习笔记003-TCP粘包/拆包问题的解决之道(下)
- Netty5入门学习笔记002-TCP粘包/拆包问题的解决之道(上)
- Netty5入门学习笔记003-TCP粘包/拆包问题的解决之道(下)
- Netty5入门学习笔记002-TCP粘包/拆包问题的解决之道(上)
- Netty5入门学习笔记003-TCP粘包/拆包问题的解决之道(下)
- 解决TCP网络传输“粘包”问题
- 解决TCP网络传输“粘包”问题
- [转]TCP粘包问题的解决
- 解决TCP网络传输“粘包”问题
- 解决TCP网络传输“粘包”问题
- 解决TCP网络传输“粘包”问题
- 解决TCP网络传输“粘包”问题
- 解决TCP网络传输“粘包”问题
- 解决TCP网络传输“粘包”问题
- 解决TCP网络传输“粘包”问题
- pic操作lcd 模拟spi 9非标准spi 9位数据位
- Leetcode 48 Rotated Image
- SDUT-3930(线段树+状压)
- java开发环境搭建
- LeetCode@Array_169_Majority_Element
- 五、Netty5解决TCP粘包问题
- 展讯平台判断otg状态
- 傅盛认知三部曲之三:战略就是格局+破局
- 深入理解AJAX系列第三篇--async属性值之同步和异步及同步和异步区别
- 一个想法照进现实-《IT连》创业项目:直觉型面试招聘的Bug
- css精灵的使用
- 16.销毁一个活动
- iOS完整学习路线图
- 自学Java之Java类库(开发Swing应用程序)(014day)