0

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • can you please point out the mistake in my code. –  Jun 28 '13 at 13:35
  • ... you could try to use `cli.InputStream()` directly without `BufferedInputStream` – Selvin Jun 28 '13 at 14:18
  • Humor me for a minute - do you know for a fact, such as by examining the data, that the tail end of the file hasn't arrived? Just to rule out differences between K=1024 vs K=1000, or actual sizes vs disk blocks required, etc. – Chris Stratton Jun 28 '13 at 15:22
  • @Selvin He certainly could, but why? It won't make any difference. – user207421 Jun 28 '13 at 18:37
  • What about closing your writes? I see you call `bout.flush()` but never `bout.close()`. – VolatileDream Jun 28 '13 at 18:43
  • @jex i am not closing the stream after it , it will close on android application close so thats y i havent written code here –  Jun 28 '13 at 19:32
  • @shankan You're not making sense. You're wondering why your read loop never terminates, when it only terminates when the connection is closed, and you're not closing the connection. So what exactly is your question? – user207421 Jun 30 '13 at 09:53

2 Answers2

1

Your receiver loop that reads from the socket won't terminate until the sender closes his socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • yes it works , but i dnt want to close the streams as application is file manager and use may want more sockets sending files after this file send –  Jun 28 '13 at 19:33
  • 2
    So looping until EOS won't work, will it? You'll have to send the length of the file ahead of the file and loop only until you've read exactly that many bytes. – user207421 Jun 29 '13 at 01:13
  • please have a look on my solution below , and please explain me why its happening so without close sockets –  Jun 30 '13 at 07:27
-1

Try using:

while((count = bin.read(byt)) != -1)

instead of:

while((count = bin.read(byt))>0)
johnny
  • 7
  • 1
    for FSM sake where is the differences? if there is EOF bin.read(byt) will return -1 so both `(count = bin.read(byt))>0` and `(count = bin.read(byt)) != -1` will return false – Selvin Jun 28 '13 at 13:59
  • i have tried to put -1 , but still same problem. –  Jun 28 '13 at 14:05
  • @shankhan: The former isn’t true for `0`. – Ry- Jun 28 '13 at 15:06
  • 1
    @Selvin From the title we can assume that the InputStream ultimately comes form a network socket. Unlike with a file, you won't get eof on a socket if the connection remains unbroken, because as long as it is still connected the fact that there is no more data available now doesn't necessarily mean that there won't be any more available in the future. – Chris Stratton Jun 28 '13 at 15:14
  • @ChrisStratton ... your comment is even better explenation why this answer should be downvoted ... `(count = bin.read(byt)) != -1` will not be `false` until connection is closed(on socket stream) ... – Selvin Jun 28 '13 at 15:18
  • This can only make a difference if read() returns zero, which it only does if the specified buffer is zero length, which it isn't, it is 8192 bytes long. And of course if read returns zero you would hardly want to keep looping. -1. – user207421 Jun 28 '13 at 18:33