DEV Community

Cover image for Subnets
Meerthika
Meerthika

Posted on

Subnets

In this blog, I’ll break down subnetting, show you how it works, and walk you through my subnetting project (complete with diagrams and code).

Let’s untangle the magic behind those /24, /16, and 255.255.255.0 masks. 🎭


🚦 Why Subnetting Matters

"Without subnetting, your network’s like a city with no street names — total chaos."

Imagine 10,000 devices trying to talk on the same street with no addresses. That’s what a flat IP network looks like.

Subnetting introduces:

  • 📦 Structure
  • 🚀 Routing efficiency
  • 🔐 Security boundaries

Let’s break it down 👇


🌐 What is a Subnet?

A subnet (short for subnetwork) is a smaller network within a larger one, created by dividing an IP range using a subnet mask.

For example:

🧮 IP Address: 135.70.1.0

🎭 Subnet Mask: 255.255.255.0

We’re saying:

  • First 3 octets = Network
  • Last octet = Hosts

This helps:

  • Routers 🚚 know where to send packets
  • Admins 🧑‍💻 manage traffic flow between devices and regions

🧩 How Subnetting Works

A subnet mask splits the IP address into:

  • 🧱 Network bits — define the subnet
  • 🏠 Host bits — define the actual devices

Think of each subnet like a neighborhood in a city:

Devices (houses) are easier to locate, control, and communicate within that area 🏡

📸 Visual Aid:

How Subnetting Works


🔢 IPv4 Classes and Subnet Masks

Here’s a handy chart that shows the basics of IPv4 classes:

🏷️ Class 🛡️ Subnet Mask 🔁 # of Networks 👥 Hosts per Network
A 255.0.0.0 128 networks 16,777,216 hosts
B 255.255.0.0 16,384 networks 65,536 hosts
C 255.255.255.0 2,097,152 networks 256 hosts

🔍 These classes define how many networks and hosts you can support. For example, Class C is often used in homes and small offices!


🛠️ How I Built My Own Subnet Planner in Go

Ever wanted to divide networks smartly for homes, offices, or cafés? 🍵🏢🏡

I created a Subnet Calculator in Go that:

  • ✅ Accepts an IP address and the desired number of subnets
  • 🧮 Calculates the new subnet mask
  • ✂️ Splits the network accordingly
  • 🔍 Shows usable ranges for each area

⚙️ Go Code: Subnet Planner

package main

import (
    "net"
    "fmt"
)

func main() {
    _, ipNet, err := net.ParseCIDR("192.168.0.0/16")
    baseIP := ipNet.IP
    oldPrefix, _ := ipNet.Mask.Size()

    if err != nil {
        panic(err)
    }

    newPrefix := 24
    numSubsets := 1 << (newPrefix - oldPrefix)

    var subnets []net.IPNet

    for i := 0; i < numSubsets; i++ {
        subnetIP := make(net.IP, len(baseIP))
        copy(subnetIP, baseIP)

        subnetIP[2] = byte(i)
        subnet := net.IPNet{
            IP:   subnetIP,
            Mask: net.CIDRMask(newPrefix, 32),
        }
        subnets = append(subnets, subnet)
    }

    areas := []string{"🏠 home", "🏢 office", "☕ cafe", "🌳 park", "📚 library"}

    for i, area := range areas {
        if i < len(subnets) {
            fmt.Printf("%-10s : %s\n", area, subnets[i].String())
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This prints subnet blocks for each area like:

  • 🏠 home : 192.168.0.0/24
  • 🏢 office : 192.168.1.0/24
  • cafe : 192.168.2.0/24
  • 🌳 park : 192.168.3.0/24
  • 📚 library : 192.168.4.0/24

✅ Conclusion

Subnetting isn’t just for network engineers — it’s for anyone building smart systems.

Whether you’re setting up:

  • 💻 A small office LAN
  • 🏫 A school Wi-Fi plan
  • ☁️ A cloud VPC setup

Subnetting gives you:

  1. 📶 Scalability
  2. 🔐 Security zones
  3. 🚀 Faster routing

And hey, once you get the hang of it — it’s kinda fun 😎

Top comments (2)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
meerthika profile image
Meerthika

Thankyou.