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...