The most direct role of the subnet mask is to determine whether the IP address is within the same network segment as another IP address.
Here is a simple look at the basic configuration of IP on a computer
IP Address: 192.168.0.5
Subnet Mask: 255.255.255.0
Default gateway: 192.168.0.1
Example above, IP address, subnet mask, default gateway. If the top computer A (IP address 192.168.0.5) to the computer B (IP address for 192.168.0.22) to send data, first a will send data to the router, the router after the Judge B's address and a address in the same network segment, and then the router will send the data directly to B.
The router uses the subnet mask specifically to determine that the IP address is first replaced by the binary for the IP address and subnet mask, and then compared to one of the longest digits of the subnet mask.
First step: Convert to Binary
A's IP address: 11000000,10101000,00000000,00000101
Subnet Mask: 11111111,11111111,11111111,00000000
IP address of B: 11000000,10101000,00000000,00010110
Step two: Follow the subnet mask to compare the longest
Look at the top of the content, the subnet mask on the left a total of 24 digits to 1, that means that if the first 24 bits of the two IP addresses are the same, then the two IP addresses are in the same network segment, and I see the same address for a and b that I have red marks, which means that A and B are in the same network segment.
For another example, if the data for a address is sent to the C address, the IP address of C is 192.168.56.21
First step: Convert to Binary
A's IP address: 11000000,10101000,00000000,00000101
Subnet Mask: 11111111,11111111,11111111,00000000
C's IP address: 11000000,10101000,00111000,00010101
Step two: Follow the subnet mask to compare the longest
Look at the A and C above, as required by the subnet mask, if the first 24 digits of C and the first 24 digits of a are the same, then A and C is the same network segment, look at the address of the top C, I use blue to mark different digits, so that a and C are not in the same network segment, Routers can not directly send a to C data directly through a router sent to the past, so that the router will first forward a data to another router (one does not continue to send down), and then sent to C.
Problem extension:
One: The above example of the neutron screen mask is 255.255.255.0, then can the mesh mask to modify it, completely.
In the example of A and C above, if the mesh mask is changed to 255.255.0.0, look again.
A's IP address: 11000000,10101000,00000000,00000101
Subnet Mask: 11111111,11111111,00000000,00000000
C's IP address: 11000000,10101000,00111000,00010101
So that A and C are in the same network segment.
Second: Extended subnet
In a company or school, the network number has been allocated, according to the internal administrative structure, and then the network assigned to the subnet number.
For example: If a company host has been allocated to the network according to the 255.255.0.0 subnet mask to differentiate the host number, now because the company has two departments, want to be divided into two subnets according to the department, then you can simply use the subnet mask to divide. Now consider, there are two departments, according to the binary cooking, so long as there is a digit 0 and one to distinguish between.
According to 255.255.0.0 (11111111,11111111,00000000,00000000), the front of the 16-bit is the network number, according to the subnet mask is based on the longest match, then now in the 17-bit 0 and one-zone divided into two. Then you can divide the subnet mask for one department into 255.255.128.0 and the other to 255.255.0.0, and compare it with the binary.
255.255.0.0:11111111,11111111,00000000,00000000
255.255.128.0:11111111,11111111,10000000,00000000
So it's easy to divide the two departments.
Three: When routing, choose the longest one to match
Why do you choose the longest one to match the route, the reason is this: if it is in several network segments can be matched (or according to the subnet mask) to choose the longest, you can quickly find a match. If the shortest, then need to match the host more, there is a possibility that a router can not forward, but also to change another route, it is likely to cause packets in the network loop, and finally until the packet was discarded.
Four: the concept of gateways
The concept of the default gateway is mentioned in the starting example, and the concept of the gateway is first seen.
The gateway is essentially a network of IP addresses to other networks, and the IP address of the gateway is the IP address of the device with the routing function, According to the above 192.168.0.1 Gateway example, the gateway is a machine or a PC or a server its IP address is 192.168.0.0, this device has routing function. According to this theory, the IP of a device must be in the same network segment as its own gateway, which is necessary.
said the gateway, and then the default gateway, the default word is not too much explanation, here are examples: The gateway may be more than one, there is a gateway, network Guan and so on, the default gateway is to choose one of them as the default value.