How to identify if two IP addresses are on the same network

Why knowing the subnet mask is key

Β·

2 min read

The IP address is a logical address that we use to send data on the internet to the intended location. In this atomic, we will focus on identifying classes & networks using an IP address.

IP address lives on layer 3 of the OSI model and consists of IP address class space plus the host address. Classful addressing divides IP into five layers, namely Class A, B, C, D, and E.

IP address takes the structure of xxx.xxx.xxx.xxx.

192.168.1.1

You are most probably familiar with this IP address, right? Your router management panel lives at this address.

The set of eight bits forms a byte and is also commonly referred to as an octet.

Classful IP addresses

classful-addresses.png

🌈 Quick math to calculate available IP addresses. Take the free bits and the result of 2^(free bits) is the number of IP addresses at your disposal.

In Class A, we have 24 free bits (4 octets = 32 bits, 8 bits occupied for the network, we get the remaining 24 bits) so we have 2^24 IP addresses available. Whereas, in class C, we have only 256.

As you might have already noticed, the distribution of the address space is not equal at all. Class A enjoys an enormous range of IP addresses, while class B and class C get very little address space compared to class A.

You can ignore classes D and E as you won’t need them when building your web applications.

How to check if two IP addresses are on the same network

To check if two IP addresses are on the same network, we need to:

  1. Know what network the first IP is on, and then
  2. See if the second IP is also on the same network.

Knowing the subnet mask is the key when identifying if two IPs are on the same network or not.

For example, let's check if 132.21.1.20 and 132.21.16.10 are on the same network.

Since these IPs belong to class B (refer to the table above), the subnet mask is 255.255.0.0. This means the first two octets (132.21) represent the network part while the last two represent the host part (1.20 and 16.10).

For the purpose of the question, we can confidently say that these IPs belong to the same network.

And that's it! Hope you liked this atomic and it gave you some perspective on the seemingly random collection of numbers in an IP address.

Interested in TCP, how it works, and why connection-less protocols like UDP cannot replace it? You may like TCP connection establishment for Software Developers.

Β