《Netty權威指南》之拆包粘包問題及解決方案1

客戶端和服務端代碼

package com.lyzx.netty.netty02;

import io.netty.bootstrap.Bootstrap;
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.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.junit.Test;

/**
 * @author hero.li
 * 測試netty的拆包粘包問題
 * 拆包粘包的出現的底層原因是
 * TCP協議只是一個傳輸控制協議,TCP協議只需要把數據在客戶端和服務端之間傳輸
 * 而並不知道這些數據的具體含義,就像水流一樣,只要把數據傳輸成功就完成任務
 * 對於數據可能按照自己的一些規則組合更高效的傳輸,在組合的時候就無意中改變了
 * 上層業務數據的"規則" 這就是拆包粘包問題
 *
 * 解決方案有3 種
 * 1、分隔符
 *      只需要在客戶端和服務端的消息過濾器上加上
        DelimiterBasedFrameDecoder的實現即可
 *      注意:在發送消息時就需要加上分隔符(客戶端和服務端都要加)
 * 2、消息定長
 * 3、自定義協議,報文頭和報文體,其中報文頭記錄報文體的長度
 */
public class Netty02 {
    @Test
    public void nettyServer() throws InterruptedException{
        //開啓兩個線程組,一個用於接受客戶端的請求   另一個用於異步的網絡IO的讀寫
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        //Netty啓動的輔助類 裏面封裝了客戶端和服務端的鏈接以及如何處理選擇器 selector等邏輯
        ServerBootstrap b = new ServerBootstrap();

        //傳入兩個線程組,設置傳輸塊大小爲1k,
  //添加ServerHandler類型的過濾器(表示如何處理這些消息,過濾器當然要集成netty的一個接口)
        b.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,1024)
                .childOption(ChannelOption.SO_KEEPALIVE,Boolean.FALSE)
                .childHandler(new ChannelInitializer<SocketChannel>(){
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        //以$$_$$爲分隔符,
                        ByteBuf delimiter = Unpooled.copiedBuffer("$$_$$".getBytes("UTF-8"));
                        socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
                        socketChannel.pipeline().addLast(new StringEncoder());
                        socketChannel.pipeline().addLast(new ServerHandler());
                    }
                });

        //同步等待綁定端口結束
        ChannelFuture f = b.bind(9988).sync();
        //等待服務端監聽端口關閉
        f.channel().closeFuture().sync();
        //優雅的關閉線程組
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

    @Test
    public void nettyClient() throws InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup();
        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{

                        ByteBuf delimiter = Unpooled.copiedBuffer("$$_$$".getBytes("UTF-8"));
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
                        ch.pipeline().addLast(new StringEncoder());
                        ch.pipeline().addLast(new ClientHandler());
                    }
                });

        ChannelFuture f = b.connect("127.0.0.1", 9988).sync();
        f.channel().closeFuture().sync();
        group.shutdownGracefully();
    }
}

handler代碼如下

package com.lyzx.netty.netty02;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * 對於網事件做讀寫,通常只要關注channelRead()和exceptionCaught()即可
 */
public class ServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("server:channelRead 通道可讀開始");
        //從msg對象中獲取消息並打印
        ByteBuf bytebuf = (ByteBuf)msg;
        byte[] bytes = new byte[bytebuf.readableBytes()];
        bytebuf.readBytes(bytes);
        System.out.println("收到的消息:"+new String(bytes,"UTF-8"));

        //獲取當前的時間並通過ctx的write方法異步寫數據到SocketChannel
        //ctx.write並不會直接把數據寫入到緩衝區等到調用channelReadComplete裏面的ctx.flush()
        //的時候再把數寫入到SocketChannel
        String datetime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
        ByteBuf byteBuf = Unpooled.copiedBuffer((datetime+"$$_$$").getBytes("UTF-8"));
        ctx.writeAndFlush(byteBuf);
        System.out.println("server:channelRead 通道可讀結束");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("server:channelReadComplete 通道可讀完成 ");
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("server:exceptionCaught 發生異常");
        ctx.close();
    }
}

客戶端Handler

package com.lyzx.netty.netty02;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client:channelActive  通道激活開始");
        for(int i=0;i<10;i++){
            //以$$_$$爲分隔符,所以在每次發送的時候都需要以消息末尾加上$$_$$
            ByteBuf buff  = Unpooled.copiedBuffer(("req_"+i+"$$_$$").getBytes("UTF-8"));
            ctx.writeAndFlush(buff);
        }
        System.out.println("client:channelActive  通道激活結束");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception {
        System.out.println("client:通道可讀開始");
        ByteBuf buf = (ByteBuf)msg;
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        System.out.println("response time :"+new String(bytes));
        System.out.println("client:通道可讀結束");
    }

    /**
     * 類似於AOP的後置通知 在這裏當通道讀取完畢後關閉通道
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client:通道可讀完成");
//        ctx.channel().close();
//        ctx.close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("client:發生異常");
        super.exceptionCaught(ctx, cause);
    }
}

完整代碼歡迎訪問我的github  https://github.com/lyzxhero/Netty

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章