1. 程式人生 > >五、Netty5解決TCP粘包問題

五、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的粘包和拆包。