[fpc-pascal] lNet and TCP packet length
Bernd
prof7bit at googlemail.com
Sat Feb 4 15:14:08 CET 2012
2012/2/4 Bernd <prof7bit at googlemail.com>:
> You need a robust way to determine how long your
> protocol message is, at which byte exactly it ends and the next
> message begins.
The simplest way would probably be if you prepend all your messages
with a length field and a checksum field to be sent as the very first
bytes of each of your custom protocol messages (also take care of
endianness and exactly specify this in your protocol description).
Then you can either read as many bytes are available on the stream (if
it is less than still needed) or read only as many bytes as are still
missing (if equal or more are available), leave the rest of the data
in the stream, process the message and trigger the read event again to
read what can only be the beginning of the next message.
You can also use lNet in blocking mode (which can sometimes make
things easier) simply by specifying a timeout value for the socket.
Then you can just have a thread looping and trying to read
indefinitely, counting received bytes and let it generate your own
events on completion of a message instead of using the lNet events
which fire on every TCP packet.
More information about the fpc-pascal
mailing list