An instance object of inetaddress contains an IP address that is saved as a number, and may also contain the hostname (if the host name is used to obtain an instance of InetAddress, or is constructed with a number, and the ability to reverse hostname resolution is enabled). The InetAddress class provides methods for resolving host names to IP addresses (or vice versa). The method that generates the inetaddress object (getlocalhost (),getbyname (), Getallbyname (),getbyaddress ( ), etc.)
1 Importjava.net.UnknownHostException;2 3 /*4 * To-change the license header, choose License Headers in Project Properties.5 * To change this template file, choose Tools | Templates6 * and open the template in the editor.7 */8 Importjava.net.*;9 ImportJava.util.*;Ten /** One * A * @authorSilianbo - */ - Public classInetaddressdemo { the Public Static voidMain (string[] args)throwsunknownhostexception{ - //Add the relevant code snippet here - - } + -}
Get Local Host information
1 inetaddress localaddress = inetaddress.getlocalhost (); 2 System.out.println (localaddress);
View Code
Specify information for the domain host
String host = "www.google.com"; inetaddress address = Inetaddress.getbyname (host); SYSTEM.OUT.PRINTLN (address);
Get all information based on the specified domain name
String host = "www.baidu.com"; Note All IP addresses obtained inetaddress [] addresses = Inetaddress.getallbyname (host); for (InetAddress address:addresses) SYSTEM.OUT.PRINTLN (address);
Compare according tolocalhostand the computer name to get information different
String host = "localhost"; Change localhost to the computer name you are using now, and view the different inetaddress ia = inetaddress.getbyname (host); System.out.println ("Canonical Host Name =" + Ia.getcanonicalhostname ()); System.out.println ("Host Address =" + ia.gethostaddress ()); System.out.println ("Host Name =" + Ia.gethostname ()); System.out.println ("is Loopback Address =" + ia.isloopbackaddress ());
Get Local Host AllIPAddress
Enumeration<networkinterface> netinterfaces = null; try { netinterfaces = networkinterface.getnetworkinterfaces (); while (Netinterfaces.hasmoreelements ()) { NetworkInterface ni = netinterfaces.nextelement (); System.out.println ("DisplayName:" + ni.getdisplayname ()); System.out.println ("Name:" + ni.getname ()); enumeration<inetaddress> ips = ni.getinetaddresses (); while (Ips.hasmoreelements ()) { System.out.println ("IP:" + ips.nextelement (). gethostaddress ());}}} catch (Exception e) { e.printstacktrace (); }
Run:DisplayName:Software Loopback Interface 1name:loip:127.0.0.1ip:0:0:0:0:0:0:0:1displayname:microsoft Kernel Debug Network adapter Name:eth0DisplayName:Qualcomm Atheros ar9485wb-eg Wireless Networks ADAPTERNAME:WLAN0IP:FE80:0:0:0:21C7 : 474b:810b:997d%wlan0displayname:realtek PCIe GBE Series Controller name:eth1ip:10.128.195.6ip:fe80:0:0:0:21f7:f5fa:71a7:e3d1% Eth1displayname:bluetooth Equipment (personal area Network) Name:eth2ip:fe80:0:0:0:5d4a:7ad0:3b6f:29af%eth2displayname:bluetooth equipment ( RFCOMM protocol TDI) Name:net0DisplayName:Microsoft Wi-Fi Direct virtual adapter Name:wlan1DisplayName:Microsoft ISATAP AdapterName: Net1ip:fe80:0:0:0:0:5efe:a80:c306%net1displayname:teredo Tunneling Pseudo-interfacename:net2ip:2001:0:9d38:90d7 : 149e:221a:8adf:279aip:fe80:0:0:0:149e:221a:8adf:279a%net2displayname:wan Micro Port (SSTP) Name:net3DisplayName:WAN Miniport (L2TP) Name:net4DisplayName:WAN mini-port (PPPOE) Name:ppp0DisplayName:WAN Miniport (PPTP) Name:net5DisplayName:WAN Miniport (IKEV2) Name:net6DisplayName:WAN Micro port (IP) Name:eth3DisplayName:WAN Miniport (IPV6) Name:eth4DisplayName:WAN Miniport ( Network Monitor) Name:Eth5displayname:ras Sync Adapter Name:ppp1DisplayName:Microsoft ISATAP Adapter #2Name: net7ip:fe80:0:0:0:0:5efe:c0a8:3401% Net7displayname:microsoft ISATAP Adapter #3Name: Net8ip:fe80:0:0:0:0:5efe:c0a8:c701%net8displayname:microsoft Managed network Virtual adapter Name:wlan2ip:fe80:0:0:0:6c29:b7c6:d687:691c%wlan2displayname:vmware Vsan Ethernet Adapter for Vmnet1name:eth6ip:192.168.52.1ip:fe80:0:0:0:20d1:2461:f68:f35%eth6displayname:vmware Virtual Ethernet Adapter for Vmnet8name:eth7ip:192.168.199.1ip:fe80:0:0:0:1859:4837:4f9e:32dd%eth7displayname:microsoft ISATAP Adapter #4Name: Net9ip:fe80:0:0:0:0:5efe:a0b:b6ed%net9displayname:microsoft Wi-Fi Direct virtual adapter #2Name: Wlan3displayname:microsoft ISATAP Adapter #5Name: Net10displayname:microsoft Wi-Fi Direct virtual Adapter #3Name: Wlan4displayname:microsoft Wi-Fi Direct Virtual adapter #4Name: Wlan5ip:fe80:0:0:0:95a5:ed42:ab58:12c4%wlan5displayname:realtek PCIe GBE Series Controller-WFP Native MAC Layer Lightweight Filter-0000name:eth8displayname:realtek PCIe GBE series Controller-liebao Wifi NAT driver-0000name:eth9dIsplayname:realtek PCIe GBE Series Controller-pppoe performance Enhancer-0000name:eth10displayname:realtek PCIe GBE series Controller-qos Packet Scheduler-0000name:eth11displayname:realtek PCIe GBE Series Controller-WFP 802.3 MAC Layer lightweight filter-0000name: Eth12displayname:qualcomm Atheros ar9485wb-eg Wireless Network ADAPTER-WFP Native MAC Layer lightweight filter-0000name: Wlan6displayname:qualcomm Atheros ar9485wb-eg Wireless Network adapter-virtual WiFi Filter driver-0000name: Wlan7displayname:qualcomm Atheros ar9485wb-eg Wireless Network adapter-native WiFi Filter driver-0000name: Wlan8displayname:qualcomm Atheros ar9485wb-eg Wireless Network adapter-liebao Wifi NAT driver-0000name: Wlan9displayname:qualcomm Atheros ar9485wb-eg Wireless Network adapter-pppoe performance enhancer-0000name: Wlan10displayname:qualcomm Atheros ar9485wb-eg Wireless Network adapter-qos Packet scheduler-0000name: Wlan11displayname:qualcomm Atheros ar9485wb-eg Wireless Network ADAPTER-WFP 802.3 MAC Layer lightweight filter-0000name: wlan12dIsplayname:microsoft Managed Network Virtual adapter-WFP Native MAC Layer Lightweight Filter-0000name:wlan13displayname:microsoft Managed network virtual adapter-native WiFi Filter driver-0000name:wlan14displayname:microsoft managed network virtual adapter-liebao WiFi NAT driver-0000name: Wlan15displayname:microsoft Managed Network Virtual adapter-pppoe performance Enhancer-0000name:wlan16displayname:microsoft Managed network Virtual Adapter-qos Packet Scheduler-0000name:wlan17displayname:microsoft Managed Network virtual adapter-WFP 802.3 MAC Layer Lightweight Filter-0000name:wlan18displayname:microsoft Wi-Fi Direct virtual adapter #4-WFP Native MAC Layer lightweight filter-0000name: Wlan19displayname:microsoft Wi-Fi Direct virtual adapter #4-native WiFi Filter driver-0000name:wlan20displayname:microsoft Wi-Fi Direct Virtual Adapter #4-liebao Wifi NAT driver-0000name:wlan21displayname:microsoft Wi-Fi Direct virtual adapter #4-pppoe Performance Enhan Cer-0000name:wlan22displayname:microsoft Wi-Fi Direct virtual adapter #4-qos Packet scheduler-0000name:wlan23displayname: Microsoft Wi-Fi Direct virtual adapter #4-WFP 802.3 MAC Layer Lightweight filter-0000name:wlan24displayname:wAn micro port (IP)-WFP Native MAC Layer Lightweight Filter-0000name:eth13displayname:wan Miniport (IP)-liebao Wifi NAT Driver-0000name:eth14displayname:wan Micro Port (IP)-pppoe performance Enhancer-0000name:eth15displayname:wan Miniport (IP)- QoS Packet Scheduler-0000name:eth16displayname:wan Mini-port (IPv6)-WFP Native MAC Layer lightweight filter-0000name: Eth17displayname:wan Mini Port (IPv6)-liebao Wifi NAT Driver-0000name:eth18displayname:wan Miniport (IPV6)-pppoe performance Enhancer-0000name:eth19displayname:wan Mini-port (Network Monitor)-WFP Native MAC Layer Lightweight Filter-0000name:eth20displayname : WAN Miniport (IPV6)-qos Packet Scheduler-0000name:eth21displayname:wan Mini-port (Network Monitor)-liebao Wifi NAT driver-0000name: Eth22displayname:wan Mini port (Network Monitor)-pppoe Performance Enhancer-0000name:eth23displayname:wan Miniport (Network Monitor)-QoS Packet Scheduler-0000name:eth24displayname:chinanetsnwidename:ppp2ip:10.11.182.237build SUCCESSFUL (Total time:1 second)
UnderIPAddress Constructioninetaddress
byte [] IP = new byte[] {(byte) 202, (byte) 117, (Byte), 7}; You can change the value over 255InetAddress Address1 = inetaddress.getbyaddress (IP); inetaddress address2 = Inetaddress.getbyaddress (" www.baidu.com ", IP); System.out.println (ADDRESS1); System.out.println (ADDRESS2);
UseInetaddress.getbyname ()by computer name all the on-LAN host name andIP
for (int i = 0; I <=; i++) { int s = i + 1; String host = "192.168.52." + S; try { inetaddress a = inetaddress.getbyname (host); System.out.println ("The IP is:" + a.gethostaddress ()); System.out.println ("The LocalHost is :" + a.gethostname ()); } catch (Unknownhostexception e) { e.printstacktrace (); } }
UseInetaddress.getbyname ()PressIPaddress Gets the specified network segment all power-on host name/Domain name andIPaddress, combined methodisreachable ()
for (int i = 0; I <= 155; i++) { int s = i + +; String host = "192.168.52." + S; try { inetaddress a = inetaddress.getbyname (host); System.out.println ("The IP is:" + a.gethostaddress ()); System.out.println ("The LocalHost is :" + a.gethostname ()); A.isreachable (+); } catch (Unknownhostexception e) { } }
InetAddress () related operation is so much!
InetAddress Related Notes