回顧TCP粘包/拆包問題解決方案 上文詳細說了TCP粘包/拆包問題產生的原因及解決方式,並以LineBasedFrameDecoder為例演示了粘包/拆包問題的實際解決方案,本文再介紹兩種粘包/拆包問題的解決方案:分隔符和定長解碼器。在開始本文之前,先回顧一下解決粘包/拆包問題的幾個方式: 消息長度 ...
回顧TCP粘包/拆包問題解決方案
上文詳細說了TCP粘包/拆包問題產生的原因及解決方式,並以LineBasedFrameDecoder為例演示了粘包/拆包問題的實際解決方案,本文再介紹兩種粘包/拆包問題的解決方案:分隔符和定長解碼器。在開始本文之前,先回顧一下解決粘包/拆包問題的幾個方式:
- 消息長度固定,累計讀取到長度總和為定長LEN的報文後,就認為讀取到了一個完整的消息,將計數器重置,重新讀取下一個消息
- 將回車換行符作為消息結束符,例如FTP協議,這種方式在文本協議中應用比較廣泛
- 將特殊的分隔符作為消息的結束標誌,回車換行符就是一種特殊的結束分隔符
- 通過在消息頭中定義長度欄位來標誌消息的總長度
Netty對上面4種方式做了統一的抽象,提供了4種解碼器來解決對應的問題,使用起來非常方便,有了這些解碼器,用戶不需要自己對讀取的報文進行人工解碼,也不需要考慮TCP的粘包和拆包。
基於DelimiterBasedFrameDecoder的TCP粘包/拆包解決方案
使用DelimiterBasedFrameDecoder,我們可以自動完成以分隔符作為碼流結束標誌的消息的解碼,下麵通過例子來學習下DelimiterBasedFrameDecoder的使用。
首先寫一個EchoServer,和TimeServer差不多,主要是加上了DelimiterBasedFrameDecoder,分隔符定為"¥_":
1 public class EchoServer { 2 3 public void bind(int port) throws Exception { 4 // NIO線程組 5 EventLoopGroup bossGroup = new NioEventLoopGroup(); 6 EventLoopGroup workerGroup = new NioEventLoopGroup(); 7 8 try { 9 ServerBootstrap b = new ServerBootstrap(); 10 b.group(bossGroup, workerGroup) 11 .channel(NioServerSocketChannel.class) 12 .option(ChannelOption.SO_BACKLOG, 1024) 13 .childHandler(new ChildChannelHandler()); 14 15 // 綁定埠,同步等待成功 16 ChannelFuture f = b.bind(port).sync(); 17 // 等待服務端監聽埠關閉 18 f.channel().closeFuture().sync(); 19 } finally { 20 // 優雅退出,釋放線程池資源 21 bossGroup.shutdownGracefully(); 22 workerGroup.shutdownGracefully(); 23 } 24 } 25 26 private class ChildChannelHandler extends ChannelInitializer<SocketChannel> { 27 @Override 28 protected void initChannel(SocketChannel arg0) throws Exception { 29 ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); 30 31 arg0.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); 32 arg0.pipeline().addLast(new StringDecoder()); 33 arg0.pipeline().addLast(new EchoServerHandler()); 34 } 35 } 36 37 }
接著是EchoServerHandler,將接收到的消息列印出來,並記錄收到的消息次數:
1 public class EchoServerHandler extends ChannelHandlerAdapter { 2 3 private int counter = 0; 4 5 @Override 6 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 7 String body = (String)msg; 8 System.out.println("This is " + ++counter + " times receive client:[" + body + "]"); 9 10 body += "$_"; 11 ByteBuf echo = Unpooled.copiedBuffer(body.getBytes()); 12 ctx.writeAndFlush(echo); 13 } 14 15 @Override 16 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 17 cause.printStackTrace(); 18 ctx.close(); 19 } 20 21 }
收到消息之後,將消息加上"$_"回給客戶端。看下客戶端EchoClient的寫法,也是一樣,加上DelimiterBasedFrameDecoder:
1 public class EchoClient { 2 3 public void connect(int port, String host) throws Exception { 4 EventLoopGroup group = new NioEventLoopGroup(); 5 try { 6 Bootstrap b = new Bootstrap(); 7 8 b.group(group) 9 .channel(NioSocketChannel.class) 10 .option(ChannelOption.TCP_NODELAY, true) 11 .handler(new ChannelInitializer<SocketChannel>() { 12 protected void initChannel(SocketChannel ch) throws Exception { 13 ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); 14 15 ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); 16 ch.pipeline().addLast(new StringDecoder()); 17 ch.pipeline().addLast(new EchoClientHandler()); 18 }; 19 }); 20 21 // 發起非同步連接操作 22 ChannelFuture f = b.connect(host, port).sync(); 23 // 等待客戶端連接關閉 24 f.channel().closeFuture().sync(); 25 } finally { 26 // 優雅退出,釋放NIO線程組 27 group.shutdownGracefully(); 28 } 29 } 30 31 }
寫一個EchoClientHandler,發送10條消息到Server並記錄從Server回來的數據:
1 public class EchoClientHandler extends ChannelHandlerAdapter { 2 3 private int counter; 4 5 private static final String ECHO_REQ = "Hi, RickyXu, Welcome to Netty.$_"; 6 7 public EchoClientHandler() { 8 9 } 10 11 @Override 12 public void channelActive(ChannelHandlerContext ctx) throws Exception { 13 for (int i = 0; i < 10; i++) { 14 ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes())); 15 } 16 } 17 18 @Override 19 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 20 System.out.println("This is" + ++counter + " times receive server:[" + msg + "]"); 21 } 22 23 @Override 24 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { 25 ctx.flush(); 26 } 27 28 @Override 29 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 30 cause.printStackTrace(); 31 ctx.close(); 32 } 33 34 }
先運行服務端代碼再運行客戶端代碼,看下服務端收到的數據為:
This is 1 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 2 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 3 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 4 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 5 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 6 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 7 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 8 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 9 times receive client:[Hi, RickyXu, Welcome to Netty.] This is 10 times receive client:[Hi, RickyXu, Welcome to Netty.]
一模一樣收到10條且末尾的分隔符被忽略,客戶端收到的響應為:
This is 1 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 2 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 3 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 4 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 5 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 6 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 7 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 8 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 9 times receive server:[Hi, RickyXu, Welcome to Netty.] This is 10 times receive server:[Hi, RickyXu, Welcome to Netty.]
同樣收到了10條數據且忽略了末尾的換行符。
這樣我們就通過一個示例演示了使用DelimiterBasedFrameDecoder解決TCP粘包/拆包,下麵看一下使用FixedLengthFrameDecoder解決TCP粘包/拆包的示例。
基於FixedLengthFrameDecoder的TCP粘包/拆包解決方案
FixedLengthFrameDecoder是固定長度解碼器,它能夠按照指定的長度對消息進行自動解碼,開發者不需要考慮TCP的粘包/拆包問題,非常實用,
同樣看一下例子,先寫一個EchoServer,加入FixedLengthFrameDecoder:
public class EchoServer { public void bind(int port) throws Exception { // NIO線程組 EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new ChildChannelHandler()); // 綁定埠,同步等待成功 ChannelFuture f = b.bind(port).sync(); // 等待服務端監聽埠關閉 f.channel().closeFuture().sync(); } finally { // 優雅退出,釋放線程池資源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } private class ChildChannelHandler extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel arg0) throws Exception { arg0.pipeline().addLast(new FixedLengthFrameDecoder(20)); arg0.pipeline().addLast(new StringDecoder()); arg0.pipeline().addLast(new EchoServerHandler()); } } }
接著下一下EchoServerHandler,EchoServerHandler比較簡單:
public class EchoServerHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("Receive client:[" + msg + "]"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
這裡只列印接收到的數據,因為我們使用telnet來模擬發送請求而不是寫一個EchoClient。首先telnet一下localhost 8080:
接著使用"Ctrl+]":
輸入回車,這樣就變成了回顯模式,即輸入什麼看到什麼,簡單說一下telnet原理:
- 建立與伺服器的TCP連接
- 從鍵盤上接收輸入的字元
- 把輸入的字元變成標準格式併發送給伺服器
- 從伺服器接收輸出的信息
- 輸出的信息顯示在屏幕/控制臺上
註意一下第2點+第3點,這裡是鍵盤上輸入一個字元就會發送這個字元到服務端的,對這點有疑問的可以在FixedLengthFrameDecoder的decode方法上打斷點,就可以看到每輸入一個字元,斷點就會進入一次。
接著我們"0123456789"這樣一直迴圈輸入,FixedLengthFrameDecoder設置的Length是20,按照上面的解釋,這樣的話應該輸入兩輪"0123456789"即第二個9輸入之後,控制臺上就會列印了:
看一下控制台:
Receive client:[01234567890123456789]
沒毛病,證明瞭FixedLengthFrameDecoder確實是按照定長接收包的,如果收到的包是半包消息,FixedLengthFrameDecoder會緩存半包消息並等待下個包到達之後進行拼包,直到讀取到一個完整的包。