Re: Receiver in Outputstream.read() stops after 2735 bytes
On 1/4/2010 4:14 PM, Bart Friederichs wrote:
Knute Johnson wrote:
On 1/4/2010 12:57 PM, Bart Friederichs wrote:
Hi,
I have written a sender-receiver and the receiver stops receiving any
data after 2735 bytes. The sender seems to be fine, because when
connecting with a telnet session, it sends all the bytes.
I have tried to send the data in 100 byte pieces and flush() afterwards,
to no avail.
Am I missing someting?
Yes, showing us the code :-).
:) Obviously.
This is the sender:
socket.sock.getOutputStream().write(chunk);
socket is my own class, sock inside it is a java.net.Socket, chunk is a
byte[].
This is the receiver:
int bytesleft = length;
int bytesread = 0;
while (bytesleft> 0&& bytesread> -1) {
bytesread = socket.sock.getInputStream().read(theChunk, length -
bytesleft, 1);
bytesleft -= bytesread;
}
theChunk is a byte[] of size 'length'
... whose first `bytesread' elements will hold the
values from the *last* call to read(), if I'm not mistaken.
That is, if you try to read 1000 bytes and get them in two
chunks of 600 and 400 bytes each,
- The first read() deposits input bytes 0-599 in
theChunk[0] through theChunk[599],
- The next read() deposits input bytes 600-999 in
theChunk[0] through theChunk[399], wiping out
bytes 0-399,
- At the end, theChunk[] holds input bytes 600-999,
followed by input bytes 400-599, followed by zeroes
(or old garbage),
- And, as a special bonus, you have no way of knowing
how many bytes were received or where they were stored.
--
Eric Sosman
esosman@ieee-dot-org.invalid