java socket接收保證能讀完數(shù)據(jù)的實例
socket接收保證能讀完數(shù)據(jù)
// 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 讀取數(shù)據(jù)
* @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接收硬件字節(jié)數(shù)據(jù)并解析
第一次接觸這種類型的項目,在處理數(shù)據(jù)過程中,發(fā)現(xiàn)了許多問題,記錄一下,加深記憶。
硬件將數(shù)據(jù)寫在一個buffer中,傳輸過來的是字節(jié)。
一開始我們想到的是按照字節(jié)流來接收,但是,C語言中,byte類型沒有符號位,最大值位255,java中byte類型帶有符號位,最大值為127,問題就出現(xiàn)了,當(dāng)接收到的字節(jié)數(shù)據(jù)超過127時,會取第一位為符號位,后幾位補(bǔ)碼,取反再加一變成負(fù)數(shù)。(處理方法后面有寫到)
后來想偷懶不處理數(shù)據(jù)的基礎(chǔ)上,考慮用char數(shù)組接收。char一共十六位,絕對是可以接收下硬件發(fā)來的八位數(shù)據(jù)的。但是再接收數(shù)據(jù)的時候,還是出現(xiàn)了問題。在對字節(jié)流轉(zhuǎn)變?yōu)樽址鞑⒈4娴絚har數(shù)組中的時候,char類型會自動對數(shù)據(jù)進(jìn)行處理。在char類型中,字符所對應(yīng)的最大十六進(jìn)制是7F,但硬件傳輸來的數(shù)據(jù)存在如0X80,0X8D的情況。當(dāng)char類型接收到大于7F的數(shù)據(jù)時,無法處理,字符會變成亂碼的格式,數(shù)據(jù)相對應(yīng)的也會發(fā)生改變。在接收數(shù)據(jù)的時候就無法正確存儲,更別提后期對數(shù)據(jù)進(jìn)行正確處理和校驗了。放棄。
最終還是要回到byte接收的方向上。和同事討論了下,對于超過java byte類型的數(shù)據(jù),進(jìn)行相應(yīng)處理后,存放在Int中,即可保證數(shù)據(jù)正確性。
處理方法:
對byte數(shù)組中的數(shù)據(jù)進(jìn)行判斷,當(dāng)為負(fù)數(shù)時,與0xff相與,并存放在Int數(shù)組中,可以保證數(shù)據(jù)正常
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();
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
zookeeper實戰(zhàn)之實現(xiàn)分布式鎖的方法
Zookeeper實現(xiàn)分布式鎖比Redis簡單,Zookeeper有一個特性,多個線程在Zookeeper里創(chuàng)建同一個節(jié)點時,只有一個線程執(zhí)行成功,Zookeeper主要是利用臨時有序節(jié)點這一特性實現(xiàn)分布式鎖,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2022-11-11
基于@RequestBody和@ResponseBody及Stringify()的作用說明
這篇文章主要介紹了基于@RequestBody和@ResponseBody及Stringify()的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Boot項目中定制PropertyEditors方法
在本篇文章里小編給大家分享的是一篇關(guān)于Spring Boot定制PropertyEditors的知識點內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2019-11-11
Springboot集成Springbrick實現(xiàn)動態(tài)插件的步驟詳解
這篇文章主要介紹了Springboot集成Springbrick實現(xiàn)動態(tài)插件的詳細(xì)過程,文中的流程通過代碼示例介紹的非常詳細(xì),感興趣的同學(xué)可以參考一下2023-06-06
Java中EasyPoi導(dǎo)出復(fù)雜合并單元格的方法
這篇文章主要介紹了Java中EasyPoi導(dǎo)出復(fù)雜合并單元格的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

