Java Judge IP address for intranet IP or public network IP method _java

Source: Internet
Author: User

This article describes the Java to determine whether the IP address of the intranet IP or public network IP method. Share to everyone for your reference. The specific analysis is as follows:

In the TCP/IP protocol, three IP address areas are reserved for private addresses, and their addresses range as follows:

10.0.0.0/8:10.0.0.0~10.255.255.255
172.16.0.0/12:172.16.0.0~172.31.255.255
192.168.0.0/16:192.168.0.0~192.168.255.255

So, go directly to the code:

Copy Code code as follows:
public static Boolean internalip (String IP) {
byte[] addr = IPADDRESSUTIL.TEXTTONUMERICFORMATV4 (IP);
return Internalip (addr);
}

public static Boolean Internalip (byte[] addr) {
Final byte B0 = addr[0];
Final byte B1 = addr[1];
10.x.x.x/8
Final byte section_1 = 0x0A;
172.16.x.x/12
Final byte section_2 = (byte) 0xAC;
Final byte section_3 = (byte) 0x10;
Final byte Section_4 = (byte) 0x1F;
192.168.x.x/16
Final byte section_5 = (byte) 0xc0;
Final byte section_6 = (byte) 0xa8;
Switch (B0) {
Case SECTION_1:
return true;
Case Section_2:
if (B1 >= section_3 && B1 <= section_4) {
return true;
}
Case Section_5:
Switch (B1) {
Case Section_6:
return true;
}
Default
return false;
}
}

I hope this article will help you with your Java programming.

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.