Chapter 3 ■ tCp
55
Data is actually sent and received with send() and recv(). Some protocols running on top of TCP will mark up
their data so that clients and servers know automatically when a communication is complete. Other protocols will
treat the TCP socket as a true stream and send and receive until end-of-file is reached. The shutdown() socket method
can be used to produce end-of-file in one direction on a socket (all sockets are bidirectional by nature) while leaving
the other direction open.
Deadlock can occur if two peers are written such that the socket fills with more and more data that never gets
read. Eventually, one direction will no longer be able to send() and might hang forever waiting for the backlog
to clear.
If you want to pass a socket to a Python routine that knows how to read to or write from a normal file object, the
makefile() socket method will give you a Python object that calls recv() and send() behind the scenes when the
caller needs to read and write.