IP addresses are divided into common addresses and special addresses. Most of the IP addresses used in the previous article are common IP addresses. This article describes how to use the ten methods provided by the inetaddress class to determine whether an IP address is a special IP address.
1. isanylocaladdress Method
If the IP address is a wildcard address, true is returned. Otherwise, false is returned. This wildcard address is often used by computers with multiple network interfaces (such as two NICS. The wildcard address can be used to allow clients from any network interfaces to connect to the server host. The IPv4 wildcard address is 0.0.0.0. The wildcard address of IPv6 is 0: 0: 0: 0: 0: 0: 0: 0 ::.
2. isloopbackaddress Method
If the IP address is a loopback address, true is returned. Otherwise, false is returned. The loopback address is the IP address of the local machine. The range of IPv4 loopback addresses is 127.0.0.0.
~ 127.20.255, that is, as long as the first byte is 127, It is the lookback address. For example, 127.1.2.3 and 127.0.200.200 are loopback addresses. The IPv6 loopback address is 0: 0: 0: 0: 0: 0: 0: 1. We can use the ping command to test the lookback address. The following command line is shown:
ping 127.200.200.200
Running result:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Ping statistics for 127.200.200.200: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms
Although 127.255.255.255 is also the loopback address, 127.255.255.255 cannot be pinged in windows. This is because 127.255.255.255 is a broadcast address, and does not respond to the requests sent to the broadcast address in windows. In other operating systems, different results may be obtained based on the settings.
Appendix:
What is the loopback address? How is it used? This type of interface is the most widely used virtual interface, which is used on almost every vro. It is commonly used for the following purposes.
(1) management address of a vro
After the system administrator completes the network planning, a loopback interface is created for each vro for convenient management, and an IP address is separately specified as the management address on the interface, the Administrator will use this address to remotely log on to the vrotelnet (Telnet). This address actually provides a function similar to a device name.
But there are usually many interfaces and addresses on each vro. Why don't you choose one of them? The reason is as follows: Because the Telnet command uses TCP packets, the following situation may occur: A router interface is down due to a fault, but other interfaces can still be telnet, that is, the TCP connection to this vro still exists. Therefore, the selected telnet address must never be down, and the virtual interface meets this requirement. Because this type of interface does not require interconnection with the peer end, the address of the loopback interface is usually specified as a 32-bit mask to save address resources.
(2) Use this interface address as the router ID of the Dynamic Routing Protocol OSPF and BGP
During the operation of the Dynamic Routing Protocol OSPF and BGP, you must specify a router ID for the Protocol, which is the unique identifier of the router and must be unique within the autonomous system. The router ID is a 32-bit unsigned integer, which is very similar to the IP address. In addition, the IP address is not repeated, so the router ID is usually specified as the address of an interface on the device. Because the IP address of the loopback interface is generally regarded as the router ID, it becomes the best choice for the router ID.
(3) Use this interface address as the source address of the bgp tcp connection
In the BGP protocol, two routers running BGP establish a neighbor relationship through TCP. When configuring a neighbor, the loopback interface is often specified as the source address for establishing a TCP connection (generally only for ibgp, for the same reason as 2.1, to enhance the robustness of the TCP connection)
3. islinklocaladdress Method
Returns true if the IP address is a local connection address (linklocaladdress); otherwise, returns false. The local connection address of IPv4 ranges from 169.254.0.0.
~ 169.254.255.255. The first 12 bits of the IPv6 local connection address are fe8, and other bits can be any value. For example, fe88 ::, fe80 :: ABCD :: are all local connection addresses.
4. issitelocaladdress Method
Returns true if the IP address is a local IP address (sitelocaladdress); otherwise, returns false. The local address of IPv4 is divided into three parts: 10.0.0.0
~ 10.20.255.255, 172.16.0.0 ~ 172.31.255.255, 192.168.0.0 ~ 192.168.255.255. The first 12 digits of the local address in the IPv6 region are FEC, and other digits can be any value, such as fed0 ::, fef1 :: are all local addresses in the region.
5. ismulticastaddress Method
If the IP address is a broadcast address (multicastaddress), true is returned. Otherwise, false is returned. A broadcast address can send messages to all computers in the network, rather than only one specific computer. The range of IPv4 broadcast addresses is 224.0.0.0.
~ 239.00000000255. The first byte of the IPv6 broadcast address is ff, and other bytes can be any value. Details about broadcast addresses will be discussed in a later chapter.
6. ismcglobal Method
Returns true if the IP address is a global broadcast address; otherwise, returns false. Global broadcast addresses can send messages to all computers on the Internet. Besides 224.0.0.0 and the IP address with the first byte being 239, the IPv4 broadcast address is a global broadcast address. In the global broadcast address of IPv6, the first byte is ff, and the second byte is 0e ~ Fe, other bytes can be any value, such as ffbe:, ff0e: Are global broadcast addresses.
7. ismclinklocal Method
Returns true if the IP address is a subnet broadcast address; otherwise, returns false. The broadcast address of the subnet can only send information to computers in the subnet. The subnet broadcast address of IPv4 ranges from 224.0.0.0.
~ 224.0.0.255. The first byte of the subnet broadcast address of IPv6 is ff, and the second byte range is 02 ~ F2, other bytes can be any value, such as ffb2:, ff02: ABCD: Are all subnet broadcast addresses.
VIII. ismcnodelocal Method
Returns true if the IP address is the broadcast address of the Local interface. Otherwise, returns false. The broadcast address of the Local interface cannot send the broadcast information to the network interface that generates the broadcast information, even if it is another network interface of the same computer. All IPv4 broadcast addresses are not local interface broadcast addresses. The first byte of the broadcast address of the local IPv6 interface is ff, and the second byte ranges from 01 to 01 ~ F1, other bytes can be any value, such as ffb1:, ff01: A123: all are local interface broadcast addresses.
IX. ismcorglocal Method
If the IP address is a broadcast address within the organizational unit, true is returned. Otherwise, false is returned. You can use an organizational scope broadcast address to send broadcast information to all computers in the company or enterprise. The first byte of the IPv4 organizational range broadcast address is 239, the second byte is not less than 192, and the third byte is not greater than 195. For example, 239.193.100.200 and 239.192.195.0 are organizational range broadcast addresses. The first byte of the IPv6 organizational scope broadcast address is ff, and the second byte range is 08 ~ Other bytes can be any value, such as ff08: And ff48: are broadcast addresses within the organizational unit.
10. ismcsitelocal Method
Returns true if the IP address is a broadcast address within the site range; otherwise, returns false. You can send broadcast information to a computer within the site range by using a broadcast address within the site range. The IPv4 site range broadcast address range is 239.255.0.0 ~ 239.00000000255, such as 239.00001.1 and 239.00000.0 are site-wide broadcast addresses. The first byte of the IPv6 Site range broadcast address is ff, and the second byte range is 05 ~ F5, other bytes can be any value, such as ff05:, ff45: are site-range broadcast addresses.
The following code determines whether an IP address is within the range of the preceding 10 address types:
Package test; import java.net. *; import Java. lang. reflect. *; public class mynet {public static void main (string [] ARGs) throws exception {If (ARGs. length = 0) return; inetaddress address = inetaddress. getbyname (ARGs [0]); Method methods [] = inetaddress. class. getmethods (); // method that starts with is and does not have parameters for (method: Methods) {If (method. getname (). matches ("is. * ") & method. getparametertypes (). length = 0) {If (Boolean. parseboolean (method. invoke (address ). tostring () system. out. println (method. getname () + "= true ");}}}}
Test 1
Run the following command:
java test.MyNet 224.0.0.1
Running result:
isMCLinkLocal = trueisMulticastAddress = true
Test 2
Run the following command:
java test.MyNet FFB1::
Running result:
isMCNodeLocal = trueisMulticastAddress = true
If no result is output, the specified IP address does not attribute the range of the preceding 10 IP address types, but is a common IP address.