Recall warm-up
- What does a TCP Acknowledgement Number A signify?
Stuck? Peek at 4.3.1 TCP segment header, byte-stream numbering & cumulative ACKs
- TCP Header: 20–60 B (`HLEN × 4`); 16b Src/Dst Ports, 32b Seq #, 32b Ack #, 6 Flags (`URG, ACK, PSH, RST, SYN, FIN`), 16b Window, 16b Checksum.
- Byte numbering: segment at `Seq = S` with L bytes covers `S .. S + L − 1`; receiver sends cumulative `ACK = S + L` (next expected byte).
- `MSS` = max TCP data payload per segment (`MTU − IP_hdr − TCP_hdr`).
- Why is a timeout timer needed when setting up or closing a connection over an unreliable network?
Stuck? Peek at 4.2.1 Stop-and-Wait ARQ, 1-bit sequence numbers & link utilisation
- Stop-and-Wait ARQ: 1 packet in flight; uses 1-bit sequence numbers (0, 1) to discard duplicates when ACKs are lost.
- Utilisation:
η = Tt / (Tt + 2·Tp) = 1 / (1 + 2a), wherea = Tp / Tt(with losses of prob p:(1 − p)/(1 + 2a)). - Throughput:
η × B = L / (Tt + 2·Tp).
- What are the three phases of any connection-oriented communication?
Stuck? Peek at 1.2.3 Virtual circuits and the full comparison
- Virtual circuit: setup chooses a path, switches store (in-link, in-VCI → out-link, out-VCI); packets carry short VCIs; no link capacity reserved.
- Connection-oriented: circuit, VC. Connectionless: datagram.
- Setup? reserved? per-packet routing? → circuit Y/Y/N · VC Y/N/N · datagram N/N/Y.
- Order kept by circuit and VC; datagrams may reorder.
The Hook
Before a client and server can exchange a single byte of data over TCP, they must agree on their starting 32-bit sequence numbers (ISNc and ISNs), their advertised window field values, and their Maximum Segment Sizes (MSS).
They do this with the famous TCP 3-way handshake. And there is one golden arithmetic rule you must never forget on GATE: every SYN segment and every FIN segment consumes 1 sequence number (as if it carried 1 phantom byte), whereas a pure ACK segment consumes 0 sequence numbers!
Predict First
Why does TCP need a 3-way handshake instead of a simple 2-way handshake (Client sends SYN → Server replies SYN+ACK and immediately considers the connection open)?
Suppose a client's old SYN got stuck in a slow router queue, the client gave up, and later that delayed duplicate SYN arrives at the server! In a 2-way handshake, the server would immediately open a "ghost connection" and waste resources waiting for data that never comes. With a 3-way handshake, the server waits for the client's 3rd step — and when the client sees a SYN+ACK for an old sequence number it didn't ask for, the client rejects it with RST!
Core Concept
The TCP 3-Way Handshake (Connection Setup)
Let the Server be in state LISTEN (passively waiting) and the Client start in CLOSED:
- Step 1 (
SYN, Client → Server): Client picksISNc = x, sendsSYN=1, Seq = x, and entersSYN-SENT. (ThisSYNconsumes sequence number x!) - Step 2 (
SYN + ACK, Server → Client): Server picksISNs = y, sendsSYN=1, ACK=1, Seq = y, Ack = x + 1, and entersSYN-RCVD. (ThisSYNconsumes sequence number y!) - Step 3 (
ACK, Client → Server): Client sendsSYN=0, ACK=1, Seq = x + 1, Ack = y + 1and entersESTABLISHED. When the Server receives it, the Server entersESTABLISHED! (If Step 3 carries 0 data bytes, it is a pure ACK and consumes 0 sequence numbers, so the client's first data segment ALSO starts atSeq = x + 1!)
4-Step Connection Termination & Half-Close
Because a TCP connection is full-duplex (two independent one-way pipes), each direction must be closed separately with a FIN segment (and each FIN consumes 1 sequence number!):
- Active close (Client sends
FIN=1, Seq = u): Client entersFIN-WAIT-1(can no longer send data, but can still receive data!). - Server sends
ACK = u + 1: Server entersCLOSE-WAIT; Client entersFIN-WAIT-2. This state is called half-close: the server can still send remaining application data to the client! - Passive close (Server sends
FIN=1, Seq = v): Server entersLAST-ACK. - Client sends final
ACK = v + 1: Client entersTIME-WAITfor2 × MSL(Maximum Segment Lifetime), thenCLOSED; Server entersCLOSEDupon receiving the ACK.
Why Does TIME-WAIT Last 2 × MSL? (And What is CLOSING?)
Two classic GATE questions about the TCP state diagram:
- Why does the active closer wait in
TIME-WAITfor2 × MSL?
Reason 1: In case the finalACK(Step 4) is lost! If it is lost, the server inLAST-ACKwill time out and retransmit itsFIN; the client must still be alive inTIME-WAITto resend theACK.
Reason 2: To allow all delayed duplicate segments from this connection incarnation to die out in the network before the same 4-tuple(SrcIP, SrcPort, DstIP, DstPort)is reused for a new connection! - When does the
CLOSINGstate occur? When both sides sendFINsimultaneously (simultaneous close): both moveESTABLISHED → FIN-WAIT-1 → CLOSING → TIME-WAIT.
| Sequence Number Consumption Rule | Consumes how many Seq numbers? |
|---|---|
| SYN flag set (SYN or SYN+ACK) | +1 sequence number (1 phantom byte) |
| FIN flag set (FIN or FIN+ACK) | +1 sequence number (1 phantom byte) |
| Application data of L bytes | +L sequence numbers |
| Pure ACK (SYN=0, FIN=0, L=0) | 0 sequence numbers! (Next segment uses the SAME Seq!) |
Putting it together
- TCP sets up connections with a 3-way handshake (SYN → SYN+ACK → ACK) to synchronise ISNs and reject old duplicate SYNs.
- SYN and FIN each consume 1 sequence number, whereas a pure ACK consumes 0 sequence numbers.
- TCP closes connections with a 4-step FIN/ACK exchange supporting half-close, leaving the active closer in TIME-WAIT for 2·MSL.
Visual
Worked Examples
Client A sends a SYN segment with Seq = 900 to Server B, and Server B replies with a SYN+ACK segment with Seq = 4000. What are the Ack field in B's SYN+ACK and the Seq and Ack fields in A's 3rd handshake segment?
Show the full solution
• B's SYN+ACK acknowledges A's SYN (900) → Ack = 901 (with Seq = 4000).
• A's 3rd handshake segment has Seq = 901 and acknowledges B's SYN (4000) → Ack = 4001.
Continuing from Example 1 (where the handshake finished at Client Seq = 901), the client sends a single 400-byte data segment (acknowledged by the server) and then sends a FIN segment (carrying no data) to close the connection. What is the sequence number of the client's FIN segment, and what ACK number does the server return for that FIN?
Show the full solution
• Client's 400-byte data segment uses bytes 901 .. 1300.
• Therefore, the client's FIN segment has Seq = 1301.
• Because FIN consumes 1 sequence number, the server acknowledges it with Ack = 1302!
In a TCP connection, the Maximum Segment Lifetime (MSL) is 60 seconds. Client C initiates an active close. How long (in seconds) does C remain in the TIME-WAIT state before entering CLOSED, and which side (Client C or Server S) enters TIME-WAIT?
Show the full solution
Only the peer that performs the active close (sends the first FIN — here Client C) enters TIME-WAIT, and it stays there for 2 × MSL = 2 × 60 = 120 seconds.
GATE Traps
- Pure ACK consumes ZERO sequence numbers! Never add +1 for the 3rd segment of the 3-way handshake if it carries no data!
- SYN and FIN consume 1 sequence number even though they carry 0 data bytes! Why? Because SYN and FIN must be acknowledged reliably (retransmitted if lost), so they need a sequence number!
Memory Hook
SYN = +1, FIN = +1, Pure ACK = 0; Active closer waits 2·MSL in TIME-WAIT!
Connect the Dots
The connection is now ESTABLISHED! Once data starts pouring across, how does a slow receiver stop a fast sender from overflowing its buffer? Next lesson (4.3.3): TCP flow control, sequence wrap-around time, and window scaling!
Self-Test
- NAT
Client A initiates a TCP connection to Server B with Initial Sequence Number ISN_A = 1500. After the 3-way handshake (where the 3rd segment carries no data), A sends 3000 bytes of application data and then sends a FIN segment. What is the ACK number sent by Server B in response to A’s FIN segment?
Write your answer before opening the key.
- MCQ
During the TCP 3-way handshake, Client C sends SYN (Seq = x) and receives SYN+ACK (Seq = y, Ack = x + 1) from Server S. What TCP state is Server S in immediately AFTER sending the SYN+ACK (before receiving the client’s final ACK)?
- LISTEN
- SYN-SENT
- SYN-RCVD
- ESTABLISHED
- MSQ
Which of the following statements about TCP connection management are TRUE?
- Both SYN and FIN segments consume 1 sequence number each.
- A pure ACK segment (carrying no data and with SYN=0, FIN=0) consumes 0 sequence numbers.
- After receiving a FIN and sending an ACK, the passive closer enters the CLOSE-WAIT state and can still send data to the active closer (half-close).
- A 2-way handshake is sufficient for TCP to prevent ghost connections from delayed duplicate SYN segments.
- NAT
If the Maximum Segment Lifetime (MSL) in a network is 30 seconds, for how many seconds does a TCP host remain in the TIME-WAIT state?
Write your answer before opening the key.
Rule Card
- 3-way handshake:
SYN(seq=x) → SYN+ACK(seq=y, ack=x+1) → ACK(seq=x+1, ack=y+1). - Seq consumption:
SYN = +1,FIN = +1,Pure ACK = 0,Data(L) = +L(full connection advances byD + 2). - Teardown states: Active closer goes
FIN-WAIT-1 → FIN-WAIT-2 → TIME-WAIT (2·MSL) → CLOSED; Passive closer goesCLOSE-WAIT (half-close) → LAST-ACK → CLOSED.
Concept Ledger
- 3-way handshake
- SYN and FIN consume 1 sequence number
- pure ACK consumes 0
- TCP states (CLOSED, LISTEN, SYN-SENT, SYN-RCVD, ESTABLISHED, FIN-WAIT-1/2, CLOSE-WAIT, LAST-ACK, TIME-WAIT = 2·MSL, CLOSING)
- half-close
- RST
Answer Vault
Try the questions first. Open when you are ready to check.
Warm-up answers
- That all bytes up to A − 1 have been received in order, and byte A is expected next.
- Because setup (SYN) or teardown (FIN) segments can be lost and must be retransmitted.
- Connection setup, data transfer, and connection teardown.
Self-test answers
- 4502 — ISN_A = 1500. SYN consumes 1 (→ 1501), 3000 bytes of data consume 3000 (→ 4501), and FIN consumes 1 (→ 4502). Thus B acknowledges the FIN with Ack = 4502.
- C — Upon receiving SYN in LISTEN and replying with SYN+ACK, the server transitions to SYN-RCVD (and moves to ESTABLISHED only after receiving the 3rd segment).
- A: LISTEN is before the SYN arrives.
- B: SYN-SENT is the client’s state after sending SYN.
- D: Server enters ESTABLISHED only after receiving the 3rd handshake segment.
- ABC — SYN and FIN consume 1 seq number each, pure ACKs consume 0, and CLOSE-WAIT allows half-close data transmission. A 2-way handshake fails when delayed duplicate SYNs arrive.
- D: A 2-way handshake cannot distinguish a delayed duplicate SYN from a fresh SYN; that is why TCP uses a 3-way handshake.
- 60 — TIME-WAIT duration is 2 × MSL = 2 × 30 = 60 seconds.
Flashcards
- How many sequence numbers do SYN, FIN, and a pure ACK each consume?
Reveal answer
SYN consumes 1; FIN consumes 1; a pure ACK (0 data bytes) consumes 0. - Give the two reasons why TIME-WAIT lasts 2 × MSL.
Reveal answer
1. To retransmit the final ACK if it was lost; 2. To let all old duplicate segments from this connection expire in the network. - What is a TCP half-close (CLOSE-WAIT / FIN-WAIT-2)?
Reveal answer
After one side sends FIN and gets an ACK, it can no longer send data, but the other side in CLOSE-WAIT can still send data back before sending its own FIN.
Exam shortcuts
- Total Seq Numbers Consumed by a Connection: If a host sends a
SYN, then D bytes of data, and then aFIN, its sequence number advances by exactlyD + 2(+1for SYN,+Dfor data,+1for FIN)! — solving full-connection sequence-number traces in 5 seconds. - Who enters TIME-WAIT? Always the side that sends the final ACK (which is the side that initiated the active close by sending the first
FIN). — state-transition MCQs.