Networking Basics

Expected level: Working

Programs on different machines find each other with IP addresses, human-friendly names resolved by DNS, and port numbers that select a service on the machine. Firewalls decide which of that traffic is allowed.

The building blocks

  • An IP address identifies a network interface. IPv4 looks like 192.168.1.20, IPv6 like 2001:db8::1. Addresses in ranges such as 10.x.x.x, 172.16.x.x to 172.31.x.x and 192.168.x.x are private: they work inside a network but not on the public internet.
  • DNS turns a name like example.com into IP addresses. Your machine asks a DNS server and caches the answer, so a change can take time to be seen everywhere.
  • A port is a number from 0 to 65535 that selects a service on a machine. Well-known ones: 80 (HTTP), 443 (HTTPS), 22 (SSH), 53 (DNS). Databases and dev servers use their own, such as 5432 or 3000.
  • localhost is the name for your own machine, 127.0.0.1 in IPv4 and ::1 in IPv6. Traffic to it never leaves the computer.
  • A firewall holds rules that allow or block traffic by address, port and direction, on the machine itself or on the network in between.

An address plus a port identifies one endpoint, for example 127.0.0.1:5432. A service listening only on 127.0.0.1 cannot be reached from other machines, while one listening on 0.0.0.0 accepts connections on all interfaces.

Useful commands

Purpose PowerShell Bash
Look up a name Resolve-DnsName example.com dig example.com
Test a port Test-NetConnection example.com -Port 443 nc -vz example.com 443
Who listens on a port Get-NetTCPConnection -LocalPort 3000 ss -tlnp
Show my addresses ipconfig ip addr

nslookup and ping exist on both systems. ping may be blocked by firewalls even when the service works.

Finding what holds a port

When a server fails with “address already in use”, another process already listens on that port. Find its process ID (PID), then the program behind it:

netstat -ano | findstr :3000
tasklist /FI "PID eq 1234"
Get-NetTCPConnection -LocalPort 3000 | Select-Object LocalPort, State, OwningProcess
Get-Process -Id 1234
ss -tlnp | grep :3000
lsof -i :3000

On Linux, ss -p and lsof show only your own processes unless you use sudo. Stop the process (see Windows for Developers for Stop-Process and taskkill, kill <pid> on Linux) or pick another port.

Reading the symptoms

  • “Name not resolved” means a DNS problem or a typo in the hostname.
  • “Connection refused” means the machine answered, but nothing listens on that port (service not running, or wrong port).
  • “Timed out” means packets are being dropped, often by a firewall, or the address is unreachable.

The hosts file (C:\Windows\System32\drivers\etc\hosts or /etc/hosts) overrides DNS locally. Inside containers and WSL, localhost may mean a different machine than you expect; see Servers, VMs and Containers and Linux on Windows (WSL). The protocol spoken over these connections is covered in HTTP Fundamentals.

Try it

Start any local web server on a port of your choice. Find it with the listening-port command above, open it with localhost, then try to reach it from another device on your network and see what happens. Resolve a public domain name with nslookup and note the IP address it returns.

Common mistakes

  • Assuming a blocked ping means the service is down.
  • Forgetting that DNS answers are cached, so a change can take time to show.
  • Assuming localhost means the same machine inside a container or WSL.
  • Binding a service to 127.0.0.1 and expecting other devices to reach it.

Check yourself

  • What does Connection refused tell you that Timed out does not?
  • How do you find which process is using port 3000 on your system?
  • What is the difference between listening on 127.0.0.1 and on 0.0.0.0?

Related topics

Terms

Further reading