雖然TCP協議是可靠性傳輸協議,可是對於TCP長鏈接而言,對於消息發送仍然可能會發生粘貼的情形。主要是由於TCP是一種二進制流的傳輸協議,它會根據TCP緩衝對包進行劃分。有可能將一個大數據包拆分紅多個小的數據包,也有可能將多個小的數據包合併成一個數據包。併發
本篇文章將對TCP粘包和拆包進行介紹:框架
假設Client端發送兩個數據包給Server端,以下圖:tcp
可是Server端實際接收到的數據包形式可能存在以上三種形式:ide
不管是以上哪一種狀況,從應用層的角度而言,Server端都將處理錯誤。首先以沒有考慮TCP拆包和粘包的場景爲例,分析下TCP拆包粘包將形成什麼樣的現象:oop
public static class EchoClientHandler extends ChannelHandlerAdapter { static final String ECHO_REQ = "Hi, huaijin.Welcome to Netty."; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // 發送100次消息至server端 for (int i = 0; i < 100; i++) { System.out.println("This is " + (i + 1) + " times send server: [" + ECHO_REQ + "]"); ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes())); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
這裏關於Client的啓動代碼省略,重點關注業務Handler。其中Echo總共發送了100次消息給Server,若是按照正確的情形,Server端應該接受到100次,而後分別進行處理。可是實際的情形並非這樣。大數據
public static class EchoServerHandler extends ChannelHandlerAdapter { /** * 原子計數器,統計接受到的次數 */ private AtomicInteger counter = new AtomicInteger(0); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 接受到消息打印 String body = (String) msg; System.out.println("This is " + counter.incrementAndGet() + " times receive client: [" + body + "]"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } }
服務端中使用原子計數器統計接收到的包的次數並打印接受到的消息。下面運行下實例,客戶端輸出以下:編碼
This is 1 times send server: [Hi, huaijin.Welcome to Netty.] This is 2 times send server: [Hi, huaijin.Welcome to Netty.] This is 3 times send server: [Hi, huaijin.Welcome to Netty.] This is 4 times send server: [Hi, huaijin.Welcome to Netty.] .... 中間部分省略 This is 97 times send server: [Hi, huaijin.Welcome to Netty.] This is 98 times send server: [Hi, huaijin.Welcome to Netty.] This is 99 times send server: [Hi, huaijin.Welcome to Netty.] This is 100 times send server: [Hi, huaijin.Welcome to Netty.]
從中能夠看出,Client端總共發送了100條消息至Server,可是Server端接收狀況以下:設計
This is 1 times receive client: [Hi, huaijin.Welcome to Netty.] This is 2 times receive client: [Hi, huaijin.Welcome to Netty.Hi, huaijin.Welcome to Netty.] This is 3 times receive client: [Hi, huaijin.Welcome to Netty.Hi, huaijin.Welcome to Netty.] This is 4 times receive client: [Hi, huaijin.Welcome to Netty.Hi, huaijin.Welcome to Netty.] This is 5 times receive client: [Hi, huaijin.Welcome to Netty.] ... 省略 This is 69 times receive client: [Hi, huaijin.Welcome to Netty.] This is 70 times receive client: [Hi, huaijin.Welcome to Netty.Hi, huaijin.Welcome to Netty.Hi, hu] This is 71 times receive client: [aijin.Welcome to Netty.Hi, huaijin.Welcome to Netty.] This is 72 times receive client: [Hi, huaijin.Welcome to Netty.] ... 省略 This is 84 times receive client: [Hi, huaijin.Welcome to Netty.Hi, huaijin.Welcome to Netty.] This is 93 times receive client: [Hi, huaijin.Welcome to Netty.] This is 94 times receive client: [Hi, huaijin.Welcome to Netty.]
因爲發生了粘包致使Server端只接收到94次,其中有兩條消息粘合在一塊兒。netty
有以上的情形能夠看出,當應用使用長鏈接併發發送請求時,會形成Server端接收到的請求數據發生混亂,從而處理錯誤。code
關於TCP拆包粘包的解決方式有不少,目前的主流解決方式有如下幾種:
固然netty做爲成熟框架,提供了多種方式解決TCP的拆包粘包問題,一般稱做爲半包解碼器。
netty中提供了基於分隔符實現的半包解碼器和定長的半包解碼器:
這裏仍然以上例爲主,使用DelimiterBasedFrameDecoder做爲半包解碼器。
public static class EchoClientHandler extends ChannelHandlerAdapter { /** * 消息使用"$_"分割 */ static final String ECHO_REQ = "Hi, huaijin.Welcome to Netty.$_"; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { for (int i = 0; i < 100; i++) { System.out.println("This is " + (i + 1) + " times send server: [" + ECHO_REQ + "]"); ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes())); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
客戶端代碼改動較小,只是每條消息後使用分割符"$_"分割,而後發送消息。
服務端須要使用分割符解碼器,利用其對粘包消息進行拆分:
/** * netty實現echo server * * @author huaijin */ public class EchoServer { public void bind(int port) throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // 使用分隔符"$_"的半包解碼器 ByteBuf byteBuf = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, byteBuf)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); } } public static void main(String[] args) throws InterruptedException { new EchoServer().bind(8080); } public static class EchoServerHandler extends ChannelHandlerAdapter { /** * 原子計數器,統計接受到的次數 */ private AtomicInteger counter = new AtomicInteger(0); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 接受到消息打印 String body = (String) msg; System.out.println("This is " + counter.incrementAndGet() + " times receive client: [" + body + "]"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } } }
當再次運行客戶端和服務端代碼時,服務端表現正常,接收到了100次:
This is 1 times receive client: [Hi, huaijin.Welcome to Netty.] This is 2 times receive client: [Hi, huaijin.Welcome to Netty.] This is 3 times receive client: [Hi, huaijin.Welcome to Netty.] ... 省略 This is 98 times receive client: [Hi, huaijin.Welcome to Netty.] This is 99 times receive client: [Hi, huaijin.Welcome to Netty.] This is 100 times receive client: [Hi, huaijin.Welcome to Netty.]
本篇文章主要介紹了什麼是TCP的拆包和粘包,並展現了拆包和粘包帶來的現象。並經過netty提供的方案,是如何解決TCP拆包和粘包問題。