Java network programming from getting started to mastering (9): Using the Isxxx method to determine the address type

Source: Internet
Author: User

IP addresses are divided into ordinary addresses and special addresses. Most of the previous articles used are ordinary IP addresses, and in this article we will describe how to use the 10 methods provided by the InetAddress class to determine whether an IP address is a special IP address.

First, isanylocaladdress Method

Returns True when the IP address is a wildcard address, otherwise false. This wildcard address is very much owned by computers that have multiple network interfaces, such as two NICs. You can use a wildcard address to allow client connections from any network interface to be accepted on the server host. The wildcard address for the IPV4 is 0.0.0.0. The IPV6 wildcard address is 0:0:0:0:0:0:0:0 and can also be simply written as:.

Second, isloopbackaddress Method

Returns true when the IP address is an loopback address, otherwise false. The loopback address is the IP address that represents the native computer . The range of loopback addresses for IPV4 is 127.0.0.0 ~ 127.255.255.255, that is, if the first byte is 127, it is the Lookback address. such as 127.1.2.3, 127.0.200.200 are loopback address. IPV6 loopback address is 0:0:0:0:0:0:0:1, can also be written as:: 1. We can use the ping command to test the Lookback address. As shown in the following command line:

Ping 127.200.200.200

Operation Result:

Reply from 127.0.0.1:bytes=32 time<1ms ttl=128 Reply from 127.0.0.1:bytes=32 time<1ms ttl=128 Reply from 127.0.0.1 : bytes=32 time<1ms ttl=128 Reply from 127.0.0.1:bytes=32 time<1ms ttl=128
Ping statistics for 127.200.200.200:packets:sent = 4, Received = 4, Lost = 0 (0% loss), approximate round trip t IMEs in milli-seconds:minimum = 0ms, Maximum = 0ms, Average = 0ms

Although 127.255.255.255 is also an loopback address, 127.255.255.255 is not able to ping through windows. This is because 127.255.255.255 is a broadcast address that does not respond to requests to broadcast addresses under Windows, and may have different results depending on the settings on other operating systems.

Third, islinklocaladdress Method

returns True when the IP address is the local connection address (linklocaladdress), otherwise false. The range of local connection addresses for IPV4 is 169.254.0.0 ~ 169.254.255.255. The first 12 bits of the IPV6 local connection address are FE8, and the other bits can be arbitrary values, such as FE88::, FE80::ABCD:: are local connection addresses.

Four, issitelocaladdress Method

Returns true when the IP address is a regional local address (sitelocaladdress), otherwise false is returned. IPV4 address Local address is divided into three segments: 10.0.0.0 ~ 10.255.255.255, 172.16.0.0 ~ 172.31.255.255, 192.168.0.0 ~ 192.168.255.255. The first 12 bits of the local address of the IPV6 are FEC, and the other bits can be arbitrary values, such as FED0::, FEF1:: All are regional local addresses.

Five, ismulticastaddress Method

returns True when the IP address is a broadcast address (multicastaddress), otherwise false. Broadcast addresses enable you to send information to all computers on your network, rather than just to a specific computer. The range of broadcast addresses for IPV4 is 224.0.0.0 ~ 239.255.255.255. IPV6 broadcast address the first byte is FF, and the other bytes can be any value. Details of the broadcast address will be discussed in a later section.

Six, Ismcglobal Method

Returns True when the IP address is a global broadcast address, otherwise false. A worldwide broadcast address can send information to all computers on the Internet. IPV4 's broadcast address is a global broadcast address in addition to the 224.0.0.0 and the first byte of the IP address that is 239. The first byte in the global broadcast address of IPV6 is FF, the second byte is 0E ~ FE, and the other bytes can be any value, such as Ffbe::, ff0e:: All broadcast addresses worldwide.

Seven, ismclinklocal Method

returns True when the IP address is a subnet broadcast address, otherwise false. Broadcast addresses that use subnets can only send information to computers within subnets. The range of subnet broadcast addresses for IPV4 is 224.0.0.0 ~ 224.0.0.255. The first byte of the IPV6 subnet broadcast address is FF, the second byte is in the range of F2, and the other bytes can be any value, such as FFB2::, FF02:ABCD:: All subnet broadcast addresses.  

Eight, ismcnodelocal Method

Returns true when the IP address is the local interface broadcast address, otherwise false is returned. The local interface broadcast address cannot send broadcast information to the network interface that generated the broadcast information, even if it is another network interface on the same computer. All IPV4 broadcast addresses are not local interface broadcast addresses. The first byte of the IPV6 local interface broadcast address is FF, the second section is the range of F1, and the other bytes can be any value, such as FFB1::, ff01:a123:: Both are local interface broadcast addresses.

Nine, ismcorglocal Method

Returns ture when the IP address is an organization-wide broadcast address, otherwise false. Use an organization-wide broadcast address to send broadcast information to all computers within a company or enterprise. The first byte of the IPV4 's organization-wide broadcast address is 239, the second byte is not less than 192, and the third byte is not greater than 195, such as 239.193.100.200, 239.192.195.0 are organization-wide broadcast addresses. IPv6 the first byte of an organization-wide broadcast address is FF, the second byte is scoped to F8, and the other bytes can be any value, such as FF08::, FF48:: All are organization-wide broadcast addresses.

10. ismcsitelocal Method

Returns true when the IP address is a site-wide broadcast address, otherwise false. With a site-wide broadcast address, you can send broadcast information to a site-wide computer. The range of IPV4 's site-wide broadcast addresses is 239.255.0.0 ~ 239.255.255.255, such as 239.255.1.1, 239.255.0.0 are site-wide broadcast addresses. The first byte of the IPV6 site-wide broadcast address is FF, the second byte is in the range of ~ F5, and the other bytes can be any value, such as FF05::, FF45:: All are site-wide broadcast addresses.

The following code can determine whether an IP address is within the range of the above 10 address types:

PackageTest
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 ();         //  methods that start with IS and have no parameters          for  (method method : methods)         {             if  (Method.getname (). Matches ("is. *")&& method.getparametertypes (). Length = = 0) {if (Boolean.parseboolean (Method.invoke (address). ToString ())) System.out.println (meth             Od.getname () + "= true"); }         }     } }
    • Test 1

Execute the following command:

Java test. MyNet224.0.0.1

Operation Result:

Ismclinklocal = True Ismulticastaddress = True
    • Test 2

Execute the following command:

Java test. MyNet FFB1::

Operation Result:

Ismcnodelocal = True Ismulticastaddress = True

If no result is output, the specified IP address does not attribute the range of the 10 IP address types mentioned above, just a normal IP address.

Java network programming from getting started to mastering (9): Using the Isxxx method to determine the address type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.