Learning Python Network Programming

(Sean Pound) #1
Chapter 1

TCP

The Transmission Control Protocol is documented as RFC 761. As opposed to UDP,
TCP is a connection based protocol. In such a protocol, no data is sent until the server
and the client have performed an initial exchange of control packets. This exchange
is called a handshake. This establishes a connection, and from then on data can be
sent. Each data packet that is received is acknowledged by the receiving party, and
it does so by sending a packet called an ACK. As such, TCP always requires that the
packets include a source port number, because it depends on the continual two-way
exchange of messages.


From an application's point of view, the key difference between UDP and TCP is that
the application no longer sees the data in discrete chunks; the TCP connection presents
the data to the application as a continuous, seamless stream of bytes. This makes
things much simpler if we are sending messages that are larger than a typical packet,
however it means that we need to start thinking about framing our messages. While
with UDP, we can rely on its packetization to provide a means of doing this, with TCP
we must decide a mechanism for unambiguously determining where our messages
start and end. We'll see more about this in Chapter 8, Client and Server Applications.


TCP provides the following services:



  • In-order delivery

  • Receipt acknowledgment

  • Error detection

  • Flow and congestion control


Data sent through TCP is guaranteed to get delivered to the receiving application in
the order that it was sent in. The receiving TCP implementation buffers the received
packets on the receiving device and then waits until it can deliver them in the correct
order before passing them to the application.


Because the data packets are acknowledged, sending applications can be sure that
the data is arriving and that it is okay to continue sending the data. If an ACK is not
received for a sent packet, then within a set time period the packet will be resent.
If there's still no response, then TCP will keep resending the packet at increasing
intervals, until a second, longer timeout period expires. At this point, it will give up
and notify the calling application that it has encountered a problem.


The TCP header includes a checksum of the header data and the payload.
This allows the receiver to verify whether a packet's contents have been modified
during the transmission.

Free download pdf