Skip to content
Sign inGet all access
Chapter 3·8 minFree

The Handshake

Three packets captured, and telling refused from timed out.

Three packets before a single byte of your data.

Three packets

SYN out, SYN-ACK back, ACK out:

cap "-i lo -n -c 3 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [S], seq X, win W, length 0IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [S.], seq X, ack X, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [.], ack 1, win W, length 0

length 0 on all three — nothing of yours has moved yet.

Each side sends a sequence number and acknowledges the other’s, so both know where the stream starts.

Reading the flags

Six you will meet, abbreviated:

Flag Name Means
[S] SYN open a connection
[S.] SYN-ACK agreed, and here is my side
[.] ACK received
[P.] PSH-ACK here is data
[F.] FIN-ACK I am done sending
[R.] RST-ACK this connection is over now

Every flag in one capture — a whole connection, open to close:

cap "-i lo -n -c 8 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [S], seq X, win W, length 0IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [S.], seq X, ack X, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [.], ack 1, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 1:4, ack 1, win W, length 3IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [.], ack 4, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [F.], seq 4, ack 1, win W, length 0IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [F.], seq 1, ack 5, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [.], ack 2, win W, length 0

Three to open, two to carry three bytes, three to close — chapter 5 is the last three.

Add a fourth packet to the handshake and the data appears:

cap "-i lo -n -c 4 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [S], seq X, win W, length 0IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [S.], seq X, ack X, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [.], ack 1, win W, length 0IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 1:4, ack 1, win W, length 3

length 3 is hi and the newline.

Sequence numbers

Each side picks a random start. The captures show seq X because this course replaces it:

terminal
cap "-i lo -n -c 1 tcp port 9000" "nc -w 1 127.0.0.1 9000"
Output$ cap "-i lo -n -c 1 tcp port 9000" "nc -w 1 127.0.0.1 9000"IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [S], seq X, win W, length 0$ # without this course's helper, the same packet:IP 127.0.0.1.45122 > 127.0.0.1.9000: Flags [S], seq 3304867627, win 65495

After the handshake tcpdump numbers them relative to the start, so the data packet reads seq 1:4.

What it costs

One round trip before any data. On loopback that is microseconds; across the internet it is your latency to the server, paid per connection.

This is the cost HTTP keep-alive avoids — holding the connection open skips these three packets next time.

Connection refused

Something answered and said no — two packets, immediate:

cap "-i lo -n -c 2 port 9999" "nc -w 1 127.0.0.1 9999"
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9999: Flags [S], seq X, win W, length 0IP 127.0.0.1.9999 > 127.0.0.1.CLIENT: Flags [R.], seq 0, ack X, win W, length 0

The [R.] is the kernel answering for a port nobody listens on. Your client reports:

nc -v -w 1 127.0.0.1 9999 2>&1 | head -1
Outputnc: connect to 127.0.0.1 port 9999 (tcp) failed: Connection refused

nc -z reports it in one word, which is what chapter 8 uses:

nc -z -w 1 127.0.0.1 9000 2>/dev/null && echo "9000 open" || echo "9000 closed"
nc -z -w 1 127.0.0.1 9999 2>/dev/null && echo "9999 open" || echo "9999 closed"
Output9000 open9999 closed

Connection timed out

Nothing answers. The kernel retries the same SYN, then gives up:

terminal
cap "-i eth0 -n -c 3 host 10.255.255.1" "nc -w 10 10.255.255.1 80"
OutputIP 172.17.0.4.CLIENT > 10.255.255.1.80: Flags [S], seq X, win W, length 0IP 172.17.0.4.CLIENT > 10.255.255.1.80: Flags [S], seq X, win W, length 0IP 172.17.0.4.CLIENT > 10.255.255.1.80: Flags [S], seq X, win W, length 0

Three identical SYNs, nothing back. The client waits out its timeout:

terminal
nc -v -w 3 10.255.255.1 80 2>&1 | head -1
Outputnc: connect to 10.255.255.1 port 80 (tcp) timed out: Operation in progress

Telling them apart

One packet of difference, entirely different causes — and the exit code tells you nothing:

nc -z -w 1 127.0.0.1 9999 2>/dev/null; echo "refused=$?"
timeout 4 nc -z -w 3 10.255.255.1 80 2>/dev/null; echo "timeout=$?"
Outputrefused=1timeout=1
Refused Timed out
Packets back [R.], immediately none
Time milliseconds seconds
Means port closed nothing reachable
Check is the service running address, route, firewall