TCP, UDP and ports: refused is not timed out
Watch a real TCP connection open and close, learn what refused, timed out and open each tell you, see UDP send with no reply, and find what is listening with ss.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: two Alpine containers on a Docker network: a server listening with socat on TCP 8080 and
UDP 9000, and a client using nc, tcpdump and ss. The output shown is real.
Cloud lab: none needed.
Most "can't connect" problems are solved by reading one error message correctly. TCP gives you three different answers, and each one points at a different place.
The lab is two containers: a server at 10.50.0.10 (TCP 8080 answers "hello from srv", UDP 9000 prints what it
gets) and a client at 10.50.0.2.
A TCP connection, start to finish
$ tcpdump -i eth0 -n -c 10 host 10.50.0.10 and port 8080 & nc 10.50.0.10 8080 hello from srv IP 10.50.0.2.47158 > 10.50.0.10.8080: Flags [S], seq 795707756, ... IP 10.50.0.10.8080 > 10.50.0.2.47158: Flags [S.], seq 29718999, ack 795707757, ... IP 10.50.0.2.47158 > 10.50.0.10.8080: Flags [.], ack 1, ... IP 10.50.0.10.8080 > 10.50.0.2.47158: Flags [P.], seq 1:16, ack 1, ... length 15 IP 10.50.0.2.47158 > 10.50.0.10.8080: Flags [.], ack 16, ... IP 10.50.0.10.8080 > 10.50.0.2.47158: Flags [F.], seq 16, ack 1, ... IP 10.50.0.2.47158 > 10.50.0.10.8080: Flags [F.], seq 1, ack 17, ...
(Timestamps and options trimmed.) Before one byte of data, three packets set up the connection: that round trip is why connecting across the world feels slow, and why connection reuse and keep-alive matter. Every byte is then acknowledged, so TCP can resend what's lost. The client picked a random high ephemeral port (47158); the server's port is fixed (8080). Those four values, two IPs and two ports, identify the connection.
Open, refused or timed out
The single most useful skill in this lesson:
$ nc -zv 10.50.0.10 8080 Connection to 10.50.0.10 8080 port [tcp/http-alt] succeeded! $ nc -zv 10.50.0.10 8081 nc: connect to 10.50.0.10 port 8081 (tcp) failed: Connection refused $ nc -zv -w 3 10.50.0.99 8080 nc: connect to 10.50.0.99 port 8080 (tcp) timed out: Operation in progress
On the wire, the refused port answered the SYN with a reset, Flags [R.], 0.3 milliseconds later. Refused is
fast and means you reached the host: look at the app. A timeout is slow and means silence: look at the path,
firewalls and security groups. Cloud firewalls drop rather than refuse, which is why a missing security group rule
always looks like a timeout.
UDP: just send
UDP has no handshake, no acknowledgements and no connection. One datagram goes out; nothing comes back unless the application sends its own reply.
$ tcpdump -i eth0 -n -c 1 udp port 9000 & echo hi | nc -u -w1 10.50.0.10 9000 IP 10.50.0.2.50055 > 10.50.0.10.9000: UDP, length 3
tcpdump -i eth0 -n -c 1 udp port 9000 & echo hi | nc -u -w1 10.50.0.10 9000|pipe: send output to the next command
The server printed hi; the client never learned whether it arrived. That's fine for a DNS lookup (ask again if
no answer comes) or a video frame (a late frame is useless anyway). HTTP/3 runs on QUIC, which builds its own
reliability on top of UDP to avoid TCP's slower setup.
What is listening
On the server, ss lists every socket. -t TCP, -u UDP, -l listening, -n numbers instead of names, and
-p adds the process (when you have permission):
$ ss -tlnu Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 127.0.0.11:51087 0.0.0.0:* udp UNCONN 0 0 0.0.0.0:9000 0.0.0.0:* tcp LISTEN 0 4096 127.0.0.11:45011 0.0.0.0:* tcp LISTEN 0 5 0.0.0.0:8080 0.0.0.0:*
0.0.0.0:8080 means "port 8080 on every interface", reachable from outside. A service on 127.0.0.1:8080 would
accept only local connections, a classic reason for "works on the server, refused from my laptop". (The
127.0.0.11 lines are Docker's built-in DNS resolver.) Drop -l to see established connections instead.
Ports below 1024 are well-known (22 SSH, 53 DNS, 80 HTTP, 443 HTTPS) and need root to bind on Linux; your apps usually listen on higher ones such as 3000 or 8080 behind a proxy (lesson 9).
Three answers from one server
Run the server container listening on TCP 8080. From a client container, get one open, one refused and one timed-out result, capture the refused one with tcpdump to see the reset, and list the server's sockets with ss.
~/ports.txtReveal one safe solution
$ docker network create --subnet 10.50.0.0/24 lab-net $ docker run -d --name srv --network lab-net --ip 10.50.0.10 alpine:3.22 \ sh -c 'apk add -q socat iproute2; socat TCP-LISTEN:8080,fork,reuseaddr SYSTEM:"echo hello"' $ docker run --rm --network lab-net --cap-add NET_ADMIN --cap-add NET_RAW alpine:3.22 sh -c ' apk add -q netcat-openbsd tcpdump nc -zv 10.50.0.10 8080; nc -zv 10.50.0.10 8081; nc -zv -w 3 10.50.0.99 8080 tcpdump -n -c 2 port 8081 & sleep 1; nc -z 10.50.0.10 8081; sleep 1' > ~/ports.txt 2>&1 $ docker exec srv ss -tln >> ~/ports.txt $ docker rm -f srv && docker network rm lab-net
docker network create --subnet 10.50.0.0/24 lab-netdockerbuild and run containersnetworkmanage networkscreatecreate one
docker run -d --name srv --network lab-net --ip 10.50.0.10 alpine:3.22 \runstart a new container-drun in the background--name srvname the container--network lab-netwhich network to join10.50.0.10the image to startalpine:3.22 \the command run inside, instead of the image’s default
docker run --rm --network lab-net --cap-add NET_ADMIN --cap-add NET_RAW alpine:3.22 sh -c '--rmdelete the container when it exits--cap-add NET_ADMINadd a Linux capability back--cap-add NET_RAWadd a Linux capability backalpine:3.22the image to startsh -c 'the command run inside, instead of the image’s default
docker exec srv ss -tln >> ~/ports.txtexecrun a command in a running containersrvthe containerss -tlnthe command to run inside it>>append output to a file~/ports.txtthe command to run inside it
docker rm -f srv && docker network rm lab-netrmdelete a container-fstop it first if running&&run the next command only if this one workedrmdelete one
You should see succeeded, refused, timed out, then a [S] and an [R.] packet.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
You get "Connection refused" instantly. What is the most likely cause?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.