
Signature
And loving it,
-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)
>> There are however a number of things that your application can do
>> wrong. The commonest ones to watch out for are thinking that TCP will
[quoted text clipped - 7 lines]
> And what do you mean by not reading as much data as requested? When
> would this happen and why?
This is possibly not entirely correct but illustrates the issue nicely.
You write a bunch of 512 byte "packets".
TCP/IP sticks them all together and transmits when it deems fit - it may
send too-and-a-half of your "packets" in one message.
On the receiving side, when you read the first two times you get the 512
bytes you ask for. When you read the third time you might only get only
half of the 512 bytes you expect - you have to issue another read to
wait for the rest.
> Can you please explain what you mean by not respecting message
> boundaries? I am sending data in 512 byte "packets", are you saying
> that this won't be respected by TCP?
TCP gives you a data *stream*. So if you send 512 bytes and then
another 512 bytes, the only guarantee is that the 1024 bytes will
arrive in the correct order. There is nothing in the stream to
separate the two messages from each other, that's the responsibility
of your application, for example by terminating each message with a
unique character the application can recognize, or by prefixing each
message with its length.
> And what do you mean by not reading as much data as requested? When
> would this happen and why?
If you read the API documentation carefully, you'll see that all of
the methods for reading from a stream will only read "up to" the
requested amount.
Even though TCP provides a data stream, lower layers in the network
are packet based and your data will be sent in chunks that bear little
relation to the actual messages you sent. Sometimes two messages might
get sent in one IP packet, or a single message might span more than
one IP packet.
So an application that always requests 512 bytes might get 512 bytes
the first two times but only 436 bytes the third time, because the
remainder of the third message might still in transit, or needed to be
resent. The application needs to check how many bytes were actually
read, and read again until the desired number of bytes are received.
/gordon
--
Qu0ll - 06 Oct 2007 10:28 GMT
> TCP gives you a data *stream*. So if you send 512 bytes and then
> another 512 bytes, the only guarantee is that the 1024 bytes will
[quoted text clipped - 22 lines]
> resent. The application needs to check how many bytes were actually
> read, and read again until the desired number of bytes are received.
OK thanks to yourself and Thomas for the explanation. This explains why
sometimes not all data were being received by the applet. This had lead me
to conclude that TCP/Java/Sockets were unreliable but it appears to be fixed
now by re-reading until the full packet is received.

Signature
And loving it,
-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)
Roedy Green - 06 Oct 2007 11:25 GMT
>TCP gives you a data *stream*. So if you send 512 bytes and then
>another 512 bytes, the only guarantee is that the 1024 bytes will
[quoted text clipped - 3 lines]
>unique character the application can recognize, or by prefixing each
>message with its length.
If for some reason a message is garbled (by software or user error),
when you start reading, you won't necessarily pick up at the beginning
of the next message. You may pick up in the middle of the garbled
message, or part way through the next good one. If you want to be
able to recover, you need to reserve some magic start of message
pattern to scan for, that won't occur incidentally in data.
Socket communications generally require you to know the precise length
of things before you put them on the wire.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com