背景
对tcp传输的数据按结尾字符*进行拆包,实际传输的内容是json格式的数据。包括测试代码,如果有错误,敬请指出,谢谢!
代码
package com.example.demo;
import com.alibaba.fastjson.JSONObject;
import com.ctc.wstx.util.StringUtil;
import org.springframework.util.StringUtils;
import java.util.UUID;
/**
* tcp拆包
*
* @date 2021/9/3 11:08
*/
public class aa {
private static String prefix = "";
public static void main(String[] args) {
String aa[] = new String[5];
//1.测试数据不完整的情况,但是开头是完整的
aa[0] = "{\"aabbcc\":\"1001\"}*{\"aabbcc\":\"1\"}*{\"clien";
aa[1] = "tId\":\"1\"}*{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic_pressure\":0,\"timestamp\":1635325129}*";
aa[2] = "tId\":\"1\"}*{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic_pressure\":0,\"timestamp\":1635325129}*";
aa[3] = "{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic_pressure\":0,\"timestamp\":1635325129}*{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic";
aa[4] = "_pressure\":0,\"timestamp\":1635325129}*{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic_pressure\":0,\"timestamp\":1635325129}*{\"aabbcc\"";
System.out.println("=================情况1=================");
System.out.println("预计丢失数据=================【tId\":\"1\"}】");
test(aa);
//2.测试数据开头不完整,需要丢掉
aa[0] = "{\"aabbcc\":\"1*{\"aabbcc\":\"1001\"}*{\"aabbcc\":\"1\"}*{\"clien";
aa[1] = "{\"aabbcc\":\"1001\"}*{\"aabbcc\":\"1\"}*{\"clien";
aa[2] = "tId\":\"1\"}*{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic_pressure\":0,\"timestamp\":1635325129}*";
aa[3] = "{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic_pressure\":0,\"timestamp\":1635325129}*{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic";
aa[4] = "_pressure\":0,\"timestamp\":1635325129}*{\"move\":0,\"aa\":0,\"s\":0,\"bb\":0,\"id\":10,\"p\":10,\"t\":7,\"h\":70,\"lic_pressure\":0,\"timestamp\":1635325129}*{\"aabbcc\"";
System.out.println("=================情况2=================");
System.out.println("预计丢失数据=================【{\"aabbcc\"{\"aabbcc\":\"1】");
test(aa);
//3.测试完整数据
aa[0] = "{\"aabbcc\":\"1\"}*";
aa[1] = "{\"aabbcc\":\"1\"}*";
aa[2] = "{\"aabbcc\":\"1\"}*";
aa[3] = "{\"aabbcc\":\"1\"}*";
aa[4] = "{\"aabbcc\":\"1\"}*";
System.out.println("=================情况3=================");
test(aa);
//4.测试完整数据多条
aa[0] = "{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*";
aa[1] = "{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*";
aa[2] = "{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*";
aa[3] = "{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*";
aa[4] = "{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*{\"aabbcc\":\"1\"}*";
System.out.println("=================情况4=================");
test(aa);
}
private static void test(String[] aa) {
int a = 1;
for (String json : aa) {
System.out.println("第" + a + "次数据!!");
if ("".equals(json) || json == null) {
return;
}
if ("".equals(prefix)) {
String val[] = json.split("\\*");
if (val[val.length - 1] != "" && val[val.length - 1] != null && !((val[val.length - 1].startsWith("{") && val[val.length - 1].endsWith("}")) || (val[val.length - 1].startsWith("[") && val[val.length - 1].endsWith("]")))) {
prefix = val[val.length - 1];
for (int i = 0; i < val.length - 1; i++) {
String arr = val[i];
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
} else {
System.out.println("错误数据丢掉1:" + arr);
}
}
} else {
for (String arr : val) {
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
//第一个数据完整
System.out.println("接收数据:" + arr);
} else {
System.out.println("错误数据丢掉2:" + arr);
}
}
}
} else {
String value = prefix + json;
String oldPrefix = prefix;
prefix = "";
String val[] = value.split("\\*");
if (val[val.length - 1] != "" && val[val.length - 1] != null && !((val[val.length - 1].startsWith("{") && val[val.length - 1].endsWith("}")) || (val[val.length - 1].startsWith("[") && val[val.length - 1].endsWith("]")))) {
prefix = val[val.length - 1];
for (int i = 0; i < val.length - 1; i++) {
String arr = val[i];
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
try {
JSONObject.parseObject(arr);
System.out.println("接收数据:" + arr);
} catch (Exception e) {
arr = arr.substring(oldPrefix.length());
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
System.out.println("错误数据丢掉3:" + oldPrefix);
} else {
System.out.println("错误数据丢掉4:" + oldPrefix + arr);
}
}
} else {
try {
JSONObject.parseObject(arr);
System.out.println("接收数据:" + arr);
} catch (Exception e) {
arr = arr.substring(oldPrefix.length());
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
System.out.println("错误数据丢掉10:" + oldPrefix);
} else {
System.out.println("错误数据丢掉11:" + oldPrefix + arr);
}
}
}
}
} else {
for (String arr : val) {
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
//第一个数据完整
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
try {
JSONObject.parseObject(arr);
System.out.println("接收数据:" + arr);
} catch (Exception e) {
arr = arr.substring(oldPrefix.length());
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
System.out.println("错误数据丢掉6:" + oldPrefix);
} else {
System.out.println("错误数据丢掉7:" + oldPrefix + arr);
}
}
} else {
System.out.println("错误数据丢掉8:" + oldPrefix + arr);
}
} else {
System.out.println("错误数据丢掉9:" + oldPrefix + arr);
}
}
}
}
a++;
}
System.out.println("当前prefix值:" + prefix);
}
private static void unpack(String json) {
if ("".equals(json) || json == null) {
return;
}
if ("".equals(prefix)) {
String val[] = json.split("\\*");
if (val[val.length - 1] != "" && val[val.length - 1] != null && !((val[val.length - 1].startsWith("{") && val[val.length - 1].endsWith("}")) || (val[val.length - 1].startsWith("[") && val[val.length - 1].endsWith("]")))) {
prefix = val[val.length - 1];
for (int i = 0; i < val.length - 1; i++) {
String arr = val[i];
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
} else {
System.out.println("错误数据丢掉1:" + arr);
}
}
} else {
for (String arr : val) {
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
//第一个数据完整
System.out.println("接收数据:" + arr);
} else {
System.out.println("错误数据丢掉2:" + arr);
}
}
}
} else {
String value = prefix + json;
String oldPrefix = prefix;
prefix = "";
String val[] = value.split("\\*");
if (val[val.length - 1] != "" && val[val.length - 1] != null && !((val[val.length - 1].startsWith("{") && val[val.length - 1].endsWith("}")) || (val[val.length - 1].startsWith("[") && val[val.length - 1].endsWith("]")))) {
prefix = val[val.length - 1];
for (int i = 0; i < val.length - 1; i++) {
String arr = val[i];
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
try {
JSONObject.parseObject(arr);
System.out.println("接收数据:" + arr);
} catch (Exception e) {
arr = arr.substring(oldPrefix.length());
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
System.out.println("错误数据丢掉3:" + oldPrefix);
} else {
System.out.println("错误数据丢掉4:" + oldPrefix + arr);
}
}
} else {
try {
JSONObject.parseObject(arr);
System.out.println("接收数据:" + arr);
} catch (Exception e) {
arr = arr.substring(oldPrefix.length());
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
System.out.println("错误数据丢掉10:" + oldPrefix);
} else {
System.out.println("错误数据丢掉11:" + oldPrefix + arr);
}
}
}
}
} else {
for (String arr : val) {
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
//第一个数据完整
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
try {
JSONObject.parseObject(arr);
System.out.println("接收数据:" + arr);
} catch (Exception e) {
arr = arr.substring(oldPrefix.length());
if ((arr.startsWith("{") && arr.endsWith("}")) || (arr.startsWith("[") && arr.endsWith("]"))) {
System.out.println("接收数据:" + arr);
System.out.println("错误数据丢掉6:" + oldPrefix);
} else {
System.out.println("错误数据丢掉7:" + oldPrefix + arr);
}
}
} else {
System.out.println("错误数据丢掉8:" + oldPrefix + arr);
}
} else {
System.out.println("错误数据丢掉9:" + oldPrefix + arr);
}
}
}
}
System.out.println("当前prefix值:" + prefix);
}
}