1. 程式人生 > >netty權威指南學習筆記四——TCP粘包/拆包之粘包問題解決

netty權威指南學習筆記四——TCP粘包/拆包之粘包問題解決

方法 pan 對象 protect row 學習 ddl .get font

  發生了粘包,我們需要將其清晰的進行拆包處理,這裏采用LineBasedFrameDecoder來解決

LineBasedFrameDecoder的工作原理是它依次遍歷ByteBuf中的可讀字節,判斷看是否有“\n”或“\r\n”,如果有,就以此為結束位置,從可讀索引到結束位置區間的字節就組成一行,它是以換行為結束標誌的編碼器,支持攜帶結束符或者不攜帶結束符兩種方式,同時支持配置單行最大長度,如果連續讀取到的最大長度後仍沒有發現換行符,就會拋出異常,同時忽略掉之前讀到的異常碼流。

StringDecoder的功能非常簡單,就是將接收到的對象轉換為字符串,然後繼續調用後面的Handler.

LineBasedFrameDecoder+StringDecoder組合就是按行切換的文本解碼器。

主要思路是改造客戶端和服務端的處理IO的類之前添加相應解碼器,解碼器之後才去調用IO處理類的Handler。

客戶端改造的代碼部分

TheClientServer

在處理IO的類initChannel()方法中調用處理IO類前添加相關解碼的方法

1 public class ClientChildHandler extends ChannelInitializer<SocketChannel>{
2         @Override
3         protected void initChannel(SocketChannel socketChannel) throws
Exception { 4 socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024))//添加解碼器,遍歷ByteBuf並組行 5 .addLast(new StringDecoder())//添加解碼器,將接收到的對象轉換為字符串 6 .addLast(new TimeClientHandler()); 7 } 8 }

同時在接收請求或相應的channelRead(ChannelHandlerContext ctx, Object msg)方法中進行相關改造,直接用String接收即可,這是因為,在這之前的解碼器已經將發送過來的ByteBuf類型進行讀取後並轉換為String格式了

TimeClientHandler

1     @Override
2     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
3        /* ByteBuf byteBuf = (ByteBuf) msg;
4         byte[] req = new byte[byteBuf.readableBytes()];
5         byteBuf.readBytes(req);
6         String body = new String(req,"utf-8");*/
7        String body = (String) msg;
8         System.out.println("Now is:"+body+";The client count is:"+ ++count);
9     }

服務端改造的代碼部分

TimeServer

 1 //    IO處理類的初始化
 2     private class ChildHandler extends ChannelInitializer {
 3         @Override
 4         protected void initChannel(Channel channel) throws Exception {
 5             channel.pipeline()
 6                     .addLast(new LineBasedFrameDecoder(1024))
 7                     .addLast(new StringDecoder())
 8                     .addLast(new TimeServerHandler());
 9         }
10     }

TimeServerHandler

 1    @Override
 2     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
 3        /* ByteBuf byteBuf = (ByteBuf) msg;//將請求轉為ByteBuf緩沖區
 4         byte[] req = new byte[byteBuf.readableBytes()];//獲取byteBuf的可讀字節數
 5         byteBuf.readBytes(req);//將緩沖區字節數組復制到req數組中
 6         String body = new String(req,"utf-8")//轉換為字符串
 7                 //改造去掉客戶端傳遞過來的換行符號,模擬故障造成粘包問題
 8              .substring(0,req.length-System.lineSeparator().length());*/
 9        String body = (String) msg;
10         System.out.println("the time server receive order:"+body+"the count is:"+ ++count);
11 //      處理IO內容
12         String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
13                 ?new Date(System.currentTimeMillis()).toString():"BAD ORDER";
14         currentTime = currentTime+System.getProperty("line.separator");
15         ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());//返回客戶端的消息轉化為ByteBuf對象
16         ctx.write(resp);//將待應答消息放入緩沖數組中
17     }

其余代碼與上一小節完全相同

運行結果

客戶端

  1 11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
  2 11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
  3 11:55:37.187 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
  4 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:1
  5 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:2
  6 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:3
  7 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:4
  8 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:5
  9 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:6
 10 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:7
 11 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:8
 12 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:9
 13 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:10
 14 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:11
 15 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:12
 16 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:13
 17 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:14
 18 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:15
 19 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:16
 20 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:17
 21 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:18
 22 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:19
 23 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:20
 24 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:21
 25 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:22
 26 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:23
 27 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:24
 28 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:25
 29 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:26
 30 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:27
 31 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:28
 32 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:29
 33 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:30
 34 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:31
 35 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:32
 36 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:33
 37 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:34
 38 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:35
 39 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:36
 40 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:37
 41 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:38
 42 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:39
 43 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:40
 44 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:41
 45 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:42
 46 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:43
 47 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:44
 48 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:45
 49 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:46
 50 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:47
 51 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:48
 52 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:49
 53 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:50
 54 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:51
 55 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:52
 56 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:53
 57 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:54
 58 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:55
 59 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:56
 60 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:57
 61 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:58
 62 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:59
 63 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:60
 64 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:61
 65 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:62
 66 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:63
 67 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:64
 68 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:65
 69 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:66
 70 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:67
 71 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:68
 72 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:69
 73 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:70
 74 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:71
 75 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:72
 76 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:73
 77 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:74
 78 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:75
 79 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:76
 80 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:77
 81 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:78
 82 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:79
 83 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:80
 84 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:81
 85 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:82
 86 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:83
 87 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:84
 88 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:85
 89 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:86
 90 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:87
 91 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:88
 92 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:89
 93 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:90
 94 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:91
 95 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:92
 96 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:93
 97 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:94
 98 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:95
 99 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:96
100 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:97
101 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:98
102 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:99
103 Now is:Sat Jul 21 11:55:37 CST 2018;The client count is:100

服務端

  1 11:55:37.276 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@73046a4d
  2 the time server receive order:QUERY TIME ORDERthe count is:1
  3 the time server receive order:QUERY TIME ORDERthe count is:2
  4 the time server receive order:QUERY TIME ORDERthe count is:3
  5 the time server receive order:QUERY TIME ORDERthe count is:4
  6 the time server receive order:QUERY TIME ORDERthe count is:5
  7 the time server receive order:QUERY TIME ORDERthe count is:6
  8 the time server receive order:QUERY TIME ORDERthe count is:7
  9 the time server receive order:QUERY TIME ORDERthe count is:8
 10 the time server receive order:QUERY TIME ORDERthe count is:9
 11 the time server receive order:QUERY TIME ORDERthe count is:10
 12 the time server receive order:QUERY TIME ORDERthe count is:11
 13 the time server receive order:QUERY TIME ORDERthe count is:12
 14 the time server receive order:QUERY TIME ORDERthe count is:13
 15 the time server receive order:QUERY TIME ORDERthe count is:14
 16 the time server receive order:QUERY TIME ORDERthe count is:15
 17 the time server receive order:QUERY TIME ORDERthe count is:16
 18 the time server receive order:QUERY TIME ORDERthe count is:17
 19 the time server receive order:QUERY TIME ORDERthe count is:18
 20 the time server receive order:QUERY TIME ORDERthe count is:19
 21 the time server receive order:QUERY TIME ORDERthe count is:20
 22 the time server receive order:QUERY TIME ORDERthe count is:21
 23 the time server receive order:QUERY TIME ORDERthe count is:22
 24 the time server receive order:QUERY TIME ORDERthe count is:23
 25 the time server receive order:QUERY TIME ORDERthe count is:24
 26 the time server receive order:QUERY TIME ORDERthe count is:25
 27 the time server receive order:QUERY TIME ORDERthe count is:26
 28 the time server receive order:QUERY TIME ORDERthe count is:27
 29 the time server receive order:QUERY TIME ORDERthe count is:28
 30 the time server receive order:QUERY TIME ORDERthe count is:29
 31 the time server receive order:QUERY TIME ORDERthe count is:30
 32 the time server receive order:QUERY TIME ORDERthe count is:31
 33 the time server receive order:QUERY TIME ORDERthe count is:32
 34 the time server receive order:QUERY TIME ORDERthe count is:33
 35 the time server receive order:QUERY TIME ORDERthe count is:34
 36 the time server receive order:QUERY TIME ORDERthe count is:35
 37 the time server receive order:QUERY TIME ORDERthe count is:36
 38 the time server receive order:QUERY TIME ORDERthe count is:37
 39 the time server receive order:QUERY TIME ORDERthe count is:38
 40 the time server receive order:QUERY TIME ORDERthe count is:39
 41 the time server receive order:QUERY TIME ORDERthe count is:40
 42 the time server receive order:QUERY TIME ORDERthe count is:41
 43 the time server receive order:QUERY TIME ORDERthe count is:42
 44 the time server receive order:QUERY TIME ORDERthe count is:43
 45 the time server receive order:QUERY TIME ORDERthe count is:44
 46 the time server receive order:QUERY TIME ORDERthe count is:45
 47 the time server receive order:QUERY TIME ORDERthe count is:46
 48 the time server receive order:QUERY TIME ORDERthe count is:47
 49 the time server receive order:QUERY TIME ORDERthe count is:48
 50 the time server receive order:QUERY TIME ORDERthe count is:49
 51 the time server receive order:QUERY TIME ORDERthe count is:50
 52 the time server receive order:QUERY TIME ORDERthe count is:51
 53 the time server receive order:QUERY TIME ORDERthe count is:52
 54 the time server receive order:QUERY TIME ORDERthe count is:53
 55 the time server receive order:QUERY TIME ORDERthe count is:54
 56 the time server receive order:QUERY TIME ORDERthe count is:55
 57 the time server receive order:QUERY TIME ORDERthe count is:56
 58 the time server receive order:QUERY TIME ORDERthe count is:57
 59 the time server receive order:QUERY TIME ORDERthe count is:58
 60 the time server receive order:QUERY TIME ORDERthe count is:59
 61 the time server receive order:QUERY TIME ORDERthe count is:60
 62 the time server receive order:QUERY TIME ORDERthe count is:61
 63 the time server receive order:QUERY TIME ORDERthe count is:62
 64 the time server receive order:QUERY TIME ORDERthe count is:63
 65 the time server receive order:QUERY TIME ORDERthe count is:64
 66 the time server receive order:QUERY TIME ORDERthe count is:65
 67 the time server receive order:QUERY TIME ORDERthe count is:66
 68 the time server receive order:QUERY TIME ORDERthe count is:67
 69 the time server receive order:QUERY TIME ORDERthe count is:68
 70 the time server receive order:QUERY TIME ORDERthe count is:69
 71 the time server receive order:QUERY TIME ORDERthe count is:70
 72 the time server receive order:QUERY TIME ORDERthe count is:71
 73 the time server receive order:QUERY TIME ORDERthe count is:72
 74 the time server receive order:QUERY TIME ORDERthe count is:73
 75 the time server receive order:QUERY TIME ORDERthe count is:74
 76 the time server receive order:QUERY TIME ORDERthe count is:75
 77 the time server receive order:QUERY TIME ORDERthe count is:76
 78 the time server receive order:QUERY TIME ORDERthe count is:77
 79 the time server receive order:QUERY TIME ORDERthe count is:78
 80 the time server receive order:QUERY TIME ORDERthe count is:79
 81 the time server receive order:QUERY TIME ORDERthe count is:80
 82 the time server receive order:QUERY TIME ORDERthe count is:81
 83 the time server receive order:QUERY TIME ORDERthe count is:82
 84 the time server receive order:QUERY TIME ORDERthe count is:83
 85 the time server receive order:QUERY TIME ORDERthe count is:84
 86 the time server receive order:QUERY TIME ORDERthe count is:85
 87 the time server receive order:QUERY TIME ORDERthe count is:86
 88 the time server receive order:QUERY TIME ORDERthe count is:87
 89 the time server receive order:QUERY TIME ORDERthe count is:88
 90 the time server receive order:QUERY TIME ORDERthe count is:89
 91 the time server receive order:QUERY TIME ORDERthe count is:90
 92 the time server receive order:QUERY TIME ORDERthe count is:91
 93 the time server receive order:QUERY TIME ORDERthe count is:92
 94 the time server receive order:QUERY TIME ORDERthe count is:93
 95 the time server receive order:QUERY TIME ORDERthe count is:94
 96 the time server receive order:QUERY TIME ORDERthe count is:95
 97 the time server receive order:QUERY TIME ORDERthe count is:96
 98 the time server receive order:QUERY TIME ORDERthe count is:97
 99 the time server receive order:QUERY TIME ORDERthe count is:98
100 the time server receive order:QUERY TIME ORDERthe count is:99
101 the time server receive order:QUERY TIME ORDERthe count is:100

這樣,就解決了粘包的問題。



netty權威指南學習筆記四——TCP粘包/拆包之粘包問題解決