netty: 解決粘包拆包: 分隔符DelimiterBasedFrameDecoder,定長消息FixedLengthFrameDecoder


DelimiterBasedFrameDecoder 自定義分隔符

給Server發送多條信息,但是server會講多條信息合並為一條。這時候我們需要對發生的消息指定分割,讓client和server都知道這些消息是一條一條的

//設置連接符/分隔符,換行顯示
ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());
//DelimiterBasedFrameDecoder:自定義分隔符
sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf));
				
//設置為字符串形式的解碼:將傳遞的buf改為String
sc.pipeline().addLast(new  StringDecoder());
	
//處理消息			
sc.pipeline().addLast(new ClientHandler());

  

整體代碼:

public static void main(String[] args) throws InterruptedException {
		
		EventLoopGroup worker = new NioEventLoopGroup();
		Bootstrap b = new Bootstrap();
		b.group(worker)
		.channel(NioSocketChannel.class)
		.handler(new ChannelInitializer<SocketChannel>() {

			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				// TODO Auto-generated method stub
				//設置連接符/分隔符,換行顯示
				ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());
				//DelimiterBasedFrameDecoder:自定義分隔符
				sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf));
				
				//設置為字符串形式的解碼:將傳遞的buf改為String
				sc.pipeline().addLast(new  StringDecoder());
				
				sc.pipeline().addLast(new ClientHandler());
			}
		});
		//連接端口
		ChannelFuture cf = b.connect("127.0.0.1", 8765).sync();
		cf.channel().writeAndFlush(Unpooled.copiedBuffer("aaa$_".getBytes()));
		cf.channel().writeAndFlush(Unpooled.copiedBuffer("bbbbb$_".getBytes()));
		cf.channel().writeAndFlush(Unpooled.copiedBuffer("cccccccc$_".getBytes()));
		
		cf.channel().closeFuture().sync();		
		worker.shutdownGracefully();
		
	}

  

 

FixedLengthFrameDecoder 定長消息:及發送的消息需要一定的長度,當長度不夠時,剩下的消息將會被丟棄,只能通過補空格來防止被丟棄

//自定義長度,換行顯示
//設置每次發送長度為5個字符
sc.pipeline().addLast(new FixedLengthFrameDecoder(5));
				
//設置為字符串形式的解碼:將傳遞的buf改為String
sc.pipeline().addLast(new  StringDecoder());
	
//處理消息			
sc.pipeline().addLast(new ClientHandler());

  

完整代碼

public static void main(String[] args) throws InterruptedException {
		
		EventLoopGroup worker = new NioEventLoopGroup();
		Bootstrap b = new Bootstrap();
		b.group(worker)
		.channel(NioSocketChannel.class)
		.handler(new ChannelInitializer<SocketChannel>() {

			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				// TODO Auto-generated method stub
				//設置定長長度為5個字符,換行顯示		
                                //每次發送消息的長度需要是5的倍數	
				sc.pipeline().addLast(new FixedLengthFrameDecoder(5));
				
				//設置為字符串形式的解碼:將傳遞的buf改為String
				sc.pipeline().addLast(new  StringDecoder());
				
				sc.pipeline().addLast(new ClientHandler());
			}
		});
		//連接端口
		ChannelFuture cf = b.connect("127.0.0.1", 8765).sync();
		
//打印為:aaaaa
//打印為:bb被丟棄,未滿5個長度
cf.channel().writeAndFlush(Unpooled.copiedBuffer("aaaaabb".getBytes()));
		
//打印為:bbbbb
//打印完:ccccc
cf.channel().writeAndFlush(Unpooled.copiedBuffer("bbbbbccccc".getBytes()));

//打印為:ddddd
//打印完:dd空格空格空格
cf.channel().writeAndFlush(Unpooled.copiedBuffer("ddddddd 
  ".getBytes()));
		
		cf.channel().closeFuture().sync();		
		worker.shutdownGracefully();
		
	}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM