Refused or Timed Out
Lesson 18 of 47 in Coddy's Networking with the Terminal course.
When a connection fails it fails in one of a few specific ways, and each one points somewhere different. curl reports which by its exit code, the number a command leaves behind, readable as $?.
curl -s http://127.0.0.1:9999/
echo $?Three are worth memorising:
- 0: success
- 7: connection refused, the machine answered and said nothing is listening there
- 28: timed out, nothing answered at all
7 is fast and 28 is slow, and that difference is the diagnosis. Refused means you reached the machine and got a straight no, so the address is right and the service is not running. A timeout means your request vanished, which usually means a firewall dropped it or the address is wrong.
Challenge
HardMake both failures happen and read the codes.
- Ask
http://127.0.0.1:9999/for a page, with nothing listening there, and printrefused=followed by the exit code - Do the same against
http://10.255.255.1/, an address that swallows traffic, with--max-time 2, and printtimeout=and its exit code
Expected output:
refused=7
timeout=28
$?holds the exit code of the command that just ran, so read it immediately.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Networking with the Terminal
4Is It Reachable?
ICMP and pingReading Ping OutputWhen Ping LiesRefused or Timed OutRecap: ReachabilityPractice on your own: Terminal playground