TCP Connection Management
Lesson 4 opened the connection with a handshake. This lesson is about the rest of the story: how the two ends keep track of every byte with sequence and acknowledgment numbers, what happens when a segment is lost, how the connection is closed politely with FIN, how it is torn down in the middle with RST, and why the closing side lingers in TIME_WAIT before letting go.
Tracking the conversation
TCP is a byte-stream protocol. The application hands TCP an unstructured stream of bytes, and TCP slices it into segments. The receiver must be able to put those segments back in order and to notice a gap if one is missing. It does this with two numbers carried in every segment (both are in the TCP header, which Lesson 6 dissects):
- Sequence number — the position, in the sender’s byte stream, of the first data byte carried in this segment. If a segment carries 100 bytes starting at position 1, its sequence number is 1.
- Acknowledgment number — the position of the next byte the receiver expects. Because it points at the first byte not yet received, it acknowledges everything before it. If the receiver has bytes 1 through 100, it acknowledges 101.
Both ends pick a random initial sequence number (ISN) at connection setup so that old, delayed segments from a previous connection cannot be mistaken for new data. Two segments consume one sequence number even though they carry no data: SYN and FIN. That is why the first data byte after a handshake where the client’s ISN was 1001 is 1002, not 1001.
A worked example: bytes 1–100
For clarity, number the sender’s application bytes 1, 2, 3, … . The sender builds a segment with 100 bytes of payload and sends it:
! Sender sends the first 100 bytes of the stream segment: SEQ = 1 len = 100 (carries bytes 1 .. 100) ! Receiver has bytes 1..100 in order, so it expects byte 101 next ACK: ACK = 101 (next byte expected = 101)
If the sender then sends another 100 bytes, its sequence number is 101 and the receiver answers 201. The pair of numbers advances together, one for what was sent and one for what was received. The receiver can also piggyback its own data onto an acknowledgment: a segment can carry both an ACK field and a payload, so the two directions share the overhead.
Notice the receiver does not need to acknowledge each byte individually, nor even each segment. Real implementations often wait a short time (delayed ACK) or acknowledge every second segment, and one ACK can cover several segments at once. This is the efficiency of cumulative acknowledgment.
Loss, timeout, and fast retransmit
IP can lose, duplicate, or reorder segments. TCP defends against loss with a retransmission timer:
- The sender transmits a segment and starts a timer.
- The receiver’s ACK arrives before the timer expires → the timer is cancelled and the next segment is sent.
- No ACK arrives before the timer expires → the sender assumes the segment (or its ACK) was lost and retransmits the same data with the same sequence number.
The timer value is adapted, not fixed. TCP measures the round-trip time (RTT) of live segments and computes a retransmission timeout (RTO) that tracks it, with extra margin. On a slow link the RTO grows; on a fast one it shrinks. After each failed retransmission the sender generally waits longer (an exponential back-off): 1 s, 2 s, 4 s, 8 s, and so on, up to a limit, before giving up if the application has not allowed TCP to keep trying.
Duplicate ACKs and fast retransmit
Waiting for a timer is slow — the sender may sit idle for a full RTO. TCP has a faster signal. Suppose the receiver gets bytes 1–100, then bytes 201–300 (segment 101–200 was lost). For every out-of-order segment that arrives, the receiver sends the same acknowledgment it already sent: ACK = 101. A repeated acknowledgment that names data the receiver still lacks is a duplicate ACK.
When the sender sees a small number of duplicate ACKs (the classic threshold is three) all acknowledging the same byte, it concludes that a single segment was lost rather than that the network is congested or reordered, and it retransmits immediately without waiting for the timer. This is fast retransmit. (The closely related fast recovery mechanism then reduces the send rate rather than restarting from scratch.)
Connection termination: the FIN exchange
A TCP connection is full-duplex: data flows both ways, and each direction is an independent stream. Closing is therefore one direction at a time, not a single event. To close its half, an endpoint sends a segment with the FIN flag set, meaning “I have no more data to send.”
Because FIN is one-way, a complete graceful close takes four messages, often called the four-way teardown. The party that sends the first FIN is the active closer; the other is the passive closer.
Active closer (client) Passive closer (server)
| |
| 1. FIN, SEQ = x [no more data] | server may still send
|------------------------------------------------>|
| 2. ACK, ACK = x+1 | its own outstanding data
|<------------------------------------------------|
| | ... server finishes ...
| 3. FIN, SEQ = y |
|<------------------------------------------------|
| 4. ACK, ACK = y+1 |
|------------------------------------------------>| both halves now closed
Read the phase numbers carefully. Phase 1 is the client’s FIN; phase 2 is the server’s ACK of that FIN. The client has stopped sending, but the server may not be done — it can keep sending data to the client, which is why the server votes separately. When the server is finished it sends its own FIN (phase 3), and the client acknowledges it (phase 4). FIN in each direction consumes one sequence number, so the acknowledgment is always one greater than the FIN’s sequence number.
The four phases of teardown
| Phase | Message | Meaning |
|---|---|---|
| 1 | Active closer sends FIN | “I will send no more data.” The send half is closing. |
| 2 | Passive closer sends ACK | “I have received your FIN.” Its receive half is closing. |
| 3 | Passive closer sends FIN | “I too am finished sending.” Now the second half closes. |
| 4 | Active closer sends ACK | “I have received your FIN.” The connection is fully closed. |
TIME_WAIT and abnormal termination with RST
After sending the final ACK (phase 4), the active closer does not close immediately. It enters a state called TIME_WAIT and holds the connection open for a set period, conventionally twice the maximum segment lifetime (2×MSL) — on the order of one to four minutes in real systems. Two problems are solved by waiting:
- Protecting the final ACK. If that last ACK is lost, the passive closer retransmits its FIN. If the active closer had already closed and forgotten the connection, the unexpected FIN would be answered with a RST, producing an unclean close. Staying in TIME_WAIT lets the active closer re-acknowledge the FIN properly.
- Letting old segments die. Segments from this connection may still be wandering the network. If the same four-tuple (source IP, source port, destination IP, destination port) were reused immediately, a stale segment could be accepted as part of the new connection. Waiting longer than the MSL lets those segments expire first.
You will see TIME_WAIT constantly when you run netstat on a busy web server: the server-side sockets linger after each short client connection. This is normal and desirable; operators tune the timeout only when a huge connection rate (for example behind a load balancer) exhausts the ephemeral port range.
! Illustrative only - exact columns vary by OS ("netstat -an" on Windows)
Proto Local Address Foreign Address State
TCP 127.0.0.1:52413 127.0.0.1:80 TIME_WAIT
TCP 127.0.0.1:52414 127.0.0.1:80 ESTABLISHED
TCP 127.0.0.1:80 127.0.0.1:52414 ESTABLISHED
Abnormal termination with RST
Not every close is graceful. A segment with the RST (reset) flag set tears the connection down immediately and without negotiation. RST is the TCP equivalent of slamming the phone down; no FIN, no TIME_WAIT, no courtesy. Common causes:
- Connecting to a closed port. The host has no listener on that port, so it answers the SYN with SYN + RST (or RST/ACK) to say “no service here.”
- An application crash or forced close. The operating system resets the socket, often discarding unread data.
- Data arriving for a connection that no longer exists — for example after a reboot, or when a firewall drops state.
- Policy and security tools deliberately injecting RST to cut a session.
When you see Connection reset by peer, the far end has sent a RST. When a tool reports Connection refused, the far end answered your SYN with RST because nothing was listening. A silent failure — a SYN that gets no answer at all — usually means a firewall dropped the packet rather than rejecting it.
Open, half-open, and the session life cycle
Put the events of Lessons 4 and 5 into one life cycle. Along the way, two security-relevant terms appear:
- Open (established) connection — both endpoints agree the session exists and both have state for it. Data flows freely.
- Half-open connection — one endpoint believes the session is alive while the other has lost state (crashed, rebooted, or been reset). The half-open side keeps sending and eventually times out.
- Half-closed connection — a legitimate, temporary condition during teardown: one direction has sent FIN, the other is still sending data.
Putting it together
The sequence diagram below traces one complete session from the first SYN to the final ACK, with sequence numbers shown for a connection whose client ISN is 1001 and server ISN is 5001. Trace how every ACK equals the peer’s last sequence number plus the bytes it carried.
Client (10.0.0.10:52413) Server (203.0.113.5:80)
| |
| SEQ=1001 ACK=0 [SYN] |
|------------------------------------------------------>| 1 open
| SEQ=5001 ACK=1002 [SYN, ACK] |
|<------------------------------------------------------| 2
| SEQ=1002 ACK=5002 [ACK] |
|------------------------------------------------------>| 3 established
| |
| SEQ=1002 ACK=5002 len=100 (bytes 1..100) |
|------------------------------------------------------>|
| SEQ=5002 ACK=1102 [ACK] | receiver wants byte 101
|<------------------------------------------------------|
| | ... session continues ...
| SEQ=1102 ACK=5002 len=100 (bytes 101..200) |
|------------------------------------------------------>|
| ... segment lost in transit ...
| SEQ=1102 ACK=5002 len=100 (retransmission) | timer fires, resend
|------------------------------------------------------>|
| SEQ=5002 ACK=1302 [ACK] |
|<------------------------------------------------------|
| |
| SEQ=1302 ACK=5002 [FIN] | client done sending
|------------------------------------------------------>|
| SEQ=5002 ACK=1303 [ACK] | phase 2
|<------------------------------------------------------|
| SEQ=5002 ACK=1303 [FIN] | server done sending
|<------------------------------------------------------|
| SEQ=1303 ACK=5003 [ACK] | phase 4
|------------------------------------------------------>|
| (client enters TIME_WAIT, then CLOSED) |
This one picture contains almost everything the exam asks about TCP connection management: the handshake, cumulative acknowledgment, a timer-driven retransmission, and a four-message teardown with FIN consuming a sequence number.
Key terms
| Term | Meaning |
|---|---|
| Sequence number | The position in the sender’s byte stream of the first data byte in a segment. |
| Acknowledgment number | The next byte the receiver expects; acknowledges all earlier bytes cumulatively. |
| ISN | Initial sequence number; a random starting point chosen at connection setup. |
| RTO | Retransmission timeout; the adapted time a sender waits before resending unacknowledged data. |
| Duplicate ACK | A repeated acknowledgment naming data the receiver still lacks; three of them trigger fast retransmit. |
| FIN | Finish flag; closes one direction of a connection. Consumes one sequence number. |
| RST | Reset flag; aborts a connection immediately with no negotiation. |
| TIME_WAIT | The state the active closer holds for about 2×MSL to catch a retransmitted FIN and let old segments expire. |
| Half-open connection | One endpoint has state for the session while the other does not. |
| Half-closed connection | A normal teardown state: one direction has sent FIN, the other is still sending. |
Review
Check what you have learned with these review questions.
Sources & references
- Postel, J. RFC 793: Transmission Control Protocol (1981), Sections 2.6 (sequence numbers), 3.3–3.5 (retransmission, data communication), and 3.5 (Closing a Connection, TIME-WAIT). rfc-editor.org/rfc/rfc793.
- Braden, R. RFC 1122: Requirements for Internet Hosts — Communication Layers (1989), Sections 4.2.2 (retransmission), 4.2.2.1 (establishment), and 4.2.2.2 (closing). rfc-editor.org/rfc/rfc1122.
- Paxson, V., Allman, M., Chu, J. & Sargent, M. RFC 6298: Computing TCP's Retransmission Timer (2011). rfc-editor.org/rfc/rfc6298.
- Cisco. CCNA (200-301) Exam Topics, IP Services / TCP/IP transport. cisco.com.
Target exams: CCNA 200-301 v1.1 · CyberOps Associate 200-201. Last content verification: 2026-10-11.