A subnet mask is a basis for determining whether the IP addresses of any two computers belong to the same subnet. The subnet mask is the same as the IP address structure, which is a 32-bit binary number, where the network number section is all "1" and the host number is all "0". Use the subnet mask to determine whether two hosts are in the same subnet. If the IP addresses of the two hosts are the same as the result of their subnet mask, respectively, the two hosts are in the same subnet. Example: I P Address 192.168.0.1 Subnet Mask 255.255.255.0 Convert to binary for operation: I P Address 11010000.10101000.00000000.00000001 Subnet Mask 11111111.11111111.11111111.00000000 and operations 11000000.10101000.00000000.00000000 After conversion to decimal: 192.168.0.0 I P Address 192.168.0.254 Subnet Mask 255.255.255.0 Convert to binary for operation:
I P Address 11010000.10101000.00000000.11111110 Subnet Mask 11111111.11111111.11111111.00000000 and operations 11000000.10101000.00000000.00000000 After conversion to decimal: 192.168.0.0 By using the and operation of the IP address of the two computers and the subnet mask above, we can see that the result of the operation is the same. are 192.168.0.0, so these two computers can be considered to be the same subnet. /* * Function: To determine the IP address of two computers is the same sub-network. * Input Parameters: String Mask: netmask, Format: "255.255.255.0"; * String ip1: Computer 1 IP address, format: "192.168.0.254"; * String IP2: Computer 2 IP address, format: "192.168.0.1"; * * Return value: 0:ip1 and IP2 belong to the same subnet; 1:ip address or subnet mask format is illegal; 2:ip1 and IP2 do not belong to the same subnet */ public int Checknetsegment (string mask, String ip1, String ip2) { /* Implement function here */ return 0; } |