-1

I am writing files / large amount of bytes over socket.

But lets say I am writing bytes. I do this;

//Connection.data is a dataoutputstream
byte[] a = new byte[filelength];
//load file into the array
//write file
for (int i = 0; i < a.length; i++) {
Connection.data.writeByte(a[i]);                                              
}

To receive:

//dat is a datainputstream
byte[] byteA = new byte[bytestoread]
for (int i = 0; i < toread; i++) {
        byteA[i] = dat.readByte();
}

I do log the incoming data, and lets say if the file is 200000 bytes, it stops at around 199990 bytes etc. Basically, any size of the byte[], and it will stop at the last bytes, and time out. I will explain more if you dont understand. Thanks.

1 Answers1

3

When you have finished writing you need to call flush to ensure that the bytes are actually sent.

Connection.data.flush();

From the documentation:

Flushes this data output stream. This forces any buffered output bytes to be written out to the stream.

The flush method of DataOutputStream calls the flush method of its underlying output stream.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452