7.2TCP segment, handshake and teardown
The idea
TCP opens a connection with a three-way handshake and closes it with FINs. The sequence number counts bytes; the ack number is the next byte expected.
Think of it like this: Phone etiquette: "Can you hear me?" "Yes, can you hear me?" "Yes." Then talking, and a polite goodbye in both directions.
Why it exists
GATE asks which flags are set in each packet, relations between seq and ack, and which state a side waits in.
How it works
- P1: SYN (seq = x, chosen at random). P2: SYN + ACK (seq = y, ack = x + 1). P3: ACK (ack = y + 1).
- SYN and FIN each consume one sequence number.
- Data: ack = last byte received + 1.
- Close: active side FIN-WAIT-1, then FIN-WAIT-2 after the ack, then TIME-WAIT (2 × MSL).
Worked example: After the client sends FIN and the server ACKs it, which state does the client wait in for the server's FIN?
FIN-WAIT-2 (GATE 2017).
Now play with it
- →Step through the table with x = 1000 and y = 5000.
- →Change x and see the acks shift by 1 and by the data size.
- Client → Server:
SYN, seq = 1000client opens: picks its initial sequence number x. The SYN uses up one sequence number. - Server → Client:
SYN + ACK, seq = 5000, ack = 1001server picks y and acknowledges x + 1 (the next byte it expects). - Client → Server:
ACK, seq = 1001, ack = 5001connection established. This ACK may already carry data. - Client → Server:
data, 500 bytes, seq = 1001first data byte has sequence number x + 1. - Server → Client:
ACK, ack = 1501cumulative ACK: everything below x + 501 has arrived. - Client → Server:
FIN, seq = 1501client is done sending (FIN-WAIT-1). FIN also consumes one sequence number. - Server → Client:
ACK, ack = 1502client enters FIN-WAIT-2 and waits for the server's FIN. - Server → Client:
FIN, seq = 5001server is done too (LAST-ACK). - Client → Server:
ACK, ack = 5002client enters TIME-WAIT for 2 × MSL, then closes.
Check yourself
In the second packet of the TCP handshake
Exam notes (the dense version)
TCP is reliable, connection-oriented and byte-stream based. Header 20 to 60 bytes: ports, 32-bit sequence number (index of the first data byte), 32-bit acknowledgement number (next byte expected), flags (SYN, ACK, FIN, RST), receiver window (flow control), checksum. 3-way handshake: SYN (seq = x) → SYN+ACK (seq = y, ack = x + 1) → ACK (ack = y + 1). Initial sequence numbers are chosen randomly. SYN and FIN each consume one sequence number. Teardown: FIN, ACK, FIN, ACK, with the active closer going through FIN-WAIT-1, FIN-WAIT-2 and then TIME-WAIT (2 × MSL). A segment can carry data and an ack together (piggybacking).
Previous year questions: Transport Layer: UDP & TCP (11)
PYQs are grouped by topic, so this is the full set for Transport Layer: UDP & TCP; try the ones that match this subtopic. Open all 11 on the topic page.