TCP (Transmission Control Protocol)
What it is for
UDP adds only port numbers on top of IP; retransmitting lost packets, reordering and limiting the sending rate are left to the application.
TCP does these three things in the transport layer: it retransmits lost data, reorders by sequence number and uses a window to control the sending rate. For this both ends keep state for every connection, the header grows from UDP's 8 bytes to at least 20 bytes, and a handshake taking 1.5 RTT happens before any data is sent.
The three-way handshake
The handshake makes each side's initial sequence number acknowledged by the other:
· A sends SYN with its initial sequence number -> B learns A's number;
· B replies SYN+ACK carrying its own number and acknowledging A's -> A learns B's number and knows its own arrived;
· A replies ACK -> B learns that its own number arrived too.
With only two exchanges B's number is never acknowledged; after three both numbers are acknowledged, so a fourth is not needed.
Common misconceptions
TCP does not guarantee delivery; it guarantees that the application is told when delivery fails. With the cable unplugged TCP cannot deliver either.
On a link without loss TCP is as fast as UDP. The difference appears under loss: TCP retransmits the lost data before delivering what follows, while UDP does not retransmit and keeps sending.
An ACK only means the peer's kernel accepted the data; the peer application may not have called read yet.