What you will learn
- How IPv4 and IPv6 addresses are written and why IPv6 exists.
- How a subnet mask splits an address into network and host parts.
- What port numbers are and which ones you may use.
- Loopback, private ranges and NAT, and why your laptop’s address is not your public address.
IPv4: four bytes
An IPv4 address is a 32-bit number, written as four decimal bytes separated by dots:
192.168.1.40 = 11000000 10101000 00000001 00101000 = 0xC0A80128
32 bits allow about 4.3 billion addresses. That sounded like plenty in 1981; today there are far more phones, laptops, servers and smart bulbs than that, and the free pool of IPv4 addresses ran out years ago. Tricks like NAT (below) stretched IPv4, but the real fix is IPv6.
IPv6: sixteen bytes
An IPv6 address is 128 bits, written as eight groups of four hex digits separated by colons:
2001:0db8:0000:0000:0000:ff00:0042:8329
Two shortening rules make them easier to read:
- Leading zeros in a group can be dropped:
0db8becomesdb8,0042becomes42. - One run of consecutive all-zero groups can be replaced by
::.
So the address above is usually written 2001:db8::ff00:42:8329. You can only use :: once, otherwise the reader could not tell how many zero groups each :: hides.
Some special IPv6 forms you will meet:
| Address | Meaning |
|---|---|
::1 | Loopback (“this machine”), like 127.0.0.1 |
:: | The unspecified (any) address, like 0.0.0.0 |
::ffff:192.0.2.7 | An IPv4 address mapped into IPv6 form |
fe80::/10 | Link-local addresses, valid only on one network segment |
2001:db8::/32 | Reserved for documentation examples (like this lesson) |
The IPv4-mapped form matters for servers: an IPv6 socket can often accept IPv4 clients too, and those clients show up as ::ffff:a.b.c.d. You will see exactly that in the UDP lesson.
Subnets: network part and host part
Organisations get blocks of addresses. The first bits of an address identify the network; the remaining bits identify a host on that network. The split point is written with CIDR notation: a slash and the number of network bits.
192.168.1.40/24
network bits: first 24 -> 192.168.1
host bits: last 8 -> .40 (hosts .1 to .254 on this subnet)
The same split can be expressed as a netmask, a 32-bit value with the network bits set to 1:
/24 = 255.255.255.0
/20 = 255.255.240.0
/16 = 255.255.0.0
To find the network address, AND the address with the mask. In C:
uint32_t addr = 0xC0A80128; /* 192.168.1.40 in host byte order */
uint32_t mask = 0xFFFFFF00; /* /24 */
uint32_t net = addr & mask; /* 192.168.1.0 */
uint32_t bcast = net | ~mask; /* 192.168.1.255 - the subnet broadcast address */
IPv6 uses the same slash notation; a typical home or office LAN is a /64, leaving 64 bits for hosts. The subnet broadcast address (all host bits set to 1) becomes important in the broadcast lesson.
Port numbers: which program on the host
An IP address gets a packet to a machine. A port number (16 bits, 0–65535) gets it to the right program on that machine. A web server listens on port 443 (HTTPS) or 80 (HTTP); SSH listens on 22; your own test server might listen on 5050.
| Range | Name | What to know |
|---|---|---|
| 0–1023 | Well-known ports | Binding usually needs root (or CAP_NET_BIND_SERVICE on Linux). |
| 1024–49151 | Registered ports | Assigned by IANA to services, but any program may use a free one. |
| 49152–65535 | Dynamic / ephemeral | The OS picks from here when a client connects without choosing a port. |
A TCP connection is uniquely identified by four values: local address, local port, remote address, remote port. That is how one web server on port 443 can talk to thousands of browsers at once: every connection differs in at least the remote address or port.
Your system’s list of named services lives in /etc/services, which is what lets getaddrinfo() accept "https" instead of "443".
Loopback, private networks and NAT
Loopback. 127.0.0.1 (all of 127.0.0.0/8, really) and ::1 always mean “this machine”. Packets never leave the computer. This is how you test a client and server on one laptop, as we do throughout the course.
Private ranges. These IPv4 blocks are reserved for internal networks and are never routed on the public Internet:
| Range | CIDR |
|---|---|
| 10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 |
| 172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 |
| 192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 |
IPv6 has a similar idea called unique local addresses (fc00::/7).
NAT. Your home router has one public IPv4 address. Every device behind it gets a private address such as 192.168.1.40. When your laptop opens a connection, the router rewrites the source address and port to its own public ones and remembers the mapping, so replies find their way back. This is Network Address Translation.
What this means for socket programs:
- A client behind NAT can connect out freely.
- A server behind NAT cannot be reached from outside unless the router forwards a port to it.
- The address
getsockname()reports on your laptop is the private one, not the address the world sees.
Key takeaways
- IPv4 addresses are 4 bytes in dotted decimal; IPv6 addresses are 16 bytes in colon-separated hex with
::shortening. - CIDR
/ngives the number of network bits; AND with the netmask to get the network address. - Ports pick the program on a host; ports below 1024 normally need privileges.
- Loopback is for local testing; private ranges sit behind NAT and need port forwarding to be reachable.
Exercises
- Expand
fe80::1:2to its full eight-group form, then shorten2001:0db8:0000:0000:0001:0000:0000:0001correctly. - For
10.20.30.40/20, compute the netmask, network address and broadcast address by hand, then check with a short C program. - Run
ip addr(Linux) orifconfig(macOS). Identify your loopback, private and (if any) IPv6 link-local addresses.
Quiz
- Why can
::appear only once in an IPv6 address? - Which of these are private IPv4 addresses:
172.20.1.1,172.40.1.1,192.168.0.9,11.0.0.1? - Your server runs on a laptop at
192.168.1.40behind a home router. Why can’t a friend across town connect to it, and what fixes it?
Show answers
- Each
::hides an unknown number of zero groups. With two of them a reader could not tell how to split the missing groups between them. 172.20.1.1(inside172.16.0.0/12) and192.168.0.9.172.40.1.1and11.0.0.1are public.- The address is private and the router’s NAT drops unsolicited incoming connections. Forward a port on the router to the laptop (or host the server on a machine with a public address).