---
title: "TCP connection and graceful termination"
subject: "Computer Networks and Security"
chapter: 5
key: "semester_7/computer_networks_and_security:tcp-connection-termination"
source: https://www.bctengineer.com/q/semester_7/computer_networks_and_security/tcp-connection-termination
site: BCT Engineering
---

# TCP connection and graceful termination

Computer Networks and Security · Chapter 5: Transport Layer

- Asked in: 2072 Kartik, 2+6 marks
- Video: [Lec-71: TCP connection Establishment and connection Termination | Transport layer](https://www.youtube.com/watch?v=qIEHUUt2Wfc) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/computer_networks_and_security/tcp-connection-termination
- Whole chapter: https://www.bctengineer.com/md/semester_7/computer_networks_and_security/chapter-5

## Question

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

## 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.

## Answer

#### 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**.

```text
 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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is a TCP connection identified?
   A. By source IP only
   B. By source and destination port only
   C. By the pair of sockets (source IP, source port, destination IP, destination port)
   D. By the sequence number
   Answer: C. A connection is identified by the two sockets, i.e. both IP addresses and both ports. (Key point: TCP connection)

2. Why are four segments normally needed to terminate a connection gracefully?
   A. TCP is connectionless
   B. The connection is full duplex and each direction is closed separately
   C. Each segment carries a different checksum
   D. The router needs to be notified
   Answer: B. Each direction needs its own FIN and ACK. (Key point: Four-way handshake)

3. After the server ACKs the client's FIN, what can the server still do?
   A. Nothing; the connection is closed
   B. Send remaining data to the client
   C. Restart the handshake
   D. Send a SYN
   Answer: B. This is the half-close: client-to-server is closed but the server may still send data. (Key point: Half-close)

4. When can steps 2 and 3 be combined, and how is an abrupt close done?
   A. Combined when the server has no more data (FIN+ACK); abrupt close uses RST
   B. Combined when the client is in TIME_WAIT; abrupt close uses SYN
   C. Never combined; abrupt close uses FIN
   D. Combined always; abrupt close uses ACK
   Answer: A. With no more data the server sends FIN+ACK in one segment, giving three segments; RST aborts abruptly. (Key point: Combined and abrupt)

## Related in Computer Networks and Security

- [TCP connection establishment and release](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Atcp-establish-release) — Computer Networks and Security, chapter 5
- [TCP connection establishment](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Atcp-connection-establishment) — Computer Networks and Security, chapter 5

## Related in other subjects

- [Zero reflection coefficient; 3rd order LPF equal termination](https://www.bctengineer.com/question.md?key=semester_5%2Ffilter_design%3Areflection-zero-third-order-equal) — Filter Design, chapter 5, asked 4×
