Kartik Gupta
Kartik Gupta

Reputation: 93

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

So I am trying to send a byte[] array to Server, I used the method DataOutputStream.write(byte[]), it was flushed but never reached the Server side. So i am trying to send one byte at a time to server and only starting few bytes reach there and the others are lost.

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

After this Nothing happens

Upvotes: 0

Views: 789

Answers (1)

user207421
user207421

Reputation: 310840

Don't mixed buffered and unbuffered streams on the same socket. You will lose data in the buffered one. You should send the length with DataOutputStream.writeInt() or writeLong(), and read it with readInt() or readLong().

Upvotes: 1

Related Questions