The server side of the sequence
A listening server makes a socket, binds it to a local address and port, and calls listen to mark it as ready for connections. Each call to accept takes one completed connection from the queue and returns a new socket for that client. The listening socket stays in place, so the server can accept the next client at once. Binding to a port below 1024 requires privileges, which is why ordinary programs use high ports.
The client side of the sequence
A client makes a socket and calls connect with the server address. The kernel carries out the TCP handshake, a three-way exchange of packets, before connect returns successfully. If nothing listens, the kernel answers with a reset, and connect fails with connection refused. That refused error is the signal that reached the port but found no listener.
Watching the calls with strace
The strace tool records the system calls a process makes, as the first module showed. Tracing a short bash connection reveals the socket calls, even though bash hides them behind a redirection.
strace -e trace=socket,connect bash -c 'exec 3<>/dev/tcp/127.0.0.1/9999' 2>&1 | tail -3The output shows a socket call that creates the descriptor and a connect call to 127.0.0.1 on port 9999, which fails with ECONNREFUSED when nothing listens. The exact text varies by version, but the connect call is the one that matters. The strace tool writes to standard error, so the redirection merges it with the output.
Loopback as a test bed
Because the traffic never leaves the machine, loopback is ideal for testing. You can start a listener, connect to it, and read the bytes, all within one terminal. The unit on raw TCP showed the pattern with BusyBox nc, and the same test works with any program that listens. Do not rely on the address 127.0.0.1 being the only way to reach a service, because a server bound to 0.0.0.0 accepts connections on every local address.
Bind address matters
A server that binds to 127.0.0.1 accepts connections only from the same machine. A server bound to 0.0.0.0 accepts them from every interface. The difference is the most common reason a service is reachable from the network when the author meant it to be local. When you read a listening socket, look at the local address first.
A service that listens on 0.0.0.0 is exposed to every network the machine is attached to. Bind to 127.0.0.1 for local-only tools unless you have a reason to expose them.
Hands-on: trace a refused connection
Run the strace command from this unit and read the connect line. Then think about what would change if a listener were running on port 9999. The lab asks you to write the result of a refused connection into a file, which is a practical way to record the behavior.
bash -c 'exec 3<>/dev/tcp/127.0.0.1/9999' 2>/dev/null; echo "status=$?"The output is status=1 when nothing listens. A status of 0 would mean the handshake completed, which is what a live server would produce.