InputStream is = socket.getInputStream();
BufferedInputStream reader = new BufferedInputStream(is);
byte[] buffer = new byte[10];
if (10 != reader.read(buffer)) {
logger.error("Abnormal reading of message header");
throw new Exception("异常");
}
String ssss = new String(buffer);
int len = Integer.parseInt(ssss);
logger.info("---------back ------------------------start-----------------");
logger.info("read body length:" + len);
byte[] body = new byte[len];
reader.read(body);
String bodyStr = new String(body);
int saveLength =bodyStr.trim().getBytes().length;
logger.info("bodyStr.getBytes().length"+saveLength);
// 增加判断如果长度与接收到的记录数据不一致,再接收剩余的字节数据
if(saveLength < len){
byte[] left = new byte[len-saveLength];
reader.read(left);
byte[] part0 = new byte[saveLength];
System.arraycopy(body,0,part0,0,saveLength);
body = addBytes(part0, left);
}
is.close();
reader.close();
socket.close();
socket 接收超过16k数据中断,拼接返回值
于 2023-08-24 14:27:34 首次发布