Socket: 반 가방 및 접착 가방 의 처리 방법

3830 단어 알고리즘Android
  • 먼저 생각 을 말 해 보 자.
  • 반 패키지 상황 이 발생 했 을 때 원래 한 단락 의 정 보 는 두 부분 또는 여러 부분 으로 나 뉘 어 메시지 의 완전한 여 부 를 판단 하 는 함수 가 판단 할 수 없 기 때문에 이때 먼저 도착 한 내용 을 저장 하여 나중에 도착 한 내용 과 연결 하 는 데 사용한다.
  • 접착 상황 이 발생 할 때 메시지 A 와 메시지 B 가 밀접 하 게 연결 되 어 메 시 지 를 처리 하 는 함수 가 메 시 지 를 분리 하지 않 으 면 메 시 지 를 잃 어 버 리 거나 프로그램 이 여기에 걸 리 기 쉽다.따라서 데이터 헤드 의 길이 에 따라 받 은 메시지 의 길이 가 일치 하 는 지 판단 하고 메시지 길이 가 데이터 헤드 에 기 록 된 길이 보다 크 면 붙 인 두 메 시 지 를 분리 합 니 다.
  • 먼저 클 라 이언 트 가 서버 정 보 를 감청 하 는 함수 입 니 다.
  • 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());
                }
            }
        }
    
  • 데이터 헤드 를 판단 한다.(PS: 내 가 설정 한 데이터 헤더: 두 식별 자 + 4 비트 CRC + 4 비트 데이터 길이)
  • 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
  • CRC 검사:
  • 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;
        }```
    
    

    좋은 웹페이지 즐겨찾기