Several methods of obtaining IP address in Java

Source: Internet
Author: User
Tags get ip

Here are some ideas on how to get IP addresses in Java.

1, directly using the Java.net.InetAddress class to obtain, but this method is only valid in the Windows environment, in the Linux environment can only obtain the localhost address (that is, the/etc/hosts file content)

The code is as follows:

1     Importjava.net.InetAddress;2 3     /**4 * This method works on Windows system.5 * in Linux system it returns 127.0.0.1 the content of the the Hosts file.6      */7      Public Static voidgetipaddressinwindows () {8         Try {9InetAddress address =inetaddress.getlocalhost ();TenSystem.out.println ("Host Name:" +address.gethostname ()); OneSystem.out.println ("Host Address:" +address.gethostaddress ()); A  -}Catch(unknownhostexception e) { - e.printstacktrace (); the         } -}

2, the method that can work normally under Linux, the code is as follows:

Import java.net.Inet4Address;
Import java.net.InetAddress;
Import Java.net.NetworkInterface;
Import java.net.SocketException;
Import java.net.UnknownHostException;
Import java.util.Enumeration;

    /*** This method is used-get all IP addresses from the network interfaces. * Network Interfaces:eth0, Wlan0, L0, Vmnet1, Vmnet8*/     Public Static voidgetallipaddress () {Try {            //Get all network interfaceEnumeration<networkinterface> allnetworkinterfaces =networkinterface.getnetworkinterfaces (); NetworkInterface NetworkInterface=NULL; //Check if there is more than one network interface             while(Allnetworkinterfaces.hasmoreelements ()) {//Get Next network interfaceNetworkInterface =allnetworkinterfaces.nextelement (); //output interface ' s nameSYSTEM.OUT.PRINTLN ("Network interface:" +networkinterface.getdisplayname ()); //get all IP address the bound to this network interfaceEnumeration<inetaddress> allinetaddress =networkinterface.getinetaddresses (); InetAddress ipAddress=NULL; //Check if there is more than one IP addresses//Band to one network interface                 while(Allinetaddress.hasmoreelements ()) {//Get Next IP addressIpAddress =allinetaddress.nextelement (); if(IpAddress! =NULL&& ipAddressinstanceofinet4address) {System.out.println ("IP Address:" +ipaddress.gethostaddress ()); }                }            }                    } Catch(SocketException e) {e.printstacktrace (); }    }//End Method Getallipaddress                            

Above this method some shortcomings, it will output all the network card IP address, sometimes we only need one or a few separate network card IP, can be obtained by the following methods:

1

Import java.net.Inet4Address;
Import java.net.InetAddress;
Import Java.net.NetworkInterface;
Import java.net.SocketException;
Import java.net.UnknownHostException;
Import java.util.Enumeration;

       /**2 * This method was used to get the IP address by network interface ' s name.3      * @paramNetworkinterfacename Network interface ' s name4      * @returnreturn True if get IP address successfully,5 * Otherwise return false.6      */7      Public Static Booleangetipaddrbyname (String networkinterfacename) {8         Try {9           //Get network interface by nameTenNetworkInterface NetworkInterface = One Networkinterface.getbyname (networkinterfacename); A             if(NetworkInterface = =NULL) { -                 return false; -             } theSYSTEM.OUT.PRINTLN ("Network interface:" + - networkinterface.getdisplayname ()); -              -InetAddress ipAddress =NULL; +             //get all IP addresses band to this interface -Enumeration<inetaddress> addresses =networkinterface.getinetaddresses (); +              A              while(Addresses.hasmoreelements ()) { atIpAddress =addresses.nextelement (); -                  -                 if(IpAddress! =NULL&& ipAddressinstanceofinet4address) { -SYSTEM.OUT.PRINTLN ("IP address:" + - ipaddress.gethostaddress ()); -                 } in             } -}Catch(SocketException e) { to e.printstacktrace (); +         } -          the         return true; *}//End Method Getipaddrbyname

Several methods of obtaining IP address in Java

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.