socket接收保证能读完数据

//    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);
//    }
    /**
     * 最终使用此方法
     * @param in  输入流
         * @param bData  读取数据
     * @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接收硬件字节数据并解析

第一次接触这种类型的项目,在处理数据过程中,发现了许多问题,记录一下,加深记忆。

硬件将数据写在一个buffer中,传输过来的是字节。

一开始我们想到的是按照字节流来接收,但是,C语言中,byte类型没有符号位,最大值位255,java中byte类型带有符号位,最大值为127,问题就出现了,当接收到的字节数据超过127时,会取第一位为符号位,后几位补码,取反再加一变成负数。(处理方法后面有写到)

后来想偷懒不处理数据的基础上,考虑用char数组接收。char一共十六位,绝对是可以接收下硬件发来的八位数据的。但是再接收数据的时候,还是出现了问题。在对字节流转变为字符流并保存到char数组中的时候,char类型会自动对数据进行处理。在char类型中,字符所对应的最大十六进制是7F,但硬件传输来的数据存在如0X80,0X8D的情况。当char类型接收到大于7F的数据时,无法处理,字符会变成乱码的格式,数据相对应的也会发生改变。在接收数据的时候就无法正确存储,更别提后期对数据进行正确处理和校验了。放弃。

最终还是要回到byte接收的方向上。和同事讨论了下,对于超过java byte类型的数据,进行相应处理后,存放在Int中,即可保证数据正确性。

处理方法:

对byte数组中的数据进行判断,当为负数时,与0xff相与,并存放在Int数组中,可以保证数据正常

ServerSocket serverSocket;
    try {
 serverSocket = new ServerSocket(9090);
 System.out.println("***等待客户端连接***");
 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();
     }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠悠之家。

点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部