Skip to main content

TCP connection and graceful termination

Short answer

A TCP connection is a full-duplex reliable virtual circuit identified by the socket pair. Graceful termination uses a four-way handshake: the active closer sends FIN, receives ACK; later the other side sends FIN, the closer ACKs, and enters TIME_WAIT for 2×MSL to handle lost ACKs and stale segments.

What is a TCP connection? Explain how a TCP connection can be gracefully terminated.

TCP connection

A TCP connection is a logical, full-duplex, reliable virtual circuit between two application processes. It is identified by the pair of sockets (source IP, source port, destination IP, destination port). It has three phases: establishment (3-way handshake), data transfer, and termination.

Graceful termination

Because the connection is full duplex, each direction is closed separately, which needs four segments. This is called a four-way handshake.

 Client (active close)             Server
   |--- FIN, seq=u ---------------->|  client: FIN_WAIT_1
   |<-- ACK, ack=u+1 ---------------|  client: FIN_WAIT_2
   |      (server may still send data: half-close)
   |<-- FIN, seq=v ----------------|  server: LAST_ACK
   |--- ACK, ack=v+1 -------------->|
 TIME_WAIT (2 x MSL)             CLOSED
 CLOSED

Steps

  1. The client application calls close(). TCP sends a FIN segment. State: FIN_WAIT_1.
  2. The server acknowledges the FIN (ACK u+1). The client enters FIN_WAIT_2. The server is in CLOSE_WAIT and can still send remaining data (half-close).
  3. When the server application has finished, it sends its own FIN. State: LAST_ACK.
  4. The client acknowledges it with an ACK and enters TIME_WAIT. It waits 2 × MSL (MSL = maximum segment lifetime, typically 30 s to 2 min, so the wait is 1 to 4 min) so that the last ACK, if lost, can be resent and delayed duplicate segments disappear. Then the connection is closed.

Steps 2 and 3 may be combined into one FIN+ACK segment if the server has no more data (three segments only). If an error occurs, TCP can terminate abruptly using an RST segment.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Networks and Security chapter 5 questions →