Networking & Sockets

localhost and the loopback interface

/ localhost -> LOH-kuhl-host /

Sometimes you want to send a letter to yourself - to the same building you are standing in - without it ever going out to the post office. Computers have exactly this: a special address that means 'this very machine', so a program can talk to another program on the same computer using the full network machinery, but without any packet ever touching a real network cable. That address is localhost, and the virtual network device that carries it is the loopback interface.

Concretely, localhost is a name that resolves to a fixed loopback address: 127.0.0.1 in IPv4 (any 127.x.x.x address works) or ::1 in IPv6. When a program connects to 127.0.0.1, the operating system recognizes it as the machine's own loopback interface and routes the bytes internally, never sending them onto the wire. From the program's point of view it is an ordinary socket connection - same socket(), connect(), send(), recv() - but it is fast, private to the machine, and works with no network at all. Many services bind only to 127.0.0.1 specifically so that only programs on the same machine can reach them.

Why it matters: loopback is how you develop and test network programs without a second computer - run the server and the client on your own laptop, point the client at 127.0.0.1, and you have a real connection. It is also a security boundary: a database told to bind to 127.0.0.1 cannot be reached from the outside world at all, only from local processes. Just remember the flip side - because localhost is reliable and instant, it can hide bugs (like the no-message-boundaries one) that only surface on a real, slower network.

Run a server bound to port 8080 on your laptop, then in another terminal connect a client to 127.0.0.1:8080 - the two programs talk over a real socket connection without a single byte leaving the machine. Binding the server to 127.0.0.1 instead of 0.0.0.0 means outside machines cannot reach it at all.

127.0.0.1 routes back to your own machine - a real connection that never touches the network.

Binding to 127.0.0.1 limits a service to local processes only; binding to 0.0.0.0 exposes it on every interface. Confusing the two is a real security mistake - do not bind a sensitive service to 0.0.0.0 by accident.

Also called
loopback address127.0.0.1::1迴路位址本機位址