五、Netty5解决TCP粘包问题

我们在前面的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();
    }
}

服务端运行程序如下:

服务已经启动,端口:8080
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:4
The time server receive order:QUERY TIME ORDER;the counter is:5
The time server receive order:QUERY TIME ORDER;the counter is:6
The time server receive order:QUERY TIME ORDER;the counter is:7
The time server receive order:QUERY TIME ORDER;the counter is:8
The time server receive order:QUERY TIME ORDER;the counter is:9
The time server receive order:QUERY TIME ORDER;the counter is:10
The time server receive order:QUERY TIME ORDER;the counter is:11
The time server receive order:QUERY TIME ORDER;the counter is:12
The time server receive order:QUERY TIME ORDER;the counter is:13
The time server receive order:QUERY TIME ORDER;the counter is:14
The time server receive order:QUERY TIME ORDER;the counter is:15
The time server receive order:QUERY TIME ORDER;the counter is:16
The time server receive order:QUERY TIME ORDER;the counter is:17
The time server receive order:QUERY TIME ORDER;the counter is:18
The time server receive order:QUERY TIME ORDER;the counter is:19
The time server receive order:QUERY TIME ORDER;the counter is:20
The time server receive order:QUERY TIME ORDER;the counter is:21
The time server receive order:QUERY TIME ORDER;the counter is:22
The time server receive order:QUERY TIME ORDER;the counter is:23
The time server receive order:QUERY TIME ORDER;the counter is:24
The time server receive order:QUERY TIME ORDER;the counter is:25
The time server receive order:QUERY TIME ORDER;the counter is:26
The time server receive order:QUERY TIME ORDER;the counter is:27
The time server receive order:QUERY TIME ORDER;the counter is:28
The time server receive order:QUERY TIME ORDER;the counter is:29
The time server receive order:QUERY TIME ORDER;the counter is:30
The time server receive order:QUERY TIME ORDER;the counter is:31
The time server receive order:QUERY TIME ORDER;the counter is:32
The time server receive order:QUERY TIME ORDER;the counter is:33
The time server receive order:QUERY TIME ORDER;the counter is:34
The time server receive order:QUERY TIME ORDER;the counter is:35
The time server receive order:QUERY TIME ORDER;the counter is:36
The time server receive order:QUERY TIME ORDER;the counter is:37
The time server receive order:QUERY TIME ORDER;the counter is:38
The time server receive order:QUERY TIME ORDER;the counter is:39
The time server receive order:QUERY TIME ORDER;the counter is:40
The time server receive order:QUERY TIME ORDER;the counter is:41
The time server receive order:QUERY TIME ORDER;the counter is:42
The time server receive order:QUERY TIME ORDER;the counter is:43
The time server receive order:QUERY TIME ORDER;the counter is:44
The time server receive order:QUERY TIME ORDER;the counter is:45
The time server receive order:QUERY TIME ORDER;the counter is:46
The time server receive order:QUERY TIME ORDER;the counter is:47
The time server receive order:QUERY TIME ORDER;the counter is:48
The time server receive order:QUERY TIME ORDER;the counter is:49
The time server receive order:QUERY TIME ORDER;the counter is:50

客户端运行程序如下:

Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:1
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:2
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:3
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:4
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:5
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:6
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:7
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:8
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:9
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:10
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:11
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:12
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:13
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:14
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:15
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:16
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:17
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:18
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:19
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:20
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:21
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:22
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:23
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:24
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:25
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:26
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:27
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:28
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:29
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:30
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:31
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:32
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:33
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:34
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:35
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:36
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:37
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:38
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:39
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:40
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:41
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:42
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:43
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:44
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:45
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:46
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:47
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:48
Now is:Mon Jun 05 10:29:49 CST 2017; the counter is:49
Now 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的粘包和拆包。

你可能感兴趣的:(idea,intellij,netty5,TCP粘包和拆包)