MA-NET1 · Module 8
TCP — reliable byte streams
Topics: Sequence numbers · Cumulative ACKs · RTT estimation · Retransmission
Overview
Session 8 — TCP: reliable transfer basics
Mira is building the phone app for Skylark Forecast. Every time someone opens a radar map, the app asks a Northline Relay server for map tiles, and those bytes have to cross the BayLink access network and the SpineNet backbone, which may delay, reorder, or silently drop packets along the way. This session shows how TCP turns that unreliable journey into something an application can treat like a clean, ordered pipe of bytes.
You will learn what "connection-oriented, full-duplex" really means, how a byte stream gets chopped into segments (and why MSS and MTU are related but not the same), how sequence numbers and acknowledgment numbers count bytes rather than packets, why cumulative ACKs make the protocol forgiving, how the sender picks a timeout from measured round-trip times, and what duplicate ACKs reveal about a lost segment. You will finish by contrasting all of this with UDP, which makes none of these promises.
Plan: ideas → worked numerical stories → short drills.
Time: about 55–70 minutes if you redraw each timeline yourself before checking the written solution.
Concepts
Session 8 — TCP: reliable transfer basics
The one-paragraph story
The network underneath your program is best-effort: it will try to deliver each packet, but it promises nothing. A packet can be dropped when a router queue overflows, corrupted by a noisy link, delayed behind a burst of other traffic, or delivered after a packet that was sent later. Most applications cannot live with that — a weather tile with a hole in the middle is useless. TCP is the transport protocol that sits between the application and the network and rebuilds the missing guarantees: every byte arrives, exactly once, in the order it was sent. This session explains the machinery that makes that illusion work, using Mira's Skylark Forecast app talking to a Northline Relay server as the running scene.
Core ideas — how TCP builds a reliable byte pipe
1. A connection, in both directions at once
TCP is connection-oriented. Before any application data moves, the two endpoints (Mira's phone and the relay server) exchange a few control segments to agree that a conversation exists and to set up bookkeeping on both sides. The next session walks through that opening exchange; for now, just notice what it means: each endpoint keeps state — counters, buffers, timers — for this one conversation. The routers in the middle (BayLink's, SpineNet's) keep no such state. They see individual packets and forward them; they have no idea that a "connection" exists.
TCP is also full-duplex: data can flow phone → server and server → phone at the same time on the same connection. The phone might be uploading its location while the server is streaming back tiles. Each direction has its own sequence numbers, and each side's segments can carry acknowledgments for the other side's data at the same time (this is called piggybacking). TCP is strictly point-to-point, too: one sender, one receiver. It does not broadcast.
2. A stream of bytes, cut into segments
The application hands TCP a stream of bytes. TCP does not know or care where the application's "messages" begin and end; it only sees a long sequence of bytes. To send them, TCP collects bytes from a send buffer and wraps chunks of them in segments, each with a TCP header (at least 20 bytes) in front.
How big may the data part of a segment be? The cap is the MSS — Maximum Segment Size — and it counts only the application bytes in a segment, not the header. Where does MSS come from? From the MTU — Maximum Transmission Unit, the largest link-layer frame payload a hop can carry. An IP packet has to fit inside the MTU, and the IP packet contains an IP header plus the TCP header plus the data:
[ \text{MSS} ;\approx; \text{MTU} - \text{IP header} - \text{TCP header} ]
With a 20-byte IP header and a 20-byte TCP header, an MTU of 1,400 bytes gives an MSS of 1,360 bytes. The two numbers live at different layers: MTU is a property of a link, MSS is a transport-layer setting that the two endpoints agree on so that segments will not need to be chopped up in the network. If that feels like two names for the same thing, hold on to this: MTU is measured with headers included; MSS is measured without them.
3. Sequence numbers count bytes, not segments
This is the idea that trips up most beginners, so it gets its own heading. TCP numbers every byte in the stream. The sequence number of a segment is the stream position of the first byte carried in that segment.
Suppose the first byte of a transfer has stream number 5,200 and the MSS is 800. The first segment carries bytes 5,200 through 5,999, so its sequence number is 5,200. The second segment starts at the next unused byte, so its sequence number is 6,000. The third is 6,800. Notice the jumps are 800, the number of data bytes — not 1, as they would be if segments were numbered like pages in a book. If a segment carries only 300 bytes (the last one in a transfer, say), the next sequence number advances by 300.
(Real connections start from an initial sequence number that each side picks for itself, not from zero. For the stories in this course we just say where counting begins.)
4. Acknowledgment numbers say "the next byte I need"
The acknowledgment number (ACK number) in a segment travelling back to the sender states: "I have received every byte up to, but not including, this number; this is the next byte I expect." So after the receiver gets bytes 5,200–5,999 in order, it replies with ACK number 6,000. It does not say "I got segment 1." It names a position in the byte stream.
This design has a lovely consequence: acknowledgments are cumulative. An ACK of 6,800 simultaneously confirms everything below 6,800. If one ACK in a series is lost on the way back, a later, larger ACK quietly makes up for it — the sender never even notices the earlier loss. Cumulative acknowledgment is a big reason TCP copes with a flaky network without much fuss.
5. What the receiver does with out-of-order data
Packets can arrive out of order. Imagine segments starting at 6,000, 6,800 and 7,600 are sent, and the one at 6,800 is lost. The segment at 7,600 arrives. The receiver now holds a gap: it has everything below 6,800, it is missing 6,800 up to 7,599, and it has the data from 7,600 onward. Because ACKs are cumulative, the receiver can only truthfully say "next byte I need is 6,800" — it repeats that same ACK number. Many implementations keep the out-of-order segment buffered so it will not have to be sent again; the specification leaves that choice open, but the ACK number stays pinned at the first gap either way.
6. Duplicate ACKs: a gentle hint of loss
When the same ACK number arrives again and again, the repeats are called duplicate ACKs. Each one is a tiny message from the receiver: "something after the gap arrived, but the gap is still there." A single duplicate ACK is weak evidence — the network may have merely reordered two segments. Three duplicate ACKs in a row (so four copies of the same number in total) are strong evidence that one segment really was lost while its successors got through. TCP's reaction is fast retransmit: send the missing segment again at once, without waiting for the timer to expire. The result is a recovery in about one round trip instead of a long idle wait.
7. The timer: how long should the sender wait?
If the sender hears nothing for too long, it retransmits the oldest unacknowledged segment. But "too long" must be tuned. A timeout that is too short makes the sender resend segments that were merely slow, wasting capacity. A timeout that is too long leaves the connection idle after a real loss. The right answer depends on the current round-trip time (RTT), and RTT changes constantly as queues grow and shrink.
So TCP measures it. Whenever a segment that was sent once gets acknowledged, the elapsed time is a sample RTT. A single sample is noisy, so TCP smooths them with an exponentially weighted moving average:
[ \text{EstRTT}{\text{new}} = (1-\alpha),\text{EstRTT}{\text{old}} + \alpha,\text{Sample},\qquad \alpha = \tfrac{1}{8} ]
It also tracks how jumpy the samples are, the deviation:
[ \text{Dev}{\text{new}} = (1-\beta),\text{Dev}{\text{old}} + \beta,\lvert \text{Sample}-\text{EstRTT}_{\text{old}}\rvert,\qquad \beta = \tfrac{1}{4} ]
and then sets the timeout to the estimate plus a safety margin proportional to the jumpiness:
[ \text{Timeout} = \text{EstRTT} + 4\cdot \text{Dev}. ]
Read the logic in words: "expect the typical round trip, then allow four typical wobbles on top." When samples are steady, the margin shrinks; when they are erratic, it grows. (Implementations also double the timeout after each consecutive retransmission, and they skip RTT samples from segments that had to be resent, since those samples would be ambiguous.)
8. Putting the pieces together
Seen from above, TCP's reliability is a loop: send segments tagged with byte numbers; wait for ACK numbers that creep upward; if a gap persists (three duplicate ACKs) or the timer fires, resend the first missing piece; and keep sending new bytes in the meantime. The application never sees any of it — it simply writes bytes in and reads the same bytes out.
9. A short narrated walk-through
Here is the whole loop in one small story, with deliberately tiny numbers. Mira's phone asks for a tile, and the relay answers with three segments of 500 bytes, numbered from 3,000.
The relay sends the segment starting at 3,000, then 3,500, then 4,000. The phone receives the first one and replies "next I need 3,500." It receives the second and replies "next I need 4,000." The third arrives and the reply is "next I need 4,500." Each reply is larger than the one before, which is how the relay knows things are going well.
Now change the story: the middle segment vanishes. The phone gets 3,000, replies "3,500," then receives the segment starting at 4,000. That does not fill the hole, so the reply is "3,500" again. The relay sees the same number twice. With nothing more to go on, it cannot tell whether the middle segment is lost or merely late. If more segments follow and the phone keeps repeating "3,500," the evidence builds; if nothing else follows, the relay's timer eventually settles the matter and the segment is resent. Either way, the application on the phone receives bytes 3,000 onward without any hole, because TCP does not hand data upward until it is contiguous.
That last sentence deserves emphasis. TCP delivers to the application in order. Data that arrives early is held back in the receive buffer until the gap before it is filled. This is the cost of reliability: a single lost segment can make later, already-arrived bytes wait.
10. Why bytes, and why cumulative?
It is fair to wonder why the designers numbered bytes rather than segments. One reason is flexibility: TCP may repackage unacknowledged data into different segment boundaries when it retransmits. For instance, two small unacknowledged segments can be merged into one larger resend. Because the numbering lives in the byte stream, a receiver can still make perfect sense of the resend, even though the boundaries have moved. If segments had their own numbers, that repackaging would be awkward.
A second reason is economy. Acknowledging a position in the stream is a single small number, and one ACK can cover many segments. Receivers often delay an ACK slightly (a fraction of a second) so one reply can cover two arriving segments; this halves the ACK traffic with no loss of information.
Contrast: what UDP does instead
UDP is the other main transport protocol. It adds only the bare minimum on top of IP: port numbers to tell programs apart on one host, and an optional checksum. There is no connection setup, no sequence numbers, no acknowledgments, no timers, no retransmission. Each UDP datagram is independent; if one vanishes, nobody notices unless the application itself does something about it. The header is only 8 bytes, against TCP's 20 or more.
That sounds like a poor deal until you consider who wants it. A live radar "ping" that is refreshed every few seconds does not benefit from retransmitting a stale reading — the next reading will arrive soon anyway. A name lookup is a single small question and a single small answer; opening a connection first would double the delay. Choose TCP when every byte matters and order matters; choose UDP when freshness and low delay matter more than perfect delivery, or when the application wants to design its own recovery rules.
Common confusions
"Sequence number 6,000 means segment number 6,000." No. It means the first byte in that segment is byte 6,000 of the stream. The segment has no number of its own.
"The ACK number is the last byte received." The ACK number is the next byte expected. If the receiver has bytes up to 5,999, it sends 6,000. Off-by-one slips here ruin otherwise correct timelines.
"A duplicate ACK means the ACK itself was duplicated by the network." It is usually just the receiver reacting to another out-of-order arrival by repeating the number it still needs.
"MSS and MTU are interchangeable." MTU includes headers and belongs to a link; MSS excludes TCP and IP headers and belongs to the transport layer.
"TCP guarantees the data arrives quickly." It guarantees ordered, complete delivery — not speed. Loss makes the connection slower; it does not make it fail (unless the problem persists so long that the connection gives up).
Quick self-check before the worked examples
Try answering these in your head, in plain words, before moving on.
- If a segment has sequence number 4,000 and carries 250 bytes, what is the sequence number of the next in-order segment, and what ACK number does the receiver send after getting the first one?
- Why can a lost ACK sometimes cause no retransmission at all?
- Why is a larger deviation in RTT samples a reason to wait longer before timing out?
- What would a UDP-based app have to build for itself if it wanted reliable delivery?
If any answer is shaky, reread the matching numbered section above (3, 4, 7 and the UDP contrast, respectively). The answers to the first two are worked in the examples that follow.
If this felt dense
Read section 3 and section 4 together once more, then draw a number line from 5,200 to 7,000 and mark where each segment begins. Slide an imaginary finger along the line — the ACK number is simply where your finger currently rests: the first byte you still need. Everything else in this session is a variation on keeping that finger moving.
Examples
Session 8 — Worked examples
Example A — Cutting a byte stream into segments
Scene. Mira's Skylark Forecast app is about to upload a saved-places file of 5,600 bytes to a Northline Relay server. The path crosses a SpineNet tunnel whose MTU is 1,400 bytes. The first byte of the upload carries stream number 5,200.
Step 1 — find the MSS. Subtract the IP and TCP headers (20 bytes each, no options):
[ \text{MSS} = 1400 - 20 - 20 = 1360 \text{ bytes} ]
That is the most this path allows per segment. To keep the arithmetic friendly in the rest of this example, suppose the app's TCP stack was configured to use a smaller MSS of 800 bytes (stacks are free to choose less than the maximum).
Step 2 — how many segments?
[ 5600 \div 800 = 7 \text{ segments, all full} ]
Step 3 — sequence numbers. Each segment starts where the previous one ended:
| Segment | Sequence number | Bytes carried | Last byte | Receiver's ACK once it arrives | |---|---|---|---|---| | 1 | 5,200 | 800 | 5,999 | 6,000 | | 2 | 6,000 | 800 | 6,799 | 6,800 | | 3 | 6,800 | 800 | 7,599 | 7,600 | | 4 | 7,600 | 800 | 8,399 | 8,400 | | 5 | 8,400 | 800 | 9,199 | 9,200 | | 6 | 9,200 | 800 | 9,999 | 10,000 | | 7 | 10,000 | 800 | 10,799 | 10,800 |
Step 4 — interpret. After the last ACK, the receiver says "next I need 10,800", which equals the starting number plus the file size: (5200 + 5600 = 10800). That identity is a handy check on any timeline you draw: final ACK = first byte number + total bytes.
Notice also that the packets on the wire would each be 840 bytes (800 data + 40 headers), well under the 1,400 MTU, so nothing needs to be split further.
Example B — One segment lost, three duplicate ACKs
Scene. The server is streaming tile data back to Mira. The MSS is 600 bytes; the server's next byte to send is 2,400. It sends five segments back to back. SpineNet drops the second one.
The segments sent
| Segment | Seq | Bytes covered | Fate | |---|---|---|---| | S1 | 2,400 | 2,400–2,999 | arrives | | S2 | 3,000 | 3,000–3,599 | lost | | S3 | 3,600 | 3,600–4,199 | arrives | | S4 | 4,200 | 4,200–4,799 | arrives | | S5 | 4,800 | 4,800–5,399 | arrives |
What the phone replies, step by step
- S1 arrives in order. Everything up to 2,999 is in hand → ACK 3,000.
- S3 arrives. It starts at 3,600, but the phone still needs 3,000. The gap is unchanged → ACK 3,000 again (first duplicate). The phone keeps S3 in a buffer.
- S4 arrives → ACK 3,000 (second duplicate).
- S5 arrives → ACK 3,000 (third duplicate).
What the server does. It has now seen the number 3,000 four times (one original, three duplicates). Three duplicates are the trigger: it performs fast retransmit of the segment starting at byte 3,000 without waiting for its timer.
What happens next. The retransmitted segment (seq 3,000, 600 bytes) arrives. Now the phone's buffered data joins up: bytes 3,000–3,599 (new), 3,600–4,199, 4,200–4,799 and 4,800–5,399 all form a continuous run. The phone sends ACK 5,400 — one cumulative ACK that covers four segments at once.
Interpretation. Check the number: the last byte held is 5,399, so the next needed is 5,400 ✔. The loss cost about one round trip of delay, not a whole timeout. Also note that if the phone had discarded S3–S5 instead of buffering them, the sender would have had to resend them all; buffering saves bandwidth.
Example C — An acknowledgment gets lost (and nothing bad happens)
Scene. Mira's phone sends three small segments of 400 bytes each, starting at byte 9,000: seq 9,000, 9,400 and 9,800. All three reach the server. The server answers each in turn:
- after the first: ACK 9,400
- after the second: ACK 9,800
- after the third: ACK 10,200
Case 1 — the middle ACK is lost. The ACK 9,800 never reaches the phone, but ACK 9,400 and ACK 10,200 do. When ACK 10,200 arrives, the phone reads it as "everything below 10,200 has been received." That covers the second and third segments, so nothing is resent. A lost ACK was absorbed by the next one — that is the cumulative-ACK payoff.
Case 2 — every ACK is lost. The phone hears nothing. Its timer for the oldest unacknowledged segment (seq 9,000) expires, so it retransmits that segment. The server sees bytes 9,000–9,399, which it already has: a duplicate. It discards the duplicate data but still answers with the current ACK, which is 10,200 (it already holds everything through 10,199). That ACK arrives and clears all three segments.
Interpretation. In Case 2 the retransmission was unnecessary, because the data had arrived; only the ACKs had gone missing. TCP cannot tell the difference between "data lost" and "ACK lost" — it only knows that no acknowledgment came. The receiver's habit of re-acknowledging duplicates is exactly what lets the sender recover.
Example D — Updating the timeout from three RTT samples
Scene. The Skylark app keeps an estimate of the round trip to the Northline Relay server. Current values: EstRTT = 80 ms, Dev = 10 ms. Three new samples arrive in order: 96 ms, 72 ms, 120 ms. Use (\alpha = 1/8), (\beta = 1/4), and update Dev first (it uses the old estimate), then EstRTT.
Sample 1 = 96 ms
- Dev = 0.75 × 10 + 0.25 × |96 − 80| = 7.5 + 4 = 11.5 ms
- EstRTT = 0.875 × 80 + 0.125 × 96 = 70 + 12 = 82 ms
- Timeout = 82 + 4 × 11.5 = 82 + 46 = 128 ms
Sample 2 = 72 ms
- Dev = 0.75 × 11.5 + 0.25 × |72 − 82| = 8.625 + 2.5 = 11.125 ms
- EstRTT = 0.875 × 82 + 0.125 × 72 = 71.75 + 9 = 80.75 ms
- Timeout = 80.75 + 4 × 11.125 = 80.75 + 44.5 = 125.25 ms
Sample 3 = 120 ms
- Dev = 0.75 × 11.125 + 0.25 × |120 − 80.75| = 8.34375 + 9.8125 = 18.15625 ms
- EstRTT = 0.875 × 80.75 + 0.125 × 120 = 70.65625 + 15 = 85.65625 ms
- Timeout = 85.65625 + 4 × 18.15625 = 85.65625 + 72.625 ≈ 158.3 ms
Interpretation. The estimate itself barely moved (80 → about 86 ms) because one jumpy sample only gets a one-eighth vote. But the deviation nearly doubled (10 → about 18 ms), so the timeout grew from roughly 120 to 158 ms. TCP reacted to the unpredictability by waiting longer before declaring a loss. If the next samples are all near 80 ms, Dev will shrink again and the timeout will tighten back up.
Example E — Same tiny message over TCP and UDP
Scene. Skylark sends a 60-byte status reading from the phone to Northline Relay. We compare the cost of delivering it over UDP and over TCP, using minimum headers and a 20-byte IP header.
UDP. One datagram: 8-byte UDP header + 60 bytes data = 68 bytes at the transport layer; with IP, 88 bytes on the wire. Nothing else is sent. If it is lost, the phone never knows — the next reading, 10 seconds later, replaces it anyway.
TCP (connection already open). One segment: 20-byte TCP header + 60 bytes = 80 bytes; with IP, 100 bytes. The server then returns an ACK segment of 20 bytes of header, 40 bytes with IP (it may be piggybacked on a return data segment if the server has something to say). If the segment is lost, the phone retransmits after the timeout.
TCP (connection not yet open). Before the 100-byte segment can go out, a three-segment opening exchange must complete first, costing about one round trip of waiting.
Compare.
| | UDP | TCP, open | TCP, cold start | |---|---|---|---| | Bytes on wire for the reading | 88 | 100 (+40 for ACK) | 100 (+ opening segments + 40 ACK) | | Waiting before data leaves | none | none | ≈ 1 RTT | | Recovery from loss | none (app decides) | automatic | automatic | | Delivery order guaranteed | no | yes | yes |
Interpretation. For one stale-tolerant 60-byte reading every ten seconds, UDP is cheaper and quicker, and a lost reading is harmless. For the 5,600-byte saved-places file from Example A, a single missing byte would corrupt the file, so TCP's extra bytes and bookkeeping are worth it. The protocols are tools; the application's tolerance for loss decides which is the better fit.
Practice check — TCP reliable transfer basics
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.A TCP segment carries 450 bytes of data and has sequence number 12,300. What sequence number should the next segment in the same direction carry (assuming it follows immediately)?
2.A receiver has taken in every byte of the stream up to and including byte 7,899 and nothing beyond. Which acknowledgment number does it send?
3.A link has an MTU of 1,500 bytes. With a 20-byte IP header and a 20-byte TCP header (no options), what is the largest MSS that avoids splitting packets on that link?
4.The sender transmits segments starting at bytes 800, 1,200, 1,600 and 2,000 (400 bytes each). Only the segment at 1,200 is lost; the other three arrive in order of sending. What ACK numbers does the receiver send, in order of arrival?
5.Mira's phone sends three consecutive segments and the middle segment's ACK is lost, but the ACK for the third segment arrives. What does the sender conclude?
6.A connection has EstRTT = 100 ms and Dev = 15 ms. Using Timeout = EstRTT + 4 × Dev, what is the timeout?
7.Which application behaviour is the best fit for UDP rather than TCP?
8.Why does a TCP sender usually need three duplicate ACKs before it retransmits early, rather than reacting to the first one?