Skip to the page

Computer networks · core

IP addresses, subnets and ports

An IP address finds the machine, a port finds the program. A subnet mask tells which devices are on the same network.

The notes

What you must know

  • A subnet mask 255.255.255.0 is /24: the first 24 bits are the network and the last 8 are the host. Network 192.168.1.0, broadcast 192.168.1.255, 254 usable addresses.
  • Two addresses are reserved in each subnet: the network address and the broadcast address.
  • A device on another network is reached through the router (default gateway). 192.168.2.5 is not on 192.168.1.0/24.
  • 255.255.255.192 is /26: blocks of 64. Address 192.168.1.37 is in 0 to 63, so network .0, broadcast .63, and 62 usable hosts.
  • Private ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. They are not routed on the internet; the router uses NAT.
  • A port is a 16-bit number (0 to 65535). A connection is identified by source IP, source port, destination IP, destination port and protocol.
  • Only one program can listen on the same IP, port and protocol. Port 8080 already in use: find the process (netstat -ano on Windows; lsof -i :8080 or ss -ltnp on Linux and Mac) and stop it.
  • localhost (127.0.0.1) means this same machine. A friend cannot open your localhost. Use your Wi-Fi IP, make the server listen on 0.0.0.0, and allow the port in the firewall.

The red pen

Common traps: mistake → fix

  • Mistake: A /24 network has 256 devices

    Fix: 254 usable, because two addresses are reserved.

  • Mistake: A 192.168 address can be reached from the internet

    Fix: it is private.

  • Mistake: Send your friend localhost:3000

    Fix: localhost is their own laptop.

  • Mistake: Restart the laptop when the port is busy

    Fix: find and stop the process.

Easy to hard

Questions interviewers ask, easy to hard

  1. Is 192.168.2.5 on the same network as 192.168.1.10 with mask 255.255.255.0?
  2. Find the network, broadcast and usable hosts for 192.168.1.37/26.
  3. Port 8080 is already in use. What do you do?
  4. Why can a friend not open your project at localhost:3000?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview