Learning Python Network Programming

(Sean Pound) #1
Chapter 8

So, we'll use a push approach, and we will now write our chat protocol as follows:



  1. Communication will be conducted over TCP.

  2. The client will initiate a chat session by creating a socket connection to
    the server.

  3. The server will accept the connection, listen for any messages from the client,
    and accept them.

  4. The client will listen on the connection for any messages from the server,
    and accept them.

  5. The server will send any messages from the client to all the other
    connected clients.

  6. Messages will be encoded in the UTF-8 character set for transmission,
    and they will be terminated by the null byte.


Handling data on persistent connections


A new problem which our persistent connection approach raises is that we can no
longer assume that our socket.recv() call will contain data from only one message.
In our echo server, because of how we have defined the protocol, we know that as
soon as we see a null byte, the message that we have received is complete, and that
the sender won't be sending anything further. That is, everything we read in the last
socket.recv() call is a part of that message.


In our new setup, we'll be reusing the same connection to send an indefinite number
of messages, and these won't be synchronized with the chunks of data that we will
pull from each socket.recv(). Hence, it's quite possible that the data from one
recv() call will contain data from multiple messages. For example, if we send
the following:


caerphilly,
illchester,
brie

Then on the wire they will look like this:


caerphilly\0illchester\0brie\0

Due to the vagaries of network transmission though, a set of successive recv() calls
may receive them as:


recv 1: caerphil
recv 2: ly\0illches
recv 3: ter\0brie\0
Free download pdf