Socket: 반 가방 및 접착 가방 의 처리 방법
private void getDataFormServer(){
isRun = true;
while(isRun){
try{
// InputStream
is = socket.getInputStream();
int length = is.available(); //
// , is
int[] arr = new int[length];
for(int i=0; i 0){
// :
if(arr_storage != null){
arr = add2intArray2oneArray(arr_storage, arr);
}
//
detectionDataHead(arr);
}
}catch (Exception e){
isRun = false;
//writeLogToFile(String string):
writeLogToFile("get Data is fail! " + e.toString());
}
}
}
private void detectionDataHead(int[] arr){
// : 4
if(!detectionDataSign(arr)){
writeLogToFile(" ");
return;
}
// : 5
if(!detectionDataLength(arr)){
return;
}
//CRC : 6
if(!detectionDataCrc(arr)){
return;
}
//
arr_storage = null;
// 。 :
JSONObject jsonObject = serverData2JsonObject(arr); // json
detectionJsonObject(jsonObject); // json
}
private boolean detectionDataSign(int[] arr){
if(arr[0] != 67 && arr[1] != 66){
arr_storage = null;
return false;
}
return true;
}
private boolean detectionDataLength(int[] arr){
int length = 0;
//
for(int i=6; i<10; i++){
length *= 256; /* , int , */
length += arr[i];
}
// == + 10,
if(arr.length == length+10){
return true;
}
//
else if(arr.length < length+10){
arr_storage = arr; //arr_storage int
return false;
}
//
else if(arr.length > length+10){
// , , 。
int[] arr1 = new int[length + 10];
int[] arr2 = new int[length - arr.length];
for(int i = 0; i < length + 10; i++){
arr1[i] = arr[i];
}
for(int j = length; j private boolean detectionDataCrc(int[] arr){
int crc = 0;
/* CRC , int , 。*/
for(int i=2; i<6; i++){
crc *= 256;
crc += arr[i];
}
for(int j = 10; j < arr.length; j++){
crc -= arr[j];
}
if(crc == 0)
return true;
writeLogToFile("CRC ");
return false;
}```
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Codility Lesson3】FrogJmpA small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.