Re: non blocking sockets and alternatives
- From: "Matt Humphrey" <matth@xxxxxxxx>
- Date: Fri, 20 Mar 2009 07:59:44 -0400
"EJP" <esmond.not.pitt@xxxxxxxxxxxxxxx> wrote in message
it seems Java structures the stream
between client and server by doing the following:
size_of_data + the_data
Nope. It just sends the data you sent. Sometimes *applications* prepend a
size word to a transaction, so you know when you've read it all.
A mistake TCP novices frequently make is to think that the data blocks they
write are preserved as blocks to be read. Unfortunately, small byte arrays
on loopback or local LAN will reinforce this illusion. In reality, however,
the stream truly has no boundaries between what is written and what is read.
The network is free to concatenate and break up the stream as it sees fit,
so long as it preserves the underlying byte sequence. The main techniques
for identifying application-specific chunks are:
Send a fixed-length byte count and then the data
Use a particular byte (e.g. 0, \r, \r\n, etc) as a terminator
Use fixed-length messages.
Use one message per connection
When reading, assume the data will arrive in fragments and readFully until
you have it all.
Matt Humphrey http://www.iviz.com/