簡體   English   中英

在Java Socket中,DataInputStream不能完全讀取數據

[英]In Java Socket,DataInputStream doesn't read the Data completely

因此,我嘗試向服務器發送一個byte []數組,我使用了DataOutputStream.write(byte [])方法,該方法已刷新但從未到達服務器端。 因此,我嘗試一次向服務器發送一個字節,並且僅開始幾個字節到達那里,而其他字節丟失。

Client Side code(ANDROID)

int len=databyte.length;
pr.println(len);
pr.flush();
setPriority(Thread.MAX_PRIORITY);
OutputStream out=soc.getOutputStream();
DataOutputStream data=new DataOutputStream(out);

for(int k=0;k<len;k++)
{
data.write(databyte[k]);

}
data.flush();
Log.d("tag", "LEN ;" +databyte.length); //In this case the Length was 1496

Server Side code
int len=Integer.parseInt(first.reader_home.readLine());
InputStream in=first.home_socket.getInputStream();
DataInputStream data=new DataInputStream(in);
System.out.println(len);
img=new byte[len]; //1496

for(int l=0;l<len;l++)
{
img[l]=data.readByte();
System.out.println("1 bit read"+l);
}
System.out.println("READ DATA");


console
1496
1 bit read0
1 bit read1
1 bit read2
1 bit read3
1 bit read4
.
.
1 bit read51
1 bit read52

之后什么都沒有發生

不要在同一套接字上混合緩沖流和非緩沖流。 您將丟失緩沖的數據。 您應該使用DataOutputStream.writeInt()writeLong(),發送長度writeLong(),並使用readInt()readLong().讀取長度readLong().

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM