# netty-learn **Repository Path**: who7708/netty-learn ## Basic Information - **Project Name**: netty-learn - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-05-16 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Channle 接口实现 - EmbeddedChannel - LocalServerChannel - NioDatagramChannel - NioSctpChannel - NioSocketChannel # ChannelHandler 常用的适配器类 - ChannelHandlerAdapter - ChannelInboundHandlerAdapter - ChannelOutboundHandlerAdapter - ChannelDuplexHandler # 关于TCP的拆包,粘包问题 TCP是一个"流"的协议,所谓流就是没有界限的遗传数据,所谓流就是没有界限的遗传数据 在业务上一个完整的包可能会被TCP分成多个包进行发送,也可能把多个小包封装成一个大包发送出去 [可以查看](https://blog.csdn.net/weixin_38626484/article/details/78750939) ## TCP粘包,拆包问题: 1.应用程序writer 写入的字节大小大于套接口 发送缓冲区的大小 2.进行MSS大小的TCP分段 3.以太网帧的payload大于MTU进行IP分片 ## 常用解决方案: 1.消息定长,例如每个报文的大小固定200个字节,不过不够空位补齐 ``` sc.pipeline().addLast(new FixedLengthFrameDecoder(5)); ``` 2.在包尾部增加特殊字符分割,例如加回车等 ``` ByteBuf buf=Unpooled.copiedBuffer("$_".getBytes()); sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); ``` 3.将消息分为消息头和消息体,在消息头中包含表示消息总长度的字段,然后进行业务逻辑的处理