Java Get network IP

Source: Internet
Author: User
Tags get ip

Java get the network IP, a shallow slightly ~

1 Importjava.net.InetAddress;2 Importjava.net.UnknownHostException;3 4 /**5 * Get network IP6 * 1, get the local IP address7 * 2, get the server name of the machine8 * 3, get the host IP address of the remote server9 * 4, get all the host IP of the remote serverTen  * One * Network programming: A * InetAddress is a class used to encapsulate IP address related information - * Getlocalhost () Create IP objects for localhost - * Getbyname (host) Gets the server host address object based on the domain name the * Getallbyname (host) creates an array of InetAddress objects for the specified set of hosts with the same name -  * - * Created by DSPs on 2/25/15. -  */ +  Public classInternetinfo { -  +     /* A * Get the Local host IP address at      */ -      Public Static voidGetlocalip () { -         //Create a local host IP address object -         Try { -InetAddress addr =inetaddress.getlocalhost (); -String hostaddr = addr.gethostaddress ();//Get IP Address inString hostName = Addr.gethostname ();//get Local machine name -SYSTEM.OUT.PRINTLN ("Local IP address:" +hostaddr); toSYSTEM.OUT.PRINTLN ("Local Machine name:" +hostName); +}Catch(unknownhostexception e) { - e.printstacktrace (); the         } *     } $ Panax Notoginseng     /** - * Obtain the IP of the host server according to the domain name the      * @paramhostName Domain name +      */ A      Public Static voidgetipbyname (String hostName) { the         Try { +             //Create host address objects based on domain name -InetAddress addr =Inetaddress.getbyname (hostName); $             //Get host IP $String hostaddr =addr.gethostaddress (); -System.out.println ("Domain name:" + hostName + "Host IP address:" +hostaddr); -}Catch(unknownhostexception e) { the e.printstacktrace (); -         }Wuyi     } the  -     /** Wu * Get all IP address of host based on domain name -      * @paramhostName Domain name About      */ $      Public Static voidgetallipbyname (String hostName) { -         Try { -inetaddress[] Addrs =Inetaddress.getallbyname (hostName); -string[] IPs =NewString[addrs.length]; ASystem.out.println ("Domain name:" + HostName + "of the host all IP address is:"); +              for(inti = 0; i < addrs.length; ++i) { theIps[i] =addrs[i].gethostaddress (); - System.out.println (Ips[i]); $             } the}Catch(unknownhostexception e) { the e.printstacktrace (); the         } the     } -  in     //Test the      Public Static voidMain (string[] args) { theString hostName = "www.baidu.com"; About Getlocalip (); the Getipbyname (hostName); the Getallipbyname (hostName); the     } +  -}

^_^

Java Get 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.