4.3.19Computer Networks

TCP — header, connection (3-way handshake, 4-way termination)

2,218 words10 min readdifficulty · medium1 backlinks

WHY does TCP need a header + handshake at all?

WHAT TCP gives you: a stream of bytes that arrive in order, without loss, without duplication, with flow control and congestion control.

WHY it can't just send data: IP packets can be lost, reordered, or duplicated. To recover, the receiver must be able to say "I got bytes up to position X." That requires both sides to agree on where counting starts — the Initial Sequence Number (ISN). You cannot acknowledge byte #5 if you never agreed where byte #0 was. Hence a handshake must happen before any data.

HOW it numbers things: TCP numbers bytes, not packets. If the ISN is 1000 and you send 500 bytes, the next segment starts at sequence 1500.


The TCP Header

Figure — TCP — header, connection (3-way handshake, 4-way termination)

Key flags (the verbs of TCP)

  • SYN — "let's synchronize sequence numbers" (only at setup).
  • ACK — "the Ack field is valid; I'm acknowledging."
  • FIN — "I have no more data to send" (closes my direction).
  • RST — "reset / abort, something's wrong."
  • PSH — "push this to the app now, don't buffer."
  • URG — "urgent pointer is valid."

3-Way Handshake (connection setup)

Why ack = ISN + 1 and not ISN? The ACK number means "next byte I expect." SYN occupies one phantom sequence slot (so a lost SYN can itself be retransmitted reliably), so the next expected byte is x+1x+1.


4-Way Termination (connection teardown)


Common Mistakes (Steel-manned)


Active Recall

Recall Quick self-test (cover the answers!)
  • Minimum / maximum TCP header size? → 20 / 60 bytes
  • What does the ACK number field carry? → next byte expected
  • Why ack = ISN+1? → SYN consumes one sequence number
  • Why is teardown 4-way? → independent half-close per direction
  • Who enters TIME_WAIT, and for how long? → active closer, 2·MSL
Recall Feynman: explain to a 12-year-old

Two friends want to pass notes reliably. First they agree "I'll start counting my notes from 1000, you start from 5000" — that's a quick three-message hello so nobody loses track (handshake). Each note says "this is my note #N" and "I've read up to your note #M." When they're done, each says a separate "I'm finished talking" and waits for an "okay" — that's four goodbye messages, because either friend might still have one last thing to say after the other's already done.


Connections

  • IP — packet structure & routing (TCP rides inside IP packets)
  • UDP — connectionless transport (contrast: no handshake, no reliability)
  • Sliding Window & Flow Control (the Window field in action)
  • TCP Congestion Control (cwnd, slow start — uses seq/ack)
  • Sockets & Ports (source/dest port fields)
  • Reliable Data Transfer Principles (sequence numbers, retransmission)
What is the minimum and maximum size of a TCP header?
20 bytes minimum, 60 bytes maximum (Data Offset field = header length in 32-bit words, max 15×4).
What does the TCP Sequence Number field represent?
The byte-stream index of the first data byte in this segment.
What does the TCP Acknowledgment Number field represent?
The next byte the receiver expects (cumulative ACK).
Why is the ACK in a SYN-ACK equal to the client's ISN + 1?
Because the SYN flag consumes one sequence number, so the next expected byte is ISN+1.
List the three segments of the TCP 3-way handshake.
SYN (C→S), SYN+ACK (S→C), ACK (C→S).
Why does TCP setup need only 3 messages instead of 4?
The server's ACK of the client and the server's own SYN piggyback into one SYN+ACK segment.
List the four segments of TCP connection termination.
FIN (C→S), ACK (S→C), FIN (S→C), ACK (C→S).
Why is teardown 4-way but setup 3-way?
Teardown is asymmetric (half-close): each direction closes independently, and the receiver may still send data, so its FIN can't always piggyback the ACK.
What is TIME_WAIT and how long does it last?
State of the active closer after its final ACK; lasts 2×MSL to re-ACK lost FINs and let old duplicate packets expire.
Which side enters TIME_WAIT?
The side that sends the first FIN (active closer), regardless of client/server role.
Does TCP number packets or bytes?
Bytes — TCP is a byte-stream protocol.
What do the SYN, FIN, and RST flags mean?
SYN = synchronize ISNs (setup); FIN = no more data (close my direction); RST = abort/reset connection.
Why does TCP need both a sequence and an acknowledgment number in every segment?
TCP is full-duplex; each direction has its own byte stream, so each segment reports its own seq and acks the other stream.

Concept Map

packets lost/reordered

needs

requires agreeing ISN

SYN then SYN-ACK then ACK

full-duplex data

numbered by

confirmed by

carries

carries

SYN synchronizes

FIN closes direction

polite flush both ways

Unreliable IP layer

TCP reliable byte stream

TCP segment header 20-60 B

3-way handshake

Established connection

Ordered byte stream

Sequence number 32b

Ack number 32b next expected byte

Flags SYN ACK FIN RST PSH URG

Window 16b flow control

4-way termination

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, TCP ka kaam hai IP ke upar ek reliable, ordered byte-stream banana. IP packets kho sakte hain, ulte-seedhe aa sakte hain — to TCP har byte ko number deta hai (packet ko nahi, byte ko!). Lekin counting shuru kahan se ho? Isiliye data bhejne se pehle dono side apna ISN (Initial Sequence Number) ek dusre ko batate hain. Yehi hai 3-way handshake: SYN bheja (seq=1000), server ne SYN+ACK bheja (apna seq=5000, aur ack=1001 — kyunki SYN ek sequence number kha jata hai), phir client ne ACK kiya (ack=5001). Ab dono directions sync ho gayi, data flow shuru.

Connection band karne mein 4-way termination lagta hai, 3 nahi. Reason: setup mein server ka ACK aur uska SYN ek hi packet mein ghus jaate hain, lekin teardown mein har direction alag-alag band hoti hai (half-close). Client FIN bhejta hai "main done", server ACK karta hai — par server abhi bhi data bhej sakta hai! Jab server bhi done hota hai to apna FIN bhejta hai, client final ACK karta hai. Isiliye chaar messages: FIN, ACK, FIN, ACK.

Ek important point: jo side pehla FIN bhejta hai (active closer) wahi TIME_WAIT mein jaata hai, 2×MSL time tak. Kyun? Agar uska last ACK kho gaya to peer dobara FIN bhejega aur use re-ACK karna padega; aur purane duplicate packets bhi expire ho jayein taaki same port-pair safely reuse ho. Yaad rakho: ye role-based hai (client ya server jo bhi pehle close kare), machine-based nahi.

Exam tip: header ka minimum 20 bytes, maximum 60 bytes (Data Offset field 32-bit words mein header length deta hai, max 15×4=60). Aur ACK number ka matlab hamesha "next expected byte" hota hai — yeh confusion sabse zyada hoti hai, isko pakka kar lo.

Go deeper — visual, from zero

Test yourself — Computer Networks

Connections