At Tuesday 16/1/2007 20:05, David Hirschfield wrote:

I'm implementing a relatively simple inter-application communication
system that uses asyncore/asynchat to send messages back and forth.

The messages are prefixed by a length value and terminator string, to
signal that a message is incoming, and an integer value specifying the
size of the message, followed by the message data.

My question is: how can I produce a short terminator string that won't
show up (or has an extremely small chance of showing up) in the binary
data that I send as messages?

If you send previously the size of data to follow, there is no need for a terminator. On the receiving side, just read so many bytes; it's a lot easier. Anyway you can add a terminator, and check for it after the data arrives, just a small consistency test.

If you can't do that for whatever reason, usually it's done backwards: ensure the chosen (fixed) terminator never happens inside the message.
Example: encoding using || as terminator, % as quote character:
- any | inside the message is prefixed by the quote character: | -> %|
- any % inside the message is doubled: % -> %%
This way, any terminator inside the message would be converted to %|%| and could never be confused.
At the receiving side, decode as follows:
- search for any % character; when found, delete it and keep the following character; keep searching until no more % are found.



--
Gabriel Genellina
Softlab SRL

        

        
                
__________________________________________________ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to