标签:
InetAddress的实例对象包含以数字形式保存的IP地址,同时还可能包含主机名(如果使用主机名来获取InetAddress的实例,或者使用数字来构造,并且启用了反向主机名解析的功能)。InetAddress类提供了将主机名解析为IP地址(或反之)的方法。其生成InetAddress对象的方法(getLocalHost(),getByName(),getAllByName(),getByAddress()等)
1 import java.net.UnknownHostException; 2 3 /* 4 * To change this license header, choose License Headers in Project Properties. 5 * To change this template file, choose Tools | Templates 6 * and open the template in the editor. 7 */ 8 import java.net.*; 9 import java.util.*; 10 /** 11 * 12 * @author silianbo 13 */ 14 public class InetAddressDemo { 15 public static void main(String[] args) throws UnknownHostException{ 16 //此处添加相关的代码段 17 18 } 19 20 }
1 InetAddress localAddress = InetAddress.getLocalHost(); 2 System.out.println(localAddress);
String host = "www.google.com"; InetAddress address = InetAddress.getByName(host); System.out.println(address);
String host = "www.baidu.com"; //注意获得的所有IP地址 InetAddress [] addresses = InetAddress.getAllByName(host); for(InetAddress address : addresses) System.out.println(address);
String host = "localhost"; //更改localhost为你现在所使用计算机名,查看不同 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 ());
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 1 Name:lo IP:127.0.0.1 IP:0:0:0:0:0:0:0:1 DisplayName:Microsoft 内核调试网络适配器 Name:eth0 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter Name:wlan0 IP:fe80:0:0:0:21c7:474b:810b:997d%wlan0 DisplayName:Realtek PCIe GBE 系列控制器 Name:eth1 IP:10.128.195.6 IP:fe80:0:0:0:21f7:f5fa:71a7:e3d1%eth1 DisplayName:Bluetooth 设备(个人区域网) Name:eth2 IP:fe80:0:0:0:5d4a:7ad0:3b6f:29af%eth2 DisplayName:Bluetooth 设备(RFCOMM 协议 TDI) Name:net0 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 Name:wlan1 DisplayName:Microsoft ISATAP Adapter Name:net1 IP:fe80:0:0:0:0:5efe:a80:c306%net1 DisplayName:Teredo Tunneling Pseudo-Interface Name:net2 IP:2001:0:9d38:90d7:149e:221a:8adf:279a IP:fe80:0:0:0:149e:221a:8adf:279a%net2 DisplayName:WAN 微型端口(SSTP) Name:net3 DisplayName:WAN 微型端口(L2TP) Name:net4 DisplayName:WAN 微型端口(PPPOE) Name:ppp0 DisplayName:WAN 微型端口(PPTP) Name:net5 DisplayName:WAN Miniport (IKEv2) Name:net6 DisplayName:WAN 微型端口(IP) Name:eth3 DisplayName:WAN 微型端口(IPv6) Name:eth4 DisplayName:WAN 微型端口(网络监视器) Name:eth5 DisplayName:RAS 同步适配器 Name:ppp1 DisplayName:Microsoft ISATAP Adapter #2 Name:net7 IP:fe80:0:0:0:0:5efe:c0a8:3401%net7 DisplayName:Microsoft ISATAP Adapter #3 Name:net8 IP:fe80:0:0:0:0:5efe:c0a8:c701%net8 DisplayName:Microsoft 托管网络虚拟适配器 Name:wlan2 IP:fe80:0:0:0:6c29:b7c6:d687:691c%wlan2 DisplayName:VMware Virtual Ethernet Adapter for VMnet1 Name:eth6 IP:192.168.52.1 IP:fe80:0:0:0:20d1:2461:f68:f35%eth6 DisplayName:VMware Virtual Ethernet Adapter for VMnet8 Name:eth7 IP:192.168.199.1 IP:fe80:0:0:0:1859:4837:4f9e:32dd%eth7 DisplayName:Microsoft ISATAP Adapter #4 Name:net9 IP:fe80:0:0:0:0:5efe:a0b:b6ed%net9 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #2 Name:wlan3 DisplayName:Microsoft ISATAP Adapter #5 Name:net10 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #3 Name:wlan4 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4 Name:wlan5 IP:fe80:0:0:0:95a5:ed42:ab58:12c4%wlan5 DisplayName:Realtek PCIe GBE 系列控制器-WFP Native MAC Layer LightWeight Filter-0000 Name:eth8 DisplayName:Realtek PCIe GBE 系列控制器-Liebao Wifi NAT Driver-0000 Name:eth9 DisplayName:Realtek PCIe GBE 系列控制器-PPPoe Performance Enhancer-0000 Name:eth10 DisplayName:Realtek PCIe GBE 系列控制器-QoS Packet Scheduler-0000 Name:eth11 DisplayName:Realtek PCIe GBE 系列控制器-WFP 802.3 MAC Layer LightWeight Filter-0000 Name:eth12 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-WFP Native MAC Layer LightWeight Filter-0000 Name:wlan6 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-Virtual WiFi Filter Driver-0000 Name:wlan7 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-Native WiFi Filter Driver-0000 Name:wlan8 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-Liebao Wifi NAT Driver-0000 Name:wlan9 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-PPPoe Performance Enhancer-0000 Name:wlan10 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-QoS Packet Scheduler-0000 Name:wlan11 DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000 Name:wlan12 DisplayName:Microsoft 托管网络虚拟适配器-WFP Native MAC Layer LightWeight Filter-0000 Name:wlan13 DisplayName:Microsoft 托管网络虚拟适配器-Native WiFi Filter Driver-0000 Name:wlan14 DisplayName:Microsoft 托管网络虚拟适配器-Liebao Wifi NAT Driver-0000 Name:wlan15 DisplayName:Microsoft 托管网络虚拟适配器-PPPoe Performance Enhancer-0000 Name:wlan16 DisplayName:Microsoft 托管网络虚拟适配器-QoS Packet Scheduler-0000 Name:wlan17 DisplayName:Microsoft 托管网络虚拟适配器-WFP 802.3 MAC Layer LightWeight Filter-0000 Name:wlan18 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-WFP Native MAC Layer LightWeight Filter-0000 Name:wlan19 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-Native WiFi Filter Driver-0000 Name:wlan20 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-Liebao Wifi NAT Driver-0000 Name:wlan21 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-PPPoe Performance Enhancer-0000 Name:wlan22 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-QoS Packet Scheduler-0000 Name:wlan23 DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-WFP 802.3 MAC Layer LightWeight Filter-0000 Name:wlan24 DisplayName:WAN 微型端口(IP)-WFP Native MAC Layer LightWeight Filter-0000 Name:eth13 DisplayName:WAN 微型端口(IP)-Liebao Wifi NAT Driver-0000 Name:eth14 DisplayName:WAN 微型端口(IP)-PPPoe Performance Enhancer-0000 Name:eth15 DisplayName:WAN 微型端口(IP)-QoS Packet Scheduler-0000 Name:eth16 DisplayName:WAN 微型端口(IPv6)-WFP Native MAC Layer LightWeight Filter-0000 Name:eth17 DisplayName:WAN 微型端口(IPv6)-Liebao Wifi NAT Driver-0000 Name:eth18 DisplayName:WAN 微型端口(IPv6)-PPPoe Performance Enhancer-0000 Name:eth19 DisplayName:WAN 微型端口(网络监视器)-WFP Native MAC Layer LightWeight Filter-0000 Name:eth20 DisplayName:WAN 微型端口(IPv6)-QoS Packet Scheduler-0000 Name:eth21 DisplayName:WAN 微型端口(网络监视器)-Liebao Wifi NAT Driver-0000 Name:eth22 DisplayName:WAN 微型端口(网络监视器)-PPPoe Performance Enhancer-0000 Name:eth23 DisplayName:WAN 微型端口(网络监视器)-QoS Packet Scheduler-0000 Name:eth24 DisplayName:ChinaNetSNWide Name:ppp2 IP:10.11.182.237 BUILD SUCCESSFUL (total time: 1 second)
byte [] ip = new byte[] { (byte) 202, (byte) 117, (byte)128 , 7}; //可以更改数值超过255 InetAddress address1 = InetAddress.getByAddress(ip); InetAddress address2 = InetAddress.getByAddress("www.baidu.com", ip); System.out.println(address1); System.out.println(address2);
for (int i = 0; i <= 100; 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(); } }
for (int i = 0; i <= 155; i++) { int s = i + 100; 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(3000); } catch (UnknownHostException e) { } }
InetAddress() 相关的操作就这么多!
标签:
原文地址:http://www.cnblogs.com/silianbo/p/4634247.html