Determines whether the specified IP address is an intranet IP address.

Source: Internet
Author: User

/*** Determine whether the given IP address is an intranet IP address ** @ author GaoHuanJie */public class Test {public boolean isInnerIP (String ipAddress) {/* determine whether the IP address is an intranet IP Address */boolean isInnerIp = false; // The default IP address is not an intranet IP address long ipNum = getIpNum (ipAddress);/*** private IP Address: class A 10.0.0.0-10.20.255.255 * Class B 172.16.0.0-255.* Class C 192.168.0.0-192.168.255.255 * Class D 127.0.0.0-127.255.255.255.255 (loop address) **/long aBegin = getIpNum ("10.0.0.0 "); long aEnd = getIpNum ("10.20.255.255"); long bBegin = getIpNum ("172.16.0.0"); long bEnd = getIpNum ("172.31.255.255"); long cBegin = getIpNum ("192.168.0.0 "); long cEnd = getIpNum ("192.168.255.255"); long dBegin = getIpNum ("127.0.0.0"); long dEnd = getIpNum ("plaintext"); isInnerIp = isInner (ipNum, aBegin, aEnd) | isInner (ipNum, bBegin, bEnd) | isInner (ipNum, cBegin, cEnd) | isInner (ipNum, dBegin, dEnd); return isInner Ip;} private long getIpNum (String ipAddress) {/* Number of IP addresses retrieved */String [] ip = ipAddress. split ("\\. "); long a = Integer. parseInt (ip [0]); long B = Integer. parseInt (ip [1]); long c = Integer. parseInt (ip [2]); long d = Integer. parseInt (ip [3]); long ipNum = a * 256*256*256 + B * 256*256 + c * 256 + d; return ipNum ;} private boolean isInner (long userIp, long begin, long end) {return (userIp >=begin) & (userI P <= end);} public static void main (String [] args) {if (new Test (). isInnerIP ("22.8.129.60") {// 172.16.0.119System.out.println ("this IP address is an intranet IP address! ");} Else {System. out. println (" this IP address is an Internet IP address! ");}}}

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.