![](/img/trans.png)
[英]BufferedInputStream.read(byte[]) Causes problems. Anyone have this problem before?
[英]How to get true byte order with BufferedInputStream.read()?
服務器根據以下條件向我發送一個整數:
DataOutputStream stdOut = new DataOutputStream(myServerSocket.getOutputStream());
stdOut.writeInt(1);
stdOut.flush();
我嘗試通過以下方式向客戶閱讀:
BufferedInputStream dIn = new BufferedInputStream(myClientSocket.getInputStream());
byte[] reception = new byte[4]; // writeInt() writes 4 bytes
int status = -1;
status = dIn.read(reception, 0, 4);
// status = 1
// (int) reception = 0
我正在等待4個字節來計算發送的Int。 但是,它僅接收1個字節並對其進行處理,而無需等待剩余的字節。 因為我等待4個字節,所以我嘗試了以下方法:
int count = 0;
while(count < 4) {
status = dIn.read(reception);
count += status;
System.out.println("Read: " + count);
}*/
我看到它讀取1個字節,然后讀取3個字節。
這就是為什么我沒有得到我的詮釋1。
注意:當客戶端使用DataInputStream時,readInt()可以正常工作...
使用DataInputStream
及其readInt()
方法。
DataInputStream dIn = new DataInputStream(new BufferedInputStream(myClientSocket.getInputStream()));
int status = dIn.readInt();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.