$_ CLI In Depth
Learning progress 0%
case networking sockets ssh

System Deep-Dive: The Socket API on Loopback

3 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:23

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.

BASH1 lines
strace -e trace=socket,connect bash -c 'exec 3<>/dev/tcp/127.0.0.1/9999' 2>&1 | tail -3

The 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.

Warning

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.

BASH1 lines
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.

A TCP connection on loopback, from listen to close

Visualizer

A TCP connection starts with a three-packet handshake and ends with a four-packet close. Watch the kernel state of each socket as the packets pass, the way ss -tan shows them.

1 / 5
socket()
bind(127.0.0.1:9000)
listen(backlog)
accept() waits
Three-way handshake, as ss shows it
StepPacketClient socketServer connection
server: listen()--LISTEN (listener)

The server calls socket(), then bind() to pick the address and port, then listen(). listen() makes the socket a passive listener with a queue for finished connections. Only the listener exists so far.

nc -l -s 127.0.0.1 -p 9000 & pid=$!; sleep 0.3; ss -tlnp | grep 9000; kill $pid
Check yourself Question 1 of 4
Concept Check Call that marks a socket ready recall

Which socket call marks a bound socket as ready to accept incoming connections?

Recap & Key Takeaways

A server calls socket, bind, listen, and accept, and each accepted client gets a new socket while the listening socket stays open.
A client's connect call performs the TCP handshake in the kernel, and a refused connection means nothing listens on that port.
strace -e trace=socket,connect shows the socket calls behind a bash /dev/tcp connection.
The bind address decides who can connect: 127.0.0.1 is local only, and 0.0.0.0 accepts connections from every interface.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue