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

DelimiterBasedFrameDecoder 自定義分隔符ide

給Server發送多條信息,可是server會講多條信息合併爲一條。這時候咱們須要對發生的消息指定分割,讓client和server都知道這些消息是一條一條的oop

//設置鏈接符/分隔符,換行顯示
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());

  

總體代碼:code

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 定長消息:及發送的消息須要必定的長度,當長度不夠時,剩下的消息將會被丟棄,只能經過補空格來防止被丟棄server

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

  

完整代碼blog

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();
		
	}
相關文章
相關標籤/搜索