Sockets & Network Programming

the loopback address

Imagine writing a letter, walking to your own front door, and posting it through your own mail slot — it never leaves the house, but it goes through exactly the same motions as real mail. The loopback address is that self-addressed channel for networking. It lets a program on your machine talk to another program on the same machine using the ordinary network stack, without any packet ever touching a network cable or wireless link.

By convention the loopback address is 127.0.0.1 in IPv4 (the whole 127.0.0.0/8 block is reserved for it) and ::1 in IPv6, and the name localhost usually maps to them. When you connect to 127.0.0.1, the operating system recognizes the loopback interface and short-circuits the traffic internally: it goes down through the socket and IP layers and straight back up to the destination socket on the same host, never reaching a physical network card. Software on the same computer can therefore use real TCP or UDP to communicate — a web app talking to its local database, a test client hitting a server you are developing — exactly as if they were on separate machines.

Why it matters: loopback is how you develop and test networked software safely on one machine, and how many services on a computer talk to each other (a database listening only on 127.0.0.1 cannot be reached from the outside network at all). That last point is a quiet security feature: binding a service to the loopback address instead of 0.0.0.0 means only local programs can connect, which is a common and deliberate hardening choice. A frequent beginner mistake is binding a dev server to 127.0.0.1 and then being puzzled that another device on the LAN cannot reach it.

You run a web server and visit http://127.0.0.1:8080 in your browser. The request and reply travel the full TCP/IP path inside your computer and never hit the network card — perfect for testing before you expose anything to the world.

Real networking that never leaves the machine.

Binding a server to 127.0.0.1 means only this machine can reach it; binding to 0.0.0.0 means every interface, including the outside network. Confusing the two is a common cause of both 'I can't connect' and accidental exposure.

Also called
localhost127.0.0.1::1loopback interface本機回送位址迴路位址