java socket Receives Examples of Guaranteed Reading of Data

  • 2021-12-04 18:35:34
  • OfStack

Directory socket reception guarantees that data can be read through socket receives hardware byte data and parses it

socket reception ensures that data can be read out


//    private static byte[] readData(InputStream in,byte[] bData) throws IOException{
//      int readLength = in.read(bData);
//      if(readLength!=bData.length){
//          byte[] temp2 = readData(in,new byte[bData.length-readLength]);
//          System.arraycopy(temp2, 0, bData, readLength, temp2.length);
//          return bData;
//      }else{
//          return bData;
//      }
//    }
//    private static void readData(InputStream in,byte[] bData) throws IOException{
//      readData(in,bData,0,bData.length);
//    }
//    private static void readData(InputStream in,byte[] bData,int off,int length) throws IOException{
//      int readLength = in.read(bData, off, length);
//      if(readLength!=length){
//          readData(in,bData,readLength+off,length-readLength);
//      }
//    }
//    private static void readData(InputStream in,byte[] bData,int off,int length) throws IOException{
//
//      while(true){
//          int readLength = in.read(bData, off, length);
//          if(readLength!=length){
//              off = readLength+off;
//              length = length-readLength;
//          }else{
//              break;
//          }
//      }
//    }
//    private static void readData(InputStream in,byte[] bData,int off,int length) throws IOException{
//      int readLength = 0;
//      do{
//          off = readLength+off;
//          length = length-readLength;
//          readLength = in.read(bData, off, length);
//      }while(readLength!=length);
//    }
    /**
     *  This method is finally used 
     * @param in   Input stream 
         * @param bData   Read data 
     * @throws IOException  
     */
    private static void readData(InputStream in,byte[] bData) throws IOException{
        int off = 0;
        int length = bData.length;
        int readLength = 0;
        do{
            off = readLength+off;
            length = length-readLength;
            readLength = in.read(bData, off, length);
        }while(readLength!=length);
    }

socket receives hardware byte data and parses it

The first contact with this type of project, in the process of processing data, found many problems, record 1, deepen memory.

Hardware writes data in 1 buffer, and transfers bytes.

At first, we thought of receiving according to byte stream. However, in C language, byte type has no sign bit, and the maximum value bit is 255. In java, byte type has sign bit, and the maximum value is 127. The problem arises. When the received byte data exceeds 127, the first bit will be taken as sign bit, and the last few bits will be complemented. (It is written after the processing method)

Later, I want to be lazy and not deal with data, so I consider receiving it with char array. char1 has a total of 106 bits, which can definitely receive 8 bits of data sent by the hardware. However, when receiving data again, there was still a problem. The char type automatically processes the data when a byte stream is changed into a character stream and saved in an char array. In char type, the maximum 106-ary corresponding to characters is 7F, but the data transmitted by hardware exists such as 0X80 and 0X8D. When the char type receives data larger than 7F, it cannot be processed, the characters will become garbled format, and the corresponding data will also change. When receiving data, it can't be stored correctly, let alone processed and verified correctly in the later period. Give up.

Eventually, we will return to the direction of byte reception. After discussing with colleagues, the data exceeding java byte type can be processed and stored in Int to ensure the correctness of the data.

Treatment method:

The data in the byte array is judged. When the data is negative, it is the same as 0xff and stored in the Int array, which can ensure the normal data


ServerSocket serverSocket;
    try {
 serverSocket = new ServerSocket(9090);
 System.out.println("*** Waiting for client connection ***");
 Socket socket = serverSocket.accept();
 InputStream is = socket.getInputStream();
   
 byte[] datas = new byte[500];
 int count = is.read(datas);
   
 int[] dataFormat=new int[500];
 for(int i=0;i<datas.length;i++){
  if(datas[i]<0){
   dataFormat[i]=datas[i]&0xff;
  }else{
   dataFormat[i]=datas[i];
  }
 }
     } catch (IOException e) {
 e.printStackTrace();
     }

Related articles: