SkillPilotAI

TCP: 3-way handshake, flags and states

TCP sets up a connection with a three-way handshake before any data moves, and tears it down with FIN (politely) or RST (abruptly). Knowing the flags and the connection states lets you read tcpdump and ss -tan output and spot real problems like CLOSE-WAIT leaks.

66.1 Why is a handshake needed?

  • The client does not know if any application is listening on the server port (e.g. 8080).
  • Both sides must agree from which sequence number they will count their bytes.
  • If the client sent data directly and the server was not ready, the data would be wasted.
  • So TCP first makes a small agreement, like saying Hello on a phone call before talking.

66.2 Three-way handshake: SYN, SYN-ACK, ACK

  • Client sends SYN with a random initial sequence number (ISN): I want to connect, my counting starts here.
  • Server port is in LISTEN; it replies SYN-ACK with its own ISN and ack = client seq + 1.
  • Client sends ACK; both sides move to ESTABLISHED and data can flow.
Three-way handshake (lab)
  1. 1.Client CLOSED, server LISTEN on 192.168.86.10:8080

  2. 2.Client -> SYN [S], seq 3887866737 (client: SYN-SENT)

  3. 3.Server -> SYN-ACK [S.], seq 872738715, ack 3887866738 (server: SYN-RECV)

  4. 4.Client -> ACK [.], ack 1: both sides ESTABLISHED

  5. 5.Client -> data [P.], seq 1:7, length 6 (hello)

ack = other side's seq + 1

66.3 TCP flags and tcpdump letters

FlagtcpdumpMeaning
SYN[S]Start a connection, carries the starting seq number
ACK[.]Confirms data received
SYN+ACK[S.]The server's reply in the handshake
FIN[F]Close my direction politely
RST[R]Reset: end the connection right now
PSH[P]Deliver the data to the application now

66.4 FIN: the four-way close

  • Each direction closes separately, so it takes four segments.
  • The side that closes first goes FIN-WAIT, then TIME-WAIT.
  • The other side goes CLOSE-WAIT (got your FIN, my app has not closed yet), then sends its own FIN (LAST-ACK).
  • TIME-WAIT lasts 60 s on Linux so late packets do not mix into a new connection.
FIN: closing politely
  1. 1.Client (no more data) -> FIN; client: FIN-WAIT-1

  2. 2.Server -> ACK; server: CLOSE-WAIT, client: FIN-WAIT-2

  3. 3.Server app closes -> server sends its own FIN; server: LAST-ACK

  4. 4.Client -> last ACK; client: TIME-WAIT (60 s), server: CLOSED

The side that closed first holds TIME-WAIT

66.5 RST: Connection refused

  • SYN to a port with no listener (9999): the server kernel replies RST-ACK immediately.
  • The client sees Connection refused; no handshake, no four-way close.
  • RST also appears when an application crashes or a firewall rejects the connection.
  • A firewall that silently drops gives no reply at all: the client waits until a timeout.
RST on a closed port
  1. 1.Client -> SYN to 192.168.86.10:9999

  2. 2.No application listening on 9999

  3. 3.Server kernel -> RST-ACK [R.]

  4. 4.Client: Connection refused (instant, no timeout)

66.6 Connection states

StateMeaningWhere
LISTENPort open, waiting for connectionsServer
SYN-SENT / SYN-RECVHandshake in progress (short)Client / server
ESTABLISHEDHandshake done, data flowingBoth
FIN-WAIT-1/2Sent FIN, waiting (short)Side that closed first
CLOSE-WAITGot FIN, application has not closedSide that received FIN
LAST-ACKSent own FIN, waiting for last ACKSide that closed second
TIME-WAITWaits 60 s on Linux after closingSide that closed first

66.7 Practical: LISTEN, handshake and data

  • Lab in Docker: server 192.168.86.10, client 192.168.86.20.
  • socat runs an echo server on 8080 (a second listener on 9090 is used for CLOSE-WAIT).
  • ss -tln shows both ports in LISTEN.
  • In tcpdump: [S], then [S.] with ack = client seq + 1, then [.], then [P.] carrying the 6 bytes of hello.
Terminal
# server 192.168.86.10socat TCP4-LISTEN:8080,reuseaddr,fork EXEC:catss -tlntcpdump -n -t -l -i eth0 tcp# client 192.168.86.20echo hello | nc -N -p 40000 192.168.86.10 8080ss -tannc -v -p 40001 192.168.86.10 9999
Output
$ ss -tlnState  Recv-Q Send-Q Local Address:PortLISTEN 0      5            0.0.0.0:9090LISTEN 0      5            0.0.0.0:8080
Output
$ tcpdump -n -t -l -i eth0 tcpIP 192.168.86.20.40000 > 192.168.86.10.8080: Flags [S], seq 3887866737IP 192.168.86.10.8080 > 192.168.86.20.40000: Flags [S.], seq 872738715, ack 3887866738IP 192.168.86.20.40000 > 192.168.86.10.8080: Flags [.], ack 1IP 192.168.86.20.40000 > 192.168.86.10.8080: Flags [P.], seq 1:7, ack 1, length 6

66.8 Practical: FIN, TIME-WAIT and RST

  • When the client's stdin ends, nc -N sends [F.]; the server echoes hello, sends its own FIN, and the client sends the last ACK.
  • ss -tan on the client shows TIME-WAIT, because the client closed first.
  • nc to port 9999: Connection refused; tcpdump shows only two packets, [S] and [R.].
Output
IP 192.168.86.20.40000 > 192.168.86.10.8080: Flags [F.], seq 7, ack 1IP 192.168.86.10.8080 > 192.168.86.20.40000: Flags [P.], seq 1:7, ack 8IP 192.168.86.20.40000 > 192.168.86.10.8080: Flags [.], ack 7IP 192.168.86.10.8080 > 192.168.86.20.40000: Flags [F.], seq 7, ack 8IP 192.168.86.20.40000 > 192.168.86.10.8080: Flags [.], ack 8 $ ss -tanState     Recv-Q Send-Q Local Address:Port   Peer Address:PortTIME-WAIT 0      0      192.168.86.20:40000 192.168.86.10:8080
Output
$ nc -v -p 40001 192.168.86.10 9999nc: connect to 192.168.86.10 port 9999 (tcp) failed: Connection refusedIP 192.168.86.20.40001 > 192.168.86.10.9999: Flags [S], seq 1033211786IP 192.168.86.10.9999 > 192.168.86.20.40001: Flags [R.], seq 0, ack 1033211787

66.9 Practical: ESTABLISHED and CLOSE-WAIT

  • bash /dev/tcp keeps a connection open: ESTAB on both sides with local and peer swapped.
  • Server on 9090 accepts and closes at once, but the client never closes its socket.
  • Result: client stuck in CLOSE-WAIT, server in FIN-WAIT-2.
Terminal
# client shell 1: connect, then waitexec 3<>/dev/tcp/192.168.86.10/8080ss -tan# server 9090: accept, then closesocat TCP4-LISTEN:9090,reuseaddr,fork EXEC:true# client shell 2: connect, then waitexec 3<>/dev/tcp/192.168.86.10/9090ss -tan state close-wait
Output
client$ ss -tanState     Recv-Q Send-Q Local Address:Port   Peer Address:PortESTAB     0      0      192.168.86.20:44638 192.168.86.10:8080server$ ss -tanState  Recv-Q Send-Q Local Address:Port   Peer Address:PortESTAB  0      0      192.168.86.10:8080  192.168.86.20:44638 client$ ss -tan state close-waitRecv-Q Send-Q Local Address:Port   Peer Address:Port1      0      192.168.86.20:53980 192.168.86.10:9090server$ ss -tan state fin-wait-2Recv-Q Send-Q Local Address:Port  Peer Address:Port0      0      192.168.86.10:9090 192.168.86.20:53980

66.10 Common mistakes

  • Thinking lots of TIME-WAIT is a bug: it is a protection (lost last ACK, old packets) and thousands are normal on a busy server.
  • Blaming the network for CLOSE-WAIT: it means the local application never closed its socket (e.g. forgot to close a DB/HTTP connection).
  • Confusing refused (RST, instant) with timeout (silent drop, firewall).
  • Reading [.] as no flag: it is ACK.

66.11 Interview tips

  • Why three packets? Each side must choose its ISN and get it confirmed; the server combines its SYN and ACK, so 3 instead of 4.
  • 500 CLOSE-WAIT on an API server = application bug, not network.
  • TIME-WAIT is on the side that closed first, 60 s on Linux.
  • Connection refused = RST from a closed port; timeout = packets dropped.