← 🚚 Transport Layer: UDP & TCP
Subtopic 2 of 4

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

  1. P1: SYN (seq = x, chosen at random). P2: SYN + ACK (seq = y, ack = x + 1). P3: ACK (ack = y + 1).
  2. SYN and FIN each consume one sequence number.
  3. Data: ack = last byte received + 1.
  4. 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 = 1000 client opens: picks its initial sequence number x. The SYN uses up one sequence number.
  • Server → Client: SYN + ACK, seq = 5000, ack = 1001 server picks y and acknowledges x + 1 (the next byte it expects).
  • Client → Server: ACK, seq = 1001, ack = 5001 connection established. This ACK may already carry data.
  • Client → Server: data, 500 bytes, seq = 1001 first data byte has sequence number x + 1.
  • Server → Client: ACK, ack = 1501 cumulative ACK: everything below x + 501 has arrived.
  • Client → Server: FIN, seq = 1501 client is done sending (FIN-WAIT-1). FIN also consumes one sequence number.
  • Server → Client: ACK, ack = 1502 client enters FIN-WAIT-2 and waits for the server's FIN.
  • Server → Client: FIN, seq = 5001 server is done too (LAST-ACK).
  • Client → Server: ACK, ack = 5002 client enters TIME-WAIT for 2 × MSL, then closes.

Check yourself

In the second packet of the TCP handshake

Remember: SYN, SYN+ACK, ACK. ack = seq + 1. ISNs random. FIN-WAIT-2 waits for the other FIN.
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.