How 2 computers on different network communicate ?🖥️💻🌐
Consider two PCs, PC1🖥️ and PC2🖥️ which are on different networks.
Every PC has an IP address assigned to it by the DHCP(Dynamic Host Configuration Protocol) server present in the router of that network(if address allocation is set to automatic). This address to which the PC gets assigned is the private IP address.
The figure below shows two different networks having 3 hosts in each, which are connected to a switch.
Private IP address classes
Class A:
10.0.0.0
to10.255.255.255
Class B:
172.16.0.0
to172.31.255.255
Class C:
192.168.0.0
to192.168.255.255
All the hosts of a private network can utilize IP addresses from these classes, which are by the way not routable, as they are not unique.
Hit ipconfig/all
(this command is for Windows) to get the information about the IP address of all the network adapters of a PC.
192.168.0.3 is the private IP address. Default gateway🔑 192.168.0.1 is the local IP address of the router this PC is connected to.
Assume that both PC1 and PC2 have the same private IP address 🏠(192.168.0.3). Now, suppose that a client wishes to communicate with PC1, but PC2 also has the same address. Here emerges the concept of a public IP address📬.
The ISP assigns a public IP address to every network. Using this IP address, hosts on different networks communicate.
Hit curl ifconfig.me
to know about the public IP address.
---------------------------------------------------------------------------------------
A simple analogy to understand private IP and public IP address
192.168.0.3 is the Apartment Number🏢 of PC1(house)(say 410) in the Apartment (Beverly Towers). Similarly, PC2 also has an apartment number🏢(say 410) in another apartment (say Malabar Towers), So, here the apartment number is the private IP address and the apartment name is the public IP address.
---------------------------------------------------------------------------------------
The router has the public IP address along with the local IP address which will be shown as the default gateway for the PCs connected to that router(subnet).
The sequence of steps to form a connection between PC1 and PC2:-
First, the PC1 generates a packet that it wants to send to PC2 which consists of the source and destination IP address and MAC address. This packet is sent to the router it is connected to.
The router then checks if it has the entries of the destination IP address and MAC address in its MAC address table and ARP table.
The router then compares the destination IP address with its subnet to check if the destination IP address is in the same network or not. If the destination is on another network it then forwards the packet to the next router that is connected to this router.
ARP(Address Resolution Protocol)
The purpose of this protocol is to map layer 3 addresses (IP addresses) to layer 2 addresses (IP addresses). Every host in a network has an ARP table which consists of IP addresses mapped to MAC addresses.
If the host doesn't have any information about the MAC address of the other host, it sends an ARP request to all the hosts of that network. (ARP request is accessible to the hosts of that network only), the host which has the matching IP address as of the ARP request, replies by sending an ARP reply which contains it MAC address. The first host then adds this information to its ARP table.
ARP table can be seen on the windows machine with arp -a
command.
Subscribe to my newsletter
Read articles from Aniruddha Balbudhe directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aniruddha Balbudhe
Aniruddha Balbudhe
I am a software developer from India proficient in computer networking and C++.