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.