Java to determine whether the IP address is intranet IP or public network IP

Source: Internet
Author: User


In the TCP/IP protocol, three IP address zones are reserved for private addresses with the following address ranges:

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:


Public Static BooleanInternalip(String IP) {byte[]Addr= Ipaddressutil.textToNumericFormatV4(IP);returnInternalip(Addr);}Public Static BooleanInternalip(byte[] addr) {Final byteB0= addr[0];Final byteB1= addr[1];    //10.X.X.X/8    Final bytesection_1=0x0A;    //172.16.X.X/12    Final bytesection_2= (byte)0xAC;Final byteSection_3= (byte)0x10;Final byteSection_4= (byte)0x1F;    //192.168.X.X/16    Final bytesection_5= (byte)0xC0;Final byteSection_6= (byte)0xa8;Switch(B0) { Casesection_1:return true; Casesection_2:if(B1>=Section_3&&B1<=Section_4) {return true;            } Casesection_5:Switch(B1) { CaseSection_6:return true;            }default:return false;    }}

Java to determine whether the IP address is intranet IP or public network IP

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.