求助!关于mina粘包的问题
接收的消息:HTTP头+空行+消息体
@Override
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput output) throws Exception {
CharsetDecoder cd = charset.newDecoder();
int i = 1;// 行数
//int byteCount = 0;// 每行的字节数
if(in.remaining()<4){//当拆包的时候剩余长度小于4的时候的保护
return false;
}
while (in.hasRemaining()) {
// 1、分离HTTP头,获取消息体的长度
in.mark();//标志当前位置,以便reset
byte b = in.get();
buffer.put(b);
if (b == "\n".getBytes()[0] && i <= 8) {
buffer.flip();
String line = buffer.getString(buffer.limit(), cd);
httpHeader.append(line);
if (line.contains("Content-Length")) {// 第6行,contentLength
contentLength = Integer.parseInt(line.split(":")[1].trim());
session.setAttribute(CCBCodecFactory.SESSION_HTTP_LENGTH,contentLength);
}
buffer.clear();
i++;
}
if (i == 9) {
System.out.println("获取的HTTP头信息:" + httpHeader.toString());
session.setAttribute(CCBCodecFactory.SESSION_HTTP_CONTEXT,httpHeader.toString());
// 2、解密报文,in.remaining()返回position和limit之前的字节数
try {
if (in.remaining() < contentLength) {
in.reset();
return false;//接收新数据,以拼凑成完整数据
} else {
int bodyLength = (Integer) session.getAttribute(CCBCodecFactory.SESSION_HTTP_LENGTH);
byte[] encryptedMessage = new byte[bodyLength];
in.get(encryptedMessage);
//业务处理 略
output.write(contents);
}
if(in.remaining()>0){
return true;
}
} catch (SecException e) {
Logger.error("报文解密失败" + e);
}
}
}
return false;
}
解码器先获取http头,得到消息体的长度,然后获取消息体 ,但是如果消息体的长度不够,新获取的数据是否和获取的HTTP头拼接呢?然后重新执行decode方法,再次获取HTTP头呢?
@Override
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput output) throws Exception {
CharsetDecoder cd = charset.newDecoder();
int i = 1;// 行数
//int byteCount = 0;// 每行的字节数
if(in.remaining()<4){//当拆包的时候剩余长度小于4的时候的保护
return false;
}
while (in.hasRemaining()) {
// 1、分离HTTP头,获取消息体的长度
in.mark();//标志当前位置,以便reset
byte b = in.get();
buffer.put(b);
if (b == "\n".getBytes()[0] && i <= 8) {
buffer.flip();
String line = buffer.getString(buffer.limit(), cd);
httpHeader.append(line);
if (line.contains("Content-Length")) {// 第6行,contentLength
contentLength = Integer.parseInt(line.split(":")[1].trim());
session.setAttribute(CCBCodecFactory.SESSION_HTTP_LENGTH,contentLength);
}
buffer.clear();
i++;
}
if (i == 9) {
System.out.println("获取的HTTP头信息:" + httpHeader.toString());
session.setAttribute(CCBCodecFactory.SESSION_HTTP_CONTEXT,httpHeader.toString());
// 2、解密报文,in.remaining()返回position和limit之前的字节数
try {
if (in.remaining() < contentLength) {
in.reset();
return false;//接收新数据,以拼凑成完整数据
} else {
int bodyLength = (Integer) session.getAttribute(CCBCodecFactory.SESSION_HTTP_LENGTH);
byte[] encryptedMessage = new byte[bodyLength];
in.get(encryptedMessage);
//业务处理 略
output.write(contents);
}
if(in.remaining()>0){
return true;
}
} catch (SecException e) {
Logger.error("报文解密失败" + e);
}
}
}
return false;
}
解码器先获取http头,得到消息体的长度,然后获取消息体 ,但是如果消息体的长度不够,新获取的数据是否和获取的HTTP头拼接呢?然后重新执行decode方法,再次获取HTTP头呢?
...全文
相关推荐
mina 断包,粘包问题解决
本文将深入探讨“mina断包”和“粘包”问题,以及如何通过提供的mina_optimize代码解决这些问题。首先,我们要理解什么是“断包”和“粘包”。
通过设置报文头固定长度解决mina断包粘包的问题
基于CumulativeProtocolDecoder解决断包粘包的问题
Mina断包,粘包处理(完整实例,有客户端,服务端)
本实例主要关注的是Mina在处理网络通信时遇到的两个常见问题——“断包”和“粘包”。1.
springboot mina 串口对接
综上所述,SpringBoot与Mina的结合,使我们能够方便地在Java应用程序中实现串口通信,解决粘包和断包问题,同时可能借助WebSocket实现与Web客户端的交互。"
mina 协议 解包 粘包
该项目基于Apache MINA框架实现了TCP客户端与服务器之间的网络通信。服务端监听指定端口,使用文本行编解码器处理消息,并通过自定义过滤器和处理器实现日志监控与业务逻辑处理。客户端连接服务端并发


