0

In the following question are the bytes handled by the Buffered classes such that the File is implicitely cut up into smaller blocks or are the bytes sent all in one go?

how to achieve transfer file between client and server using java socket

Community
  • 1
  • 1
James P.
  • 19,313
  • 27
  • 97
  • 155

1 Answers1

3

The 'while' loops read and write in chunks.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Exact. The source has been changed since. Also, it's late and I've realized that my question makes no sense so please ignore :) . In short, the BufferedOutputStream sends the bytes all in one go on the sending side (nothing fancy) and the BufferedOutputStream is used to accumulate bytes on the receiving side with each read. – James P. Jan 14 '11 at 05:07
  • @James P: No. The BufferedOutputStream sends the bytes in chunks of its own buffer size, and the BufferedInputStream reads bytes in chunks of its own buffer size. – user207421 Jan 14 '11 at 06:37
  • So what is actually happening between the client and server (see the answer posted)? On the sending side there's `out.write(mybytearray, 0, mybytearray.length)` (`BufferedOutputStream`) and the receiving end there's `is.read(aByte)` (let's say _is_ is an `BufferedInputStream`). Is the `BufferedOutputStream` sending out data in chunks only as fast as the `InputStream` is reading them on the other side? I understand the idea of buffering more or less but I'd never actually thought about how these could interact. – James P. Jan 14 '11 at 06:56
  • 1
    @James P.: The TCP stack has a per-socket send buffer at the sender and a per-socket receive buffer at the receiver. The writes just write into the send buffer, blocking while it is full. TCP writes from that buffer to the network asynchronously. The reads just read from the read buffer, blocking while it is empty. If both buffers fill up it means that the reader isn't reading, which will cause the sender to block. All this is way below the level of Java. – user207421 Jan 15 '11 at 00:12
  • Thanks for the explanation EJP. It wasn't such a dumb question after all :) – James P. Feb 03 '11 at 17:03