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