《Netty權威指南》之拆包粘包解決方案2-消息定長

客戶端代碼

package com.lyzx.netty.netty03;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
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.NioSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;

public class Client {

    public static void main(String[] args) 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{
                        ChannelHandler[] arr = {new FixedLengthFrameDecoder(64),new ClientHandler()};
                        ch.pipeline().addLast(arr);
                    }
                });

        ChannelFuture f = b.connect("127.0.0.1", 9988).sync();

        f.channel().closeFuture().sync();
        group.shutdownGracefully();
    }

}

客戶端handler

package com.lyzx.netty.netty03;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.Arrays;

public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client:channelActive____通道激活開始");
        for(int i=0;i<20;i++){
            char[] charArr = new char[64];
            Arrays.fill(charArr,' ');
            String req = "request0000000000_"+i;
            for(int j=0,count=req.length();j<count;j++){
                charArr[j] = req.charAt(j);
            }
            String reqInfo = new String(charArr);
            System.out.println("client:====================="+reqInfo+"    "+reqInfo.length());
            ctx.channel().writeAndFlush(Unpooled.copiedBuffer(reqInfo.getBytes()));
        }
        System.out.println("client:channelActive____通道激活結束");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception {
        System.out.println("client____:通道可讀開始");
        ByteBuf buff = (ByteBuf)msg;
        byte[] bytes = new byte[buff.readableBytes()];
        buff.readBytes(bytes);
        System.out.println("client____response time:"+new String(bytes).trim());
        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:發生異常");
    }
}

服務端代碼

package com.lyzx.netty.netty03;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
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.handler.codec.FixedLengthFrameDecoder;

/**
 * @author hero.li
 * 測試netty的拆包粘包問題
 * 拆包粘包的出現的底層原因是
 * TCP協議只是一個傳輸控制協議,TCP協議只需要把數據在客戶端和服務端之間傳輸
 * 而並不知道這些數據的具體含義,就像水流一樣,只要把數據傳輸成功就完成任務
 * 對於數據可能按照自己的一些規則組合更高效的傳輸,在組合的時候就無意中改變了
 * 上層業務數據的"規則" 這就是拆包粘包問題
 *
 * 解決方案有3 種
 * 1、分隔符
 *      只需要在客戶端和服務端的消息過濾器上加上DelimiterBasedFrameDecoder的實現即可
 *      注意:在發送消息時就需要加上分隔符(客戶端和服務端都要加)
 * 2、消息定長
 *      定義每一條信息都一樣長,比如64個字符,如果某一條消息的大小不足64字符就手動補齊到64字符
 *      注意是手動補齊,如果不補齊那麼久等到channel寫到64個字符時發送出去
 *      還有一點要注意下寫數據時注意用WriteAndFlush
 *
 * 3、自定義協議,報文頭和報文體,其中報文頭記錄報文體的長度
 */
public class Server {

    public static void main(String[] args) 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.TRUE)
                .childHandler(new ChannelInitializer<SocketChannel>(){
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelHandler[] arr = {new FixedLengthFrameDecoder(64),new ServerHandler()};
                        ch.pipeline().addLast(arr);
                    }
                });

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

服務端handler

package com.lyzx.netty.netty03;

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;
import java.util.Arrays;


/**
 * 對於網事件做讀寫,通常只要關注channelRead()和exceptionCaught()即可
 */
public class ServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("server:channelRead____通道可讀開始");
        //從msg對象中獲取消息並打印
        ByteBuf buff = (ByteBuf)msg;
        byte[] bytes = new byte[buff.readableBytes()];
        buff.readBytes(bytes);
        System.out.println("server:收到的消息____:"+new String(bytes).trim());
        String datetime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));

        char[] responseCharArrData = new char[64];
        Arrays.fill(responseCharArrData,' ');
        for(int i=0,count=datetime.length();i<count;i++){
            responseCharArrData[i] = datetime.charAt(i);
        }

        String responseData = new String(responseCharArrData);
        ctx.writeAndFlush(Unpooled.copiedBuffer(responseData.getBytes()));
        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();
    }

}

具體代碼位置   https://github.com/lyzxhero/Netty

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