Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

6 · Ports, sockets and TCP

In one sentence: a port picks one program on a machine; a server’s listening socket waits on an address and a port, TCP opens every connection with a three-packet handshake, and the error you get says whether something answered “no” (refused) or nothing answered at all (timed out).

The problem it solves

Lessons 3 to 5 got a packet to the right machine: address, route, MAC. But a machine runs many programs, and the packet is for one of them. That’s lesson 1’s fifth question, “which flat in the building?”. When the network is fine and the app still can’t connect, the answer is almost always here: nothing listens on that port, it listens on the wrong address, or the reply never comes back.

The idea: a door number, and someone behind it

A port is a number from 1 to 65535 that names a door on the machine. A server program asks the kernel to listen on a port; from then on the kernel hands it every new connection to that port. That waiting endpoint is a socket, and ss lists them (lesson 2’s last table).

In Practical 0’s lab, start a listener in blue with nc (netcat), and look:

ip netns exec blue nc -lk 8080 &
ip netns exec blue ss -tlnp
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess                      
LISTEN 0      1            0.0.0.0:8080      0.0.0.0:*    users:(("nc",pid=2623,fd=3))

-l listen, -k keep listening after a connection ends. In ss, -p adds the program. The local address 0.0.0.0:8080 means port 8080 on every address blue has.

The handshake

A client opens a TCP connection in three packets: SYN (“let’s talk”), SYN-ACK (“yes, let’s”), ACK (“ok”). Only then does data flow. tcpdump in blue while red sends hello (echo hello | nc -N 10.0.0.2 8080; flags in [...], the rest of each line cut; -N makes nc close the connection when its input ends):

15:42:07.090971 IP 10.0.0.1.55578 > 10.0.0.2.8080: Flags [S], seq 2026845857, win 64240, ...
15:42:07.090990 IP 10.0.0.2.8080 > 10.0.0.1.55578: Flags [S.], seq 2307351405, ack 2026845858, win 65160, ...
15:42:07.091005 IP 10.0.0.1.55578 > 10.0.0.2.8080: Flags [.], ack 1, win 502, ...
15:42:07.091117 IP 10.0.0.1.55578 > 10.0.0.2.8080: Flags [P.], seq 1:7, ack 1, win 502, ...
FlagNameMeans
[S]SYNopen a connection
[S.]SYN-ACKaccepted
[.]ACKgot it
[P.]PUSHdata (here 6 bytes, hello and a newline)
[F.]FINI’m done sending
[R.]RSTno: reset this connection

Red used port 55578. A client gets a free ephemeral port from a range the kernel picks from (ip_local_port_range, here 32768 60999). A second connection, held open, shows in ss -tn on both ends, mirrored:

ESTAB 0      0           10.0.0.1:54260     10.0.0.2:8080
ESTAB 0      0           10.0.0.2:8080     10.0.0.1:54260

After it closes, the side that closed first (here red) keeps it as TIME-WAIT for a while, so a late packet of the old connection can’t land in a new one.

Refused: the machine said no

Stop the listener and connect again. The answer comes back at once, as an RST:

15:42:44.006349 IP 10.0.0.1.45344 > 10.0.0.2.8080: Flags [S], seq 435995594, win 64240, ...
15:42:44.006362 IP 10.0.0.2.8080 > 10.0.0.1.45344: Flags [R.], seq 0, ack 435995595, win 0, length 0
nc: connect to 10.0.0.2 port 8080 (tcp) failed: Connection refused

13 microseconds between the two, and no program in blue was involved: blue’s kernel answered for the empty port. Refused means the whole network works: the packet reached the machine and it replied. The problem is the program.

A common way to get it with a server running: it listens on 127.0.0.1 only. Start nc -lk 127.0.0.1 8080 in blue and ss shows the difference:

LISTEN 0      1          127.0.0.1:8080      0.0.0.0:*   

From blue itself, nc -zv 127.0.0.1 8080 succeeds (-z only connects and sends nothing, -v prints the result). From red, Connection refused: red’s SYN arrives for 10.0.0.2:8080, and nothing listens on that address. Always read the Local Address column, not just the port.

Timed out: nothing came back

Now the listener is back on 0.0.0.0, but blue has lost its route back to 10.0.0.0/24 (lesson 4’s trap, on one link). Red sends a SYN, then sends it again, about every second at first (lesson 1 showed the gaps growing when nothing limits the wait). tcpdump in blue sees each one arrive, and blue never answers. Red’s side of it, in ss:

0      1           10.0.0.1:57948     10.0.0.2:8080

(ss -tn state syn-sent: still waiting for a SYN-ACK.) With a 5-second limit (nc -w5):

nc: connect to 10.0.0.2 port 8080 (tcp) timed out: Operation now in progress

Timed out means silence: something on the way, or on the way back, lost the packets. Lesson 7’s firewall DROP gives the same silence on purpose.

ErrorOn the wireMeansLook at
Connection refused, at once[S], then [R.] backreached the machine; nothing listens on that address and portss -tlnp on the server: the port, and the address
timed out, after your limit[S] again and again, nothing backlost on the way there or backroutes both ways, firewalls (lesson 7)

Try it on your computer

Practical 2 · A switch and a service: build lesson 5’s switch with three machines, run a service on blue, watch its handshake, then break it two ways and tell them apart.

Go deeper: four numbers name a connection

A connection is identified by four values: source address and port, destination address and port. That’s how one server port takes thousands of clients: every client connection differs in its source. It also means one client address can open at most 28,232 connections at once to the same server port (60999 - 32768 + 1, the ephemeral range), a limit a busy proxy can hit.

TIME-WAIT lasts 60 seconds in Linux, a fixed constant (TCP_TIMEWAIT_LEN), and holds one of those ports while it lasts. Four ports in TIME-WAIT after four short connections is normal; tens of thousands is a client opening a new connection per request.

Go deeper: the kernel does the handshake

The program never sees SYN, SYN-ACK or ACK. It asks the kernel to listen, and the kernel completes handshakes on its own, queueing finished connections until the program takes one (listen(2)). The RST for a closed port comes from the kernel too, which is why it took 13 microseconds and needed no program.

So a connection that opens proves the kernel listens for a program, not that the program will answer it. Lesson 8’s curl asks the program itself.

Common misunderstandings

  • “Refused means a firewall blocked it.” A firewall that drops gives a timeout. Refused is an answer: an RST from the machine, usually because nothing listens (lesson 7 shows a firewall that sends RST on purpose).
  • “It listens on 8080, so others can reach it.” Only if it listens on an address they can reach. 127.0.0.1:8080 serves only the machine itself.
  • “Timed out means the server is slow.” For a connect, it means no SYN-ACK came back at all.

Check yourself

1. curl to a server fails at once with "Connection refused". The server's admin says "the service is running". What do you ask them to run? ss -tlnp on the server. Check the port is the one you use, and that the local address is 0.0.0.0 (or the address you connect to), not 127.0.0.1.
2. A connect hangs for the whole timeout. tcpdump on the server shows the SYNs arriving and no SYN-ACKs leaving. Where is the problem? On the server's side of the way back: its route to you (lesson 4), or a firewall on it dropping (lesson 7). The way there works, since the SYNs arrive.
3. ss -tan on a client shows hundreds of TIME-WAIT lines to the same server port. Is the network broken? No: those are connections the client closed, kept for 60 seconds each. It shows a client opening many short connections.

Next: Practical 2 · A switch and a service, then lesson 7 · Firewalls and NAT.