Learning python networking

Denis McMahon denismfmcmahon at gmail.com
Wed Jan 15 06:11:10 EST 2014


On Wed, 15 Jan 2014 02:37:05 -0800, Paul Pittlerson wrote:

>> One extremely critical point about your protocol. TCP is a stream - you
>> don't have message boundaries. You can't depend on one send() becoming
>> one recv() at the other end. It might happen to work when you do one
>> thing at a time on localhost, but it won't be reliable on the internet
>> or when there's more traffic. So you'll need to delimit messages; I
>> recommend you use one of two classic ways: either prefix it with a
>> length (so you know how many more bytes to receive), or terminate it
>> with a newline (which depends on there not being a newline in the
>> text).

> I don't understand. Can you show some examples of how to do this?

How much do you understand about tcp/ip networking? because when trying 
to build something on top of tcp/ip, it's a good idea to understand the 
basics of tcp/ip first.

A tcp/ip connection is just a pipe that you pour data (octets, more or 
less analagous to bytes or characters) into at one end, and it comes out 
at the other end.

For your stream of octets (bytes / characters) to have any meaning to a 
higher level program, then the applications using the pipe at both ends 
have to understand that a message has some structure.

The message structure might be to send an n character message length 
count (where in a simple protocol n would have to be a fixed number) 
followed by the specified number of characters.

Assuming your maximum message length is 9999 characters:

You could send the characters 9999 followed by 9999 characters of message 
content.

The receiving end would receive 4 characters, convert them to the number 
9999, and assume the next 9999 characters will be the message. Then it 
expects another 4 character number.

You could send json encoded strings, in which case each message might 
start with the "{" character and end with the "}" character, but you 
would have to allow for the fact that "}" can also occur within a json 
encoded string.

You might decide that each message is simply going to end with a specific 
character or character sequence.

Whatever you choose, you need some way for the receiving application to 
distinguish between individual messages in the stream of octets / bytes / 
characters that is coming out of the pipe.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list