[fpc-pascal] lNet and TCP packet length

Malcolm Poole malcolm at lingua-z.co.uk
Sat Feb 4 18:30:56 CET 2012


On 04/02/12 14:14, Bernd wrote:
> 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.
> ________________________________
Many thanks Bernd, for your very helpful reply. The messages already 
start with a header record, which includes the length, so I'll add a 
checksum field to that and work along the lines you suggest.



More information about the fpc-pascal mailing list