Networking in C · beginner · ~6 min

Why localhost-only testing matters

Understand this course's safety policy for networking exercises, and learn how to enforce it directly in your code.

Lesson

The rule: localhost only

Every networking exercise in this course is localhost-only. Localhost means your own machine, reachable at the address 127.0.0.1 (also called the loopback address). Traffic to this address never leaves your computer.

In practice, that means:

  • Bind to INADDR_LOOPBACK (127.0.0.1), never INADDR_ANY.
    • Binding is how a server picks the address it listens on.
    • INADDR_ANY would listen on every network interface, including ones reachable from outside.
  • Connect to 127.0.0.1, never to an external host.
  • The sandbox enforces this at runtime. Submissions run with --network=none, so even an exercise that tried to reach the public internet would simply fail with ENETUNREACH ("network unreachable").

Why this rule exists

You lose nothing technically. You can practice every concept — the handshake, message framing, server design, error handling — without ever generating a packet that leaves your machine. The kernel's loopback interface handles all of it internally.

Legal and ethical reasons. Scanning, connecting to, or sending traffic to other people's hosts is not something this course teaches or condones. A defensive understanding of how servers work is enough.

Reproducibility. Localhost behaves the same everywhere: every machine, every CI runner, every Docker container.

If you need to test remotely

If your own work ever genuinely requires testing against a remote service, do it on a machine you own, against a service you own. That is outside the scope of this curriculum.

Code examples

/* GOOD — localhost only. */
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

Common mistakes

  • Binding to INADDR_ANY on a networked machine. Suddenly your "lab" server is reachable from every client on the same WiFi, not just from your own machine.

Summary

  • Networking exercises are localhost-only: bind to INADDR_LOOPBACK, connect to 127.0.0.1.
  • The sandbox enforces --network=none as a backstop, so off-machine traffic fails with ENETUNREACH.
  • The rule costs you nothing: every concept can be practiced over loopback.
  • It keeps the course legal, ethical, and reproducible everywhere.

Practice with these exercises