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"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"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"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:
cap "-i lo -n -c 1 tcp port 9000" "nc -w 1 127.0.0.1 9000"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"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 -1nc -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"Connection timed out
Nothing answers. The kernel retries the same SYN, then gives up:
cap "-i eth0 -n -c 3 host 10.255.255.1" "nc -w 10 10.255.255.1 80"Three identical SYNs, nothing back. The client waits out its timeout:
nc -v -w 3 10.255.255.1 80 2>&1 | head -1Telling 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=$?"| Refused | Timed out | |
|---|---|---|
| Packets back | [R.], immediately |
none |
| Time | milliseconds | seconds |
| Means | port closed | nothing reachable |
| Check | is the service running | address, route, firewall |