On Feb 26, 2009, at 5:02 PM, Thomas Tempelmann wrote:
me.write "TEST MESSAGE!"
me.write "SECOND MESSAGE!"
In a single firing on the dataavailable event on the receiving
socket, you
might get:
TEST MESSAGE!SECOND MESSAGE!
or you might get
TEST MESS on the first firing of dataavailable
and AGE!SECOND MESS on the second firing
Right. That's why I've implement the following format:
I always send a header of 8 bytes first, 4 bytes being a code for the
type of data to follow, and 4 bytes being the amoung of data to follow
for that type. then comes the actual data ("payload").
For reading, I use a state variable telling me how much of the payload
I am expecting. At first it's zero. That means that I'm waiting for
the header.
Thus, while waiting for the header, I check if at least 8 bytes are
available. If not, I return. Otherwise I read those 8 bytes, set the
memblock's LittleEndian to FALSE (also when creating the header!) and
read the type code and payload length. I store the payload len.
Next, I check if the expected payload len is non-zero. Then I check if
the socket has that much data available yet. If not, I return. If the
data is there, I read only that amount from the socket that the
payload length designates.
After processing this block, I loop back to checking for another
header.
That's a pretty safe and extensible algo, I think.
The format seems to work for EasyTCPSocket :).
Charles Yeomans
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|