Sever的代碼inputStream.read()讀取錯誤的布爾值
if(success){
out.write("true".getBytes().length);
out.write("true".getBytes());
out.flush();
}
else{
out.write("false".getBytes().length);
out.write("false".getBytes());
out.flush();
}
客戶端代碼
int size = inputStream.read();
byte[] buf = new byte[size];
inputStream.read(buf);
ns = new String(buf);
Boolean.valueOf(ns);
雖然服務器將結果發送客戶端讀錯。這裏有什麼問題?我該如何解決它。作爲示例服務器發送值爲true,但客戶端將其作爲虛假接收它
「讀錯」 並沒有給我們任何* *信息。假設所有字符串都是<256字節,對數據進行兩次編碼,假設只有一次調用read(byte []),那麼在這裏出現了各種各樣的錯誤,比如使用平臺默認編碼。 '會讀取所有數據等。考慮使用'DataOutputStream'來代替... –