InetAddress相关笔记

来源:互联网 发布:韩国有什么软件 编辑:程序博客网 时间:2024/06/06 19:29
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 silianbo13  */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);    
View Code

 

指定域名主机的信息

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);

比较根据localhost和计算机名获得信息的不同

 

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 ());

 

获得本地主机所有IP地址

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 内核调试网络适配器Name:eth0DisplayName:Qualcomm Atheros AR9485WB-EG Wireless Network AdapterName:wlan0IP:fe80:0:0:0:21c7:474b:810b:997d%wlan0DisplayName:Realtek PCIe GBE 系列控制器Name:eth1IP:10.128.195.6IP:fe80:0:0:0:21f7:f5fa:71a7:e3d1%eth1DisplayName:Bluetooth 设备(个人区域网)Name:eth2IP:fe80:0:0:0:5d4a:7ad0:3b6f:29af%eth2DisplayName:Bluetooth 设备(RFCOMM 协议 TDI)Name:net0DisplayName:Microsoft Wi-Fi Direct 虚拟适配器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 微型端口(SSTP)Name:net3DisplayName:WAN 微型端口(L2TP)Name:net4DisplayName:WAN 微型端口(PPPOE)Name:ppp0DisplayName:WAN 微型端口(PPTP)Name:net5DisplayName:WAN Miniport (IKEv2)Name:net6DisplayName:WAN 微型端口(IP)Name:eth3DisplayName:WAN 微型端口(IPv6)Name:eth4DisplayName:WAN 微型端口(网络监视器)Name:eth5DisplayName:RAS 同步适配器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 托管网络虚拟适配器Name:wlan2IP:fe80:0:0:0:6c29:b7c6:d687:691c%wlan2DisplayName:VMware Virtual 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 虚拟适配器 #2Name:wlan3DisplayName:Microsoft ISATAP Adapter #5Name:net10DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #3Name:wlan4DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4Name:wlan5IP:fe80:0:0:0:95a5:ed42:ab58:12c4%wlan5DisplayName:Realtek PCIe GBE 系列控制器-WFP Native MAC Layer LightWeight Filter-0000Name:eth8DisplayName:Realtek PCIe GBE 系列控制器-Liebao Wifi NAT Driver-0000Name:eth9DisplayName:Realtek PCIe GBE 系列控制器-PPPoe Performance Enhancer-0000Name:eth10DisplayName:Realtek PCIe GBE 系列控制器-QoS Packet Scheduler-0000Name:eth11DisplayName:Realtek PCIe GBE 系列控制器-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 托管网络虚拟适配器-WFP Native MAC Layer LightWeight Filter-0000Name:wlan13DisplayName:Microsoft 托管网络虚拟适配器-Native WiFi Filter Driver-0000Name:wlan14DisplayName:Microsoft 托管网络虚拟适配器-Liebao Wifi NAT Driver-0000Name:wlan15DisplayName:Microsoft 托管网络虚拟适配器-PPPoe Performance Enhancer-0000Name:wlan16DisplayName:Microsoft 托管网络虚拟适配器-QoS Packet Scheduler-0000Name:wlan17DisplayName:Microsoft 托管网络虚拟适配器-WFP 802.3 MAC Layer LightWeight Filter-0000Name:wlan18DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-WFP Native MAC Layer LightWeight Filter-0000Name:wlan19DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-Native WiFi Filter Driver-0000Name:wlan20DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-Liebao Wifi NAT Driver-0000Name:wlan21DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-PPPoe Performance Enhancer-0000Name:wlan22DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-QoS Packet Scheduler-0000Name:wlan23DisplayName:Microsoft Wi-Fi Direct 虚拟适配器 #4-WFP 802.3 MAC Layer LightWeight Filter-0000Name:wlan24DisplayName:WAN 微型端口(IP)-WFP Native MAC Layer LightWeight Filter-0000Name:eth13DisplayName:WAN 微型端口(IP)-Liebao Wifi NAT Driver-0000Name:eth14DisplayName:WAN 微型端口(IP)-PPPoe Performance Enhancer-0000Name:eth15DisplayName:WAN 微型端口(IP)-QoS Packet Scheduler-0000Name:eth16DisplayName:WAN 微型端口(IPv6)-WFP Native MAC Layer LightWeight Filter-0000Name:eth17DisplayName:WAN 微型端口(IPv6)-Liebao Wifi NAT Driver-0000Name:eth18DisplayName:WAN 微型端口(IPv6)-PPPoe Performance Enhancer-0000Name:eth19DisplayName:WAN 微型端口(网络监视器)-WFP Native MAC Layer LightWeight Filter-0000Name:eth20DisplayName:WAN 微型端口(IPv6)-QoS Packet Scheduler-0000Name:eth21DisplayName:WAN 微型端口(网络监视器)-Liebao Wifi NAT Driver-0000Name:eth22DisplayName:WAN 微型端口(网络监视器)-PPPoe Performance Enhancer-0000Name:eth23DisplayName:WAN 微型端口(网络监视器)-QoS Packet Scheduler-0000Name:eth24DisplayName:ChinaNetSNWideName:ppp2IP:10.11.182.237BUILD SUCCESSFUL (total time: 1 second)

  

根据IP地址构造InetAddress

 

byte [] ip = new byte[] { (byte) 202, (byte) 117, (byte)128 , 7};  //可以更改数值超过255InetAddress address1 = InetAddress.getByAddress(ip);InetAddress address2 = InetAddress.getByAddress("www.baidu.com", ip);System.out.println(address1);System.out.println(address2);

 

利用InetAddress.getByName()按计算机名称获局域网中所有开机主机名称和IP

 

  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();            }        }

 

  

利用InetAddress.getByName()IP地址获得指定网络段所有开机主机名称/域名和IP地址,结合方法isReachable()

 

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()  相关的操作就这么多!