AdvancedComputer Networks · Lesson 9 of 15 · Updated ·
TCP Connection in Detail — Sequence and ACK Numbers, Connection Close, TIME_WAIT and the TCP Header
Lesson 4 introduced the three-way handshake. Interviewers and exam papers go further: "The client's ISN is 1000 and it sends 200 bytes — what ACK comes back?", "Why not a two-way handshake?", "What is TIME_WAIT for?" This lesson follows one connection from start to finish with real numbers.
1. Sequence numbers count bytes
TCP treats the data as a stream of bytes and numbers every byte. Two rules cover almost every question:
- A segment's sequence number = the number of its first data byte.
- An acknowledgement number = the number of the next byte expected. It means "I have everything before this".
Each side picks its own random starting number, the ISN (initial sequence number). Random ISNs make it hard for an attacker to guess numbers and inject fake segments, and they keep old segments from a previous connection from being mistaken for new ones.
SYN and FIN each use up one sequence number, even though they carry no data. That is why the reply to SYN = 1000 is ACK = 1001.
2. One connection, number by number
Client ISN = 1000, server ISN = 5000. The client sends a 200-byte request; the server replies with 300 bytes.
The same numbers, worked out by a few lines of Python:
def exchange(isn_client, isn_server, client_bytes, server_bytes):
c, s = isn_client, isn_server
print(f"C -> S SYN seq={c}")
print(f"S -> C SYN-ACK seq={s} ack={c + 1}") # SYN uses one number
c, s = c + 1, s + 1
print(f"C -> S ACK seq={c} ack={s}")
print(f"C -> S data seq={c} len={client_bytes}")
c += client_bytes
print(f"S -> C ACK ack={c}")
print(f"S -> C data seq={s} len={server_bytes}")
s += server_bytes
print(f"C -> S ACK ack={s}")
exchange(1000, 5000, 200, 300)
C -> S SYN seq=1000
S -> C SYN-ACK seq=5000 ack=1001
C -> S ACK seq=1001 ack=5001
C -> S data seq=1001 len=200
S -> C ACK ack=1201
S -> C data seq=5001 len=300
C -> S ACK ack=5301
Exam pattern. "A TCP segment has sequence number 2171 and carries 500 bytes. What is the sequence number of the next segment?" → 2171 + 500 = 2671, and the receiver's ACK for it is also 2671.
In practice, ACKs are often piggybacked on data going the other way, and TCP uses cumulative ACKs: ACK = 1201 confirms every byte up to 1200, not just one segment.
3. Why three steps, not two?
Both sides must announce their ISN and confirm the other's:
| Step | Client's ISN | Server's ISN |
|---|---|---|
| SYN | announced | — |
| SYN-ACK | confirmed | announced |
| ACK | — | confirmed |
With only two steps, the server would never learn whether its ISN reached the client. There is also a safety reason: a delayed duplicate SYN from an old connection could reach the server. With three steps, the client sees a SYN-ACK it did not expect and resets it, so no false connection opens.
4. Closing: four segments and half-close
TCP is full duplex — data flows both ways independently — so each direction is closed separately.
- Client: FIN — "I have no more data to send."
- Server: ACK. The connection is now half-closed: the server may still send data.
- Server: FIN when it is done too.
- Client: ACK. The client now waits in TIME_WAIT.
Steps 2 and 3 are often combined into one FIN-ACK segment, so you may see three segments in a packet capture.
5. What TIME_WAIT is for
The side that closes first waits 2 × MSL (maximum segment lifetime — how long a segment may survive in the network; the TCP standard suggested 2 minutes, and many systems use a shorter value such as 30 or 60 seconds). Two reasons:
- The final ACK may be lost. Then the other side resends its FIN, and the closer must still be around to ACK it again.
- Old duplicates must die out. If a new connection with the same four values (IPs and ports) started at once, a delayed segment from the old one could be accepted as new data.
A busy server that closes many connections itself can collect thousands of sockets in TIME_WAIT — a common real-world question. It is normal behaviour, not a bug.
6. The main TCP states
| State | Meaning |
|---|---|
| LISTEN | server waiting for a SYN |
| SYN_SENT | client sent SYN, waiting for SYN-ACK |
| SYN_RECEIVED | server got SYN, sent SYN-ACK |
| ESTABLISHED | data can flow |
| FIN_WAIT_1 / FIN_WAIT_2 | closer sent FIN / got its ACK |
| CLOSE_WAIT | other side closed; this side may still send |
| LAST_ACK | sent our FIN, waiting for its ACK |
| TIME_WAIT | waiting 2 × MSL before forgetting the connection |
netstat -an (Windows and Linux) or ss -tan (Linux) lists your machine's connections with these states.
7. The TCP header in one table
The header is 20 bytes without options (up to 60 with options).
| Field | Bits | Purpose |
|---|---|---|
| Source port, destination port | 16 + 16 | which applications |
| Sequence number | 32 | first byte in this segment |
| Acknowledgement number | 32 | next byte expected |
| Header length | 4 | in 32-bit words (5 = 20 bytes) |
| Flags | — | SYN, ACK, FIN, RST, PSH, URG |
| Window size | 16 | how many bytes the receiver can accept (flow control, lesson 10) |
| Checksum | 16 | detects corruption (lesson 15) |
| Urgent pointer | 16 | rarely used |
Because the sequence number has 32 bits, it wraps around after 2³² bytes (4 GB). At high speeds this can happen within seconds, so TCP adds a timestamp option to tell old and new bytes apart.
RST (reset) aborts a connection at once — for example, when a SYN arrives at a port where no program is listening.
8. SYN flood
An attacker sends a huge number of SYNs (often from fake addresses) and never completes the handshake. Each one makes the server keep a half-open entry in SYN_RECEIVED, until its queue is full and real users cannot connect.
Defences: SYN cookies (the server encodes the connection details inside its own ISN and stores nothing until the final ACK proves the client is real), shorter timeouts, and filtering at firewalls or a provider's network.
Common mistakes
- Saying the ACK number is the last byte received — it is the next byte expected.
- Forgetting that SYN and FIN consume one sequence number each.
- Thinking the server enters TIME_WAIT — the side that closes first does.
- Treating TIME_WAIT sockets as a leak.
- Saying a TCP header is always 20 bytes — options can make it up to 60.
Quick recap
- seq = first byte of the segment; ack = next byte expected; SYN and FIN count as one byte.
- Three-way handshake: both ISNs announced and confirmed; old duplicate SYNs are rejected.
- Close: FIN, ACK, FIN, ACK; each direction closes separately (half-close).
- TIME_WAIT = 2 × MSL on the closing side: resend a lost final ACK, let old segments expire.
- SYN flood fills the half-open queue; SYN cookies defend against it.