NetworkInterface get the host IP, judge the internal and external network

Source: Internet
Author: User

I. NetworkInterface concept

Java provides a NetworkInterface class. This class can get information about all the physical network interfaces of the machine and the logical network interfaces that the virtual machines and other software use to create the physical network interface of the machine.
Step One: NetworkInterface enumerates all of the network interfaces of the machine by means of the Getnetworkinterfaces method.

Step two: Traverse all network IPs, get native IP

Two. Java implementation

Private Staticstring Getlocalip () {string SIP=NULL; InetAddress IP=NULL; BooleanBfindip =false; Enumeration<NetworkInterface> netinterfaces =NULL; Try{netinterfaces= (enumeration<networkinterface>) networkinterface.getnetworkinterfaces (); } Catch(SocketException e) {e.printstacktrace (); }         while(Netinterfaces.hasmoreelements ()) {if(BFINDIP) { Break; } networkinterface ni=(NetworkInterface) netinterfaces.nextelement (); //----------specific situations, consider using Ni.getname to judge//Traverse all IPsenumeration<inetaddress> ips =ni.getinetaddresses ();  while(Ips.hasmoreelements ()) {IP=(inetaddress) ips.nextelement (); if(Ip.issitelocaladdress () &&!ip.isloopbackaddress ()//127. Start with Lookback address&& ip.gethostaddress (). IndexOf (":") = =-1) {Bfindip=true;  Break; }            }        }        if(IP! =NULL) {SIP=ip.gethostaddress (); }        returnsip; }

Three. js distinguish inside and outside the network

functionGetipnum (ipAddress) {varIP =NewArray (); IP= Ipaddress.split ("."); varA = parseint (ip[0]); varb = parseint (ip[1]); varc = parseint (ip[2]); varD = parseint (ip[3]); returnA * * * * * * * * * * * * * * * + + c * 256 +D; }//is the intranetfunctionIsinnerip (IP) {varA1 = Getipnum ("10.0.0.0"); varA2 = Getipnum ("10.255.255.255"); varB1 = Getipnum ("172.16.0.0"); varB2 = Getipnum ("172.31.255.255"); varC1 = Getipnum ("192.168.0.0"); varC2 = Getipnum ("192.168.255.255"); varD1 = Getipnum ("10.44.0.0"); varD2 = Getipnum ("10.69.0.255"); varn =getipnum (IP); return(n >= A1 && n <= a2) | | (N >= b1 && n <= b2) | | (n >= C1 && n <= c2) | | (n >= d1 && n <=D2);}/** * * * intercept URL Request parameters * * @param paras * @returns*/functionGetrequesturl (paras) {varURL =Location.href; varparastring = url.substring (Url.indexof ("?") + 1, url.length). Split ("&"); varParaobj = {}     for(i = 0; j = parastring[i]; i++) {paraobj[j.substring (0, J.indexof ("=")). toLowerCase ()] =j.substring (J. IndexOf ("=") + 1, j.length); }    varReturnValue =paraobj[paras.tolowercase ()]; if(typeof(returnvalue) = = "Undefined") {        return""; } Else {        returnreturnvalue; }}

  

NetworkInterface get the host IP, judge the internal and external network

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.