Trace connection setup, byte ordering, and loss recovery in TCP.
Lesson goal
Trace the three-way handshake and predict what the application can read when data arrives out of order.
Before you begin — IP.
A server delivering a file needs the receiver to see the original bytes in order. TCP (Transmission Control Protocol) gives applications a reliable, ordered byte stream while the connection remains usable. IP can take care of routing each packet even if different packets experience different delays.
Before a typical TCP connection carries application data, the endpoints synchronize their starting sequence numbers for the two directions of travel.
The client typically waits one round trip for the SYN-ACK before sending its first application bytes. Reusing an established connection avoids paying that setup cost for another request. TCP specification, connection establishment
TCP can divide a block of application bytes into segments that travel in IP packets. A segment’s sequence number identifies the position of its first data byte in the stream. Segment boundaries may differ from the boundaries between an application’s writes.
Imagine four data segments sent in order. The receiver gets the first, third, and fourth. It can deliver the bytes from the first segment to the application and buffer later bytes. It has to wait to deliver the bytes beyond the gap because the application reads one ordered stream.
The numbers 1 through 4 in the figure label pieces of data for illustration. On the wire, TCP counts bytes. A cumulative acknowledgement (ACK) tells the sender the next byte the receiver expects in order. When the sender detects loss, it retransmits the missing data. Once the gap is filled, the receiver can deliver the newly contiguous bytes. If the connection breaks before recovery completes, the application must handle failure.
For a file transfer, losing a piece would leave the file incomplete. TCP’s setup, acknowledgements, and retransmissions support the delivery behavior the application needs. For rapidly changing game positions, a newly arrived snapshot may already replace the missing one. Holding the fresh snapshot behind older bytes can use up its useful lifetime.
HTTP/1.1, HTTP/2, email protocols such as SMTP, and SSH commonly use TCP. HTTP/3 uses QUIC, a transport that supplies reliable streams over UDP.
A receiver expects byte 201. Bytes 301 through 400 arrive while bytes 201 through 300 are missing. What can it deliver next?
It can buffer bytes 301 through 400. When bytes 201 through 300 arrive, the entire range 201 through 400 is contiguous and can be delivered in order.