socket接收保證能讀完數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// 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數組中,可以保證數據正常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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(); } |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/guanzhengyinqin/article/details/79402165