Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

TCP Connection States
CN

TCP Connection States

Explore the TCP state machine: LISTEN, SYN-SENT, ESTABLISHED, TIME_WAIT and everything in between.

TCP connections transition through a well-defined set of states defined in RFC 793. Understanding these states is critical for debugging network issues (the netstat or ss command shows them).

The TCP State Machine

CLOSED

   │ (passive open)        (active open)
   ├───→ LISTEN ───────────→ SYN_SENT
   │        │                    │
   │        │ (recv SYN)         │ (recv SYN+ACK)
   │        ↓                    ↓
   │     SYN_RCVD ───────────→ ESTABLISHED
   │                               │
   │                          (active close)
   │                               │
   │                               ↓
   │                            FIN_WAIT_1
   │                               │
   │                    (recv ACK) │ (recv FIN+ACK)
   │                               │
   │                            FIN_WAIT_2 ←─── CLOSE_WAIT
   │                               │            │
   │                    (recv FIN) │            │ (send FIN)
   │                               ↓            ↓
   │                         TIME_WAIT ─────→ LAST_ACK
   │                               │            │
   │                         (timeout)     (recv ACK)
   │                               ↓            ↓
   └─────────────────────────── CLOSED ←───────┘

Key States

StateMeaningTypical ss output
LISTENServer waiting for incoming connectionss -tlnp shows listening sockets
SYN-SENTClient sent SYN, waiting for SYN-ACKBrief — usually not visible
SYN-RCVDServer received SYN, sent SYN-ACKHalf-open connections (SYN flood target)
ESTABLISHEDConnection active, data flowingss -tnp shows active connections
FIN-WAIT-1Client initiated close, waiting for ACKBrief
FIN-WAIT-2Client received ACK, waiting for server’s FINCan persist if server forgets to close
CLOSE-WAITServer received FIN, waiting for app to closeStuck here = app bug (leaked socket)
TIME-WAITClient sent final ACK, waiting 2×MSLSeen on busy clients (normal)
LAST-ACKServer sent FIN, waiting for final ACKBrief
CLOSEDConnection fully terminatedNot visible

Debugging Common States

  • Many CLOSE-WAIT → server application isn’t closing the socket properly (bug)
  • Many TIME-WAIT → normal on high-throughput clients, but can exhaust port range
  • Many SYN-RCVD → possible SYN flood attack

Q: What does CLOSE-WAIT indicate?

A: The server received a FIN from the client (initiating close) but the server application hasn’t called close() yet. Many CLOSE-WAIT connections suggest the server app has a socket leak — it’s not closing connections properly.

Q: What is TIME-WAIT and why is it needed?

A: After sending the final ACK, the client waits 2×MSL (about 2-4 minutes) in TIME-WAIT. This ensures: (1) the server received the final ACK (retransmits if needed), and (2) any delayed packets in the network expire before a new connection with the same 5-tuple.

Q: How to identify a SYN flood with state machine?

A: A large number of connections in SYN-RCVD state (half-open) indicates a SYN flood attack. The server has sent SYN-ACK but never received the final ACK. These connections consume resources until they time out.

Q: What states are most common in netstat?

A: ESTABLISHED (active connections), TIME-WAIT (completed connections waiting for cleanup), LISTEN (server ports), CLOSE-WAIT (potential application bug if numerous).

My Private Notes

Notes are auto-saved locally to this device.