I’m trying to send files using sockets in Java. The problem is this: suppose there is a file of 97kb. It gets about 95.8kb and waits for more, but the writer has sent all 97kb.
The reading:
FileOutputStream fout = new FileOutputStream(fl);
int counter = 0;
byte[] byt = new byte[8192];
BufferedInputStream bin = new BufferedInputStream(cli.InputStream());
int count = 0;
while((count = bin.read(byt)) > 0)
{
counter = counter + count;
Log.d("TINTERACT", String.valueOf(count) + " _" + String.valueOf(counter) + " _" + String.valueOf(size));
fout.write(byt, 0, count);
}
fout.flush();
fout.close();
while writing is:
System.out.println("Starting writing");
FileInputStream fIn = new FileInputStream(path);
byte[] byt = new byte[8192];
BufferedInputStream bin = new BufferedInputStream(fIn);
BufferedOutputStream bout = new BufferedOutputStream(ser.OutputStream());
int count = 0, countr = 0;
while((count = bin.read(byt)) > 0)
{
System.out.println(count);
bout.write(byt, 0, count);
countr = countr + count;
}
bout.flush();
System.out.println("sent " + countr + "End");
bin.close();
writer complete sends the bytes total while reader donot get all bytes and loop wait for it