20-Netty TCP 粘包和拆包及解决方案
TCP粘包和拆包的基本介紹
對(duì)圖的說明
假設(shè)客戶端分別發(fā)送了兩個(gè)數(shù)據(jù)包D1和D2給服務(wù)端, 由于服務(wù)端一次讀取到字節(jié)數(shù)是不確定的,故有可能存在以下四種情況
TCP粘包和拆包現(xiàn)象實(shí)例
在編寫Netty程序時(shí), 如果沒有做處理,就會(huì)發(fā)生粘包和拆包問題
看一個(gè)具體的實(shí)例
NettyServer
package com.dance.netty.netty.tcp;import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;import java.nio.charset.StandardCharsets; import java.util.UUID;public class NettyServer {public static void main(String[] args) {NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);NioEventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(new NettyServerHandler());}});ChannelFuture sync = serverBootstrap.bind("127.0.0.1", 7000).sync();System.out.println("server is ready ......");sync.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}static class NettyServerHandler extends SimpleChannelInboundHandler<ByteBuf> {private int count = 0;@Overrideprotected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {byte[] bytes = new byte[msg.readableBytes()];msg.readBytes(bytes);count++;System.out.println("服務(wù)器第"+count+"次接收到來自客戶端的數(shù)據(jù):" + new String(bytes, StandardCharsets.UTF_8));// 服務(wù)器回送數(shù)據(jù)給客戶端 回送隨機(jī)的UUID給客戶端ctx.writeAndFlush(Unpooled.copiedBuffer(UUID.randomUUID().toString(),StandardCharsets.UTF_8));}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();cause.printStackTrace();}}}NettyClient
package com.dance.netty.netty.tcp;import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;import java.nio.charset.StandardCharsets;public class NettyClient {public static void main(String[] args) {NioEventLoopGroup eventExecutors = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(eventExecutors).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(new NettyClientHandler());}});ChannelFuture sync = bootstrap.connect("127.0.0.1", 7000).sync();sync.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {eventExecutors.shutdownGracefully();}}static class NettyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {private int count = 0;@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {// 連續(xù)發(fā)送10條數(shù)據(jù)for (int i = 0; i < 10; i++) {ctx.writeAndFlush(Unpooled.copiedBuffer("hello,server!" + i, StandardCharsets.UTF_8));}}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {byte[] bytes = new byte[msg.readableBytes()];msg.readBytes(bytes);// 接收服務(wù)器的返回count++;System.out.println("客戶端第"+count+"次接收服務(wù)端的回送:" + new String(bytes, StandardCharsets.UTF_8));}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();cause.printStackTrace();}} }執(zhí)行結(jié)果
Server
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. server is ready ...... 服務(wù)器第1次接收到來自客戶端的數(shù)據(jù):hello,server!0hello,server!1hello,server!2hello,server!3hello,server!4hello,server!5hello,server!6hello,server!7hello,server!8hello,server!9 服務(wù)器第1次接收到來自客戶端的數(shù)據(jù):hello,server!0 服務(wù)器第2次接收到來自客戶端的數(shù)據(jù):hello,server!1 服務(wù)器第3次接收到來自客戶端的數(shù)據(jù):hello,server!2hello,server!3hello,server!4 服務(wù)器第4次接收到來自客戶端的數(shù)據(jù):hello,server!5hello,server!6 服務(wù)器第5次接收到來自客戶端的數(shù)據(jù):hello,server!7hello,server!8hello,server!9Client1
客戶端第1次接收服務(wù)端的回送:84653e99-0e7f-431d-a897-c215af959a3bClient2
客戶端第1次接收服務(wù)端的回送:6f3b0e79-2f40-4066-bb6b-80f988ecec116b6bbd94-b345-46d6-8d36-a114534331a850628e04-ece1-4f58-b684-d30189f6cf26b2139027-6bda-4d40-9238-9fc0e59bc7a64b568ffe-f616-4f48-8f1c-05ecf3e817ee分析:
服務(wù)器啟動(dòng)后到server is ready ......
第一個(gè)客戶端啟動(dòng)后 TCP將10次發(fā)送直接封包成一次直接發(fā)送,所以導(dǎo)致了服務(wù)器一次就收到了所有的數(shù)據(jù),產(chǎn)生了TCP粘包,拆包的問題
第二客戶端啟動(dòng)后 TCP將10次發(fā)送分別封裝成了5次請(qǐng)求,產(chǎn)生粘包,拆包問題
TCP粘包和拆包解決方案
TCP粘包, 拆包解決方案實(shí)現(xiàn)
新建協(xié)議MessageProtocol
package com.dance.netty.netty.protocoltcp;/*** 消息協(xié)議*/ public class MessageProtocol {private int length;private byte[] content;public MessageProtocol() {}public MessageProtocol(int length, byte[] content) {this.length = length;this.content = content;}public int getLength() {return length;}public void setLength(int length) {this.length = length;}public byte[] getContent() {return content;}public void setContent(byte[] content) {this.content = content;} }新建編碼器
package com.dance.netty.netty.protocoltcp;import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder;/*** 自定義協(xié)議編碼器*/ public class MyMessageProtocolEncoder extends MessageToByteEncoder<MessageProtocol> {@Overrideprotected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception { // System.out.println("自定義協(xié)議---->開始編碼");// 開始發(fā)送數(shù)據(jù)out.writeInt(msg.getLength()); // 優(yōu)先發(fā)送長(zhǎng)度,定義邊界out.writeBytes(msg.getContent()); // System.out.println("自定義協(xié)議---->編碼完成");} }新建解碼器
package com.dance.netty.netty.protocoltcp;import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder;import java.util.List;public class MyMessageProtocolDecoder extends ByteToMessageDecoder {@Overrideprotected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // System.out.println("自定義協(xié)議---->開始解碼");// 獲取定義的邊界長(zhǎng)度int length = in.readInt();if(in.readableBytes() >= length){// 根據(jù)長(zhǎng)度讀取數(shù)據(jù)byte[] bytes = new byte[length];in.readBytes(bytes);// 反構(gòu)造成MessageProtocolMessageProtocol messageProtocol = new MessageProtocol(length, bytes);out.add(messageProtocol); // System.out.println("自定義協(xié)議---->解碼完成");}else{// 內(nèi)容長(zhǎng)度不夠}} }新建服務(wù)器端
package com.dance.netty.netty.protocoltcp;import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;import java.nio.charset.StandardCharsets; import java.util.UUID;public class NettyServer {public static void main(String[] args) {NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);NioEventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();// 加入自定義協(xié)議編解碼器pipeline.addLast(new MyMessageProtocolDecoder());pipeline.addLast(new MyMessageProtocolEncoder());pipeline.addLast(new NettyServerHandler());}});ChannelFuture sync = serverBootstrap.bind("127.0.0.1", 7000).sync();System.out.println("server is ready ......");sync.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}static class NettyServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {private int count = 0;@Overrideprotected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {byte[] bytes = msg.getContent();count++;System.out.println("服務(wù)器第"+count+"次接收到來自客戶端的數(shù)據(jù):" + new String(bytes, StandardCharsets.UTF_8));// 服務(wù)器回送數(shù)據(jù)給客戶端 回送隨機(jī)的UUID給客戶端byte[] s = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8);ctx.writeAndFlush(new MessageProtocol(s.length,s));}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();cause.printStackTrace();}}}新建客戶端
package com.dance.netty.netty.protocoltcp;import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;import java.nio.charset.StandardCharsets;public class NettyClient {public static void main(String[] args) {NioEventLoopGroup eventExecutors = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(eventExecutors).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();// 加入自定義分割符號(hào) // ByteBuf delimiter = Unpooled.copiedBuffer("\r\n".getBytes()); // pipeline.addFirst(new DelimiterBasedFrameDecoder(8192, delimiter));// 添加自定義協(xié)議編解碼器pipeline.addLast(new MyMessageProtocolDecoder());pipeline.addLast(new MyMessageProtocolEncoder());pipeline.addLast(new NettyClientHandler());}});ChannelFuture sync = bootstrap.connect("127.0.0.1", 7000).sync();sync.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {eventExecutors.shutdownGracefully();}}static class NettyClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {private int count = 0;@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {// 連續(xù)發(fā)送10條數(shù)據(jù)for (int i = 0; i < 10; i++) {String msg = "今天天氣冷, 打火鍋" + i;byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);// 使用自定義協(xié)議MessageProtocol messageProtocol = new MessageProtocol(bytes.length, bytes);ctx.writeAndFlush(messageProtocol);}}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {byte[] bytes = msg.getContent();// 接收服務(wù)器的返回count++;System.out.println("客戶端第"+count+"次接收服務(wù)端的回送:" + new String(bytes, StandardCharsets.UTF_8));}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();cause.printStackTrace();}} }測(cè)試
發(fā)送10次
服務(wù)器端
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. server is ready ...... 服務(wù)器第1次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋0 ...... 服務(wù)器第10次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋9客戶端
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 客戶端第1次接收服務(wù)端的回送:a6b69f1c-daba-435a-802a-c19a6350ca94 ...... 客戶端第10次接收服務(wù)端的回送:5af5c297-8668-48aa-b8c4-35656142f591ok,沒有問題, 但是真的沒有問題嗎?答案是有問題
FAQ
發(fā)送1000次
修改客戶端發(fā)送消息數(shù)量
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception {// 連續(xù)發(fā)送10條數(shù)據(jù)for (int i = 0; i < 1000; i++) {......} }重新測(cè)試
服務(wù)器端
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. server is ready ...... 服務(wù)器第1次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋0 ...... 服務(wù)器第31次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋30 服務(wù)器第32次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋31 io.netty.handler.codec.DecoderException: java.lang.NegativeArraySizeExceptionat io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1412)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:943)at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:141)at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NegativeArraySizeExceptionat com.dance.netty.netty.protocoltcp.MyMessageProtocolDecoder.decode(MyMessageProtocolDecoder.java:17)at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)... 16 more io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(1022) + length(4) exceeds writerIndex(1024): PooledUnsafeDirectByteBuf(ridx: 1022, widx: 1024, cap: 1024)at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)at io.netty.handler.codec.ByteToMessageDecoder.channelInputClosed(ByteToMessageDecoder.java:392)at io.netty.handler.codec.ByteToMessageDecoder.channelInputClosed(ByteToMessageDecoder.java:359)at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:342)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1407)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:925)at io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:822)at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.IndexOutOfBoundsException: readerIndex(1022) + length(4) exceeds writerIndex(1024): PooledUnsafeDirectByteBuf(ridx: 1022, widx: 1024, cap: 1024)at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1403)at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:786)at com.dance.netty.netty.protocoltcp.MyMessageProtocolDecoder.decode(MyMessageProtocolDecoder.java:14)at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)... 17 morewhat ? 直接報(bào)錯(cuò)了, 數(shù)組下標(biāo)越界, 讀索引1022 + 長(zhǎng)度4 > 寫縮影1024了
這個(gè)是什么問題呢 ? 我看網(wǎng)上關(guān)于這個(gè)BUG的解決方案很少,基本沒有, 好多都是貼問題的, 我翻了將近1個(gè)小時(shí),才找到一個(gè)大佬寫的一篇文章解決了, 感謝大佬
博客地址:
https://blog.csdn.net/u011035407/article/details/80454511問題描述:
這樣在剛開始的工作中數(shù)據(jù)包傳輸沒有問題,不過數(shù)據(jù)包的大小超過512b的時(shí)候就會(huì)拋出異常了。
解決方案
配合解碼器DelimiterBasedFrameDecoder一起使用,在數(shù)據(jù)包的末尾使用換行符\n表示本次數(shù)據(jù)包已經(jīng)結(jié)束,當(dāng)DelimiterBasedFrameDecoder把數(shù)據(jù)切割之后,再使用ByteToMessageDecoder實(shí)現(xiàn)decode方法把數(shù)據(jù)流轉(zhuǎn)換為Message對(duì)象。
我們?cè)?span id="ze8trgl8bvbq" class="ne-text">ChannelPipeline加入DelimiterBasedFrameDecoder解碼器
客戶端和服務(wù)器端都加
//使用\n作為分隔符 pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));在MessageToByteEncoder的實(shí)現(xiàn)方法encode()增加out.writeBytes(new byte[]{'\n'});
//在寫出字節(jié)流的末尾增加\n表示數(shù)據(jù)結(jié)束 out.writeBytes(new byte[]{'\n'});這時(shí)候就可以愉快的繼續(xù)處理數(shù)據(jù)了。等我還沒有高興半天的時(shí)候,問題又來了。還是一樣的問題
等等等,,,怎么又報(bào)錯(cuò)了,不是已經(jīng)加了黏包處理了嗎??,解決問題把,首先看解析的數(shù)據(jù)包結(jié)構(gòu)
+-------------------------------------------------+| 0 1 2 3 4 5 6 7 8 9 a b c d e f | +--------+-------------------------------------------------+----------------+ |00000000| 01 01 01 00 00 00 06 00 00 01 0a 7b 22 69 64 22 |...........{"id"| |00000010| 3a 33 2c 22 75 73 65 72 6e 61 6d 65 22 3a 22 31 |:3,"username":"1| |00000020| 38 35 30 30 33 34 30 31 36 39 22 2c 22 6e 69 63 |8500340169","nic| |00000030| 6b 6e 61 6d 65 22 3a 22 e4 bb 96 e5 9b 9b e5 a4 |kname":"........| |00000040| a7 e7 88 b7 22 2c 22 72 6f 6f 6d 49 64 22 3a 31 |....","roomId":1| |00000050| 35 32 37 32 33 38 35 36 39 34 37 34 2c 22 74 65 |527238569474,"te| |00000060| 61 6d 4e 61 6d 65 22 3a 22 e4 bf 84 e7 bd 97 e6 |amName":".......| |00000070| 96 af 22 2c 22 75 6e 69 74 73 22 3a 7b 22 75 6e |..","units":{"un| |00000080| 69 74 31 22 3a 7b 22 78 22 3a 31 30 2e 30 2c 22 |it1":{"x":10.0,"| |00000090| 79 22 3a 31 30 2e 30 7d 2c 22 75 6e 69 74 32 22 |y":10.0},"unit2"| |000000a0| 3a 7b 22 78 22 3a 31 30 2e 30 2c 22 79 22 3a 31 |:{"x":10.0,"y":1| |000000b0| 30 2e 30 7d 2c 22 75 6e 69 74 33 22 3a 7b 22 78 |0.0},"unit3":{"x| |000000c0| 22 3a 31 30 2e 30 2c 22 79 22 3a 31 30 2e 30 7d |":10.0,"y":10.0}| |000000d0| 2c 22 75 6e 69 74 34 22 3a 7b 22 78 22 3a 31 30 |,"unit4":{"x":10| |000000e0| 2e 30 2c 22 79 22 3a 31 30 2e 30 7d 2c 22 75 6e |.0,"y":10.0},"un| |000000f0| 69 74 35 22 3a 7b 22 78 22 3a 31 30 2e 30 2c 22 |it5":{"x":10.0,"| |00000100| 79 22 3a 31 30 2e 30 7d 7d 2c 22 73 74 61 74 75 |y":10.0}},"statu| |00000110| 73 22 3a 31 7d 0a |s":1}. | +--------+-------------------------------------------------+----------------+接收到的數(shù)據(jù)是完整的沒錯(cuò),但是還是報(bào)錯(cuò)了,而且數(shù)據(jù)結(jié)尾的字節(jié)的確是0a,轉(zhuǎn)化成字符就是\n沒有問題啊。
在ByteToMessageDecoder的decode方法里打印ByteBuf buf的長(zhǎng)度之后,問題找到了 長(zhǎng)度 : 10
這就是說在進(jìn)入到ByteToMessageDecoder這個(gè)解碼器的時(shí)候,數(shù)據(jù)包已經(jīng)只剩下10個(gè)長(zhǎng)度了,那么長(zhǎng)的數(shù)據(jù)被上個(gè)解碼器DelimiterBasedFrameDecoder隔空劈開了- -。問題出現(xiàn)在哪呢,看上面那塊字節(jié)流的字節(jié),找到第11個(gè)字節(jié),是0a。。。。因?yàn)椴皇菢?biāo)準(zhǔn)的json格式,最前面使用了3個(gè)字節(jié) 加上2個(gè)int長(zhǎng)度的屬性,所以 數(shù)據(jù)包頭應(yīng)該是11個(gè)字節(jié)長(zhǎng)。
而DelimiterBasedFrameDecoder在讀到第11個(gè)字節(jié)的時(shí)候讀成了\n,自然而然的就認(rèn)為這個(gè)數(shù)據(jù)包已經(jīng)結(jié)束了,而數(shù)據(jù)進(jìn)入到ByteToMessageDecoder的時(shí)候就會(huì)因?yàn)橐?guī)定的body長(zhǎng)度不等于length長(zhǎng)度而出現(xiàn)問題。
思來想去 不實(shí)用\n 這樣的單字節(jié)作為換行符,很容易在數(shù)據(jù)流中遇到,轉(zhuǎn)而使用\r\n倆字節(jié)來處理,而這倆字節(jié)出現(xiàn)在前面兩個(gè)int長(zhǎng)度中的幾率應(yīng)該很小。
最終解決
在客戶端和服務(wù)器端的pipeline中添加 以 "\r\n" 定義為邊界的符號(hào)來標(biāo)識(shí)數(shù)據(jù)包結(jié)束
//這里使用自定義分隔符 ByteBuf delimiter = Unpooled.copiedBuffer("\r\n".getBytes()); pipeline.addFirst(new DelimiterBasedFrameDecoder(8192, delimiter));Server端
Client端
編碼器中發(fā)送結(jié)束位置增加
//這里最后修改使用\r\n out.writeBytes(new byte[]{'\r','\n'});再次運(yùn)行程序 數(shù)據(jù)包可以正常接收了。
最終測(cè)試
服務(wù)器端
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. server is ready ...... 服務(wù)器第1次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋0 ...... 服務(wù)器第999次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋998 服務(wù)器第1000次接收到來自客戶端的數(shù)據(jù):今天天氣冷, 打火鍋999客戶端
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 客戶端第1次接收服務(wù)端的回送:48fa6d78-8079-4700-b488-ca2af9eb3f8c ...... 客戶端第999次接收服務(wù)端的回送:581da47b-d77b-4972-af11-6d33057f6610 客戶端第1000次接收服務(wù)端的回送:0014e906-69cb-4900-9409-f4d1af9148dd總結(jié)
以前使用netty的時(shí)候也僅限于和硬件交互,而當(dāng)時(shí)的硬件受限于成本問題是一條一條處理數(shù)據(jù)包的,所以基本上不會(huì)考慮黏包問題
然后就是ByteToMessageDecoder和MessageToByteEncoder兩個(gè)類是比較底層實(shí)現(xiàn)數(shù)據(jù)流處理的,并沒有帶有拆包黏包的處理機(jī)制,需要自己在數(shù)據(jù)包頭規(guī)定包的長(zhǎng)度,而且無法處理過大的數(shù)據(jù)包,因?yàn)槲乙婚_始首先使用了這種方式處理數(shù)據(jù),所以后來就沒有再換成DelimiterBasedFrameDecoder加 StringDecoder來解析數(shù)據(jù)包,最后使用json直接轉(zhuǎn)化為對(duì)象。
FAQ 參考粘貼于大佬的博客,加自己的改動(dòng)
若有收獲,就點(diǎn)個(gè)贊吧
總結(jié)
以上是生活随笔為你收集整理的20-Netty TCP 粘包和拆包及解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 七日杀Liunx SteamCMD开服超
- 下一篇: 一看就会!一篇全搞定!权限处理专家--S