InetAddress相关笔记
生活随笔
收集整理的這篇文章主要介紹了
InetAddress相关笔记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
InetAddress的實(shí)例對象包含以數(shù)字形式保存的IP地址,同時(shí)還可能包含主機(jī)名(如果使用主機(jī)名來獲取InetAddress的實(shí)例,或者使用數(shù)字來構(gòu)造,并且啟用了反向主機(jī)名解析的功能)。InetAddress類提供了將主機(jī)名解析為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 //此處添加相關(guān)的代碼段 17 18 } 19 20 }?
獲得本地主機(jī)信息
1 InetAddress localAddress = InetAddress.getLocalHost(); 2 System.out.println(localAddress); View Code?
指定域名主機(jī)的信息
String host = "www.google.com";InetAddress address = InetAddress.getByName(host);System.out.println(address);根據(jù)指定域名獲得所有信息
String host = "www.baidu.com"; //注意獲得的所有IP地址InetAddress [] addresses = InetAddress.getAllByName(host);for(InetAddress address : addresses)System.out.println(address);比較根據(jù)localhost和計(jì)算機(jī)名獲得信息的不同
?
String host = "localhost"; //更改localhost為你現(xiàn)在所使用計(jì)算機(jī)名,查看不同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 ());?
獲得本地主機(jī)所有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 1 Name:lo IP:127.0.0.1 IP:0:0:0:0:0:0:0:1 DisplayName:Microsoft 內(nèi)核調(diào)試網(wǎng)絡(luò)適配器 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 設(shè)備(個(gè)人區(qū)域網(wǎng)) Name:eth2 IP:fe80:0:0:0:5d4a:7ad0:3b6f:29af%eth2 DisplayName:Bluetooth 設(shè)備(RFCOMM 協(xié)議 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 微型端口(網(wǎng)絡(luò)監(jiān)視器) 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 托管網(wǎng)絡(luò)虛擬適配器 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 托管網(wǎng)絡(luò)虛擬適配器-WFP Native MAC Layer LightWeight Filter-0000 Name:wlan13 DisplayName:Microsoft 托管網(wǎng)絡(luò)虛擬適配器-Native WiFi Filter Driver-0000 Name:wlan14 DisplayName:Microsoft 托管網(wǎng)絡(luò)虛擬適配器-Liebao Wifi NAT Driver-0000 Name:wlan15 DisplayName:Microsoft 托管網(wǎng)絡(luò)虛擬適配器-PPPoe Performance Enhancer-0000 Name:wlan16 DisplayName:Microsoft 托管網(wǎng)絡(luò)虛擬適配器-QoS Packet Scheduler-0000 Name:wlan17 DisplayName:Microsoft 托管網(wǎng)絡(luò)虛擬適配器-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 微型端口(網(wǎng)絡(luò)監(jiān)視器)-WFP Native MAC Layer LightWeight Filter-0000 Name:eth20 DisplayName:WAN 微型端口(IPv6)-QoS Packet Scheduler-0000 Name:eth21 DisplayName:WAN 微型端口(網(wǎng)絡(luò)監(jiān)視器)-Liebao Wifi NAT Driver-0000 Name:eth22 DisplayName:WAN 微型端口(網(wǎng)絡(luò)監(jiān)視器)-PPPoe Performance Enhancer-0000 Name:eth23 DisplayName:WAN 微型端口(網(wǎng)絡(luò)監(jiān)視器)-QoS Packet Scheduler-0000 Name:eth24 DisplayName:ChinaNetSNWide Name:ppp2 IP:10.11.182.237 BUILD SUCCESSFUL (total time: 1 second)
根據(jù)IP地址構(gòu)造InetAddress
?
byte [] ip = new byte[] { (byte) 202, (byte) 117, (byte)128 , 7}; //可以更改數(shù)值超過255InetAddress address1 = InetAddress.getByAddress(ip);InetAddress address2 = InetAddress.getByAddress("www.baidu.com", ip);System.out.println(address1);System.out.println(address2);?
利用InetAddress.getByName()按計(jì)算機(jī)名稱獲局域網(wǎng)中所有開機(jī)主機(jī)名稱和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地址獲得指定網(wǎng)絡(luò)段所有開機(jī)主機(jī)名稱/域名和IP地址,結(jié)合方法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() 相關(guān)的操作就這么多!
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/silianbo/p/4634247.html
總結(jié)
以上是生活随笔為你收集整理的InetAddress相关笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言 基础60题(5)
- 下一篇: 端到端的超媒体REST API设计