'#', which does not appear in the data very often
What happens at point 4 now?
Typically, one uses start and end of packet markers, with some delimiter between the length and the data. If you receive"<xx:SomeDataGoesHere:Checksum><xx:MoreData:Checksum><xx:AnotherPacket:Checksum>"you can determine where a packet starts ("<"), how many bytes (xx) are to be in the packet, then you should see a known value (":"), then xx bytes, then another known value (":"), followed by a checksum, followed by another known value (">").If the :Checksum> part (known value, unknown value, known value) appears before xx bytes have been read, then you know data was lost. If the checksum is what is lost, you know that, too, because the known values are not in the correct location relative to the start of packet marker. Determining if it is the length byte that got lost is a bit tougher. If the valid packet length is always less than the ascii code for the delimiter, life is easier.Having a maximum packet length, and known start and end markers, one can locate the start and end of a packet in an otherwise unknown stream of data, with a relatively high degree of certainty.