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

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

  

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

转载于:https://www.cnblogs.com/silianbo/p/4634247.html

InetAddress相关笔记相关推荐

  1. SQL常用用法相关笔记

    SQL常用用法相关笔记 1).----CAST和CONVERT的用法 SQL中的cast和convert都是用来将一种数据类型的表达式转换为另一种数据类型的表达式. CAST和CONVERT提供相似的 ...

  2. cscope 相关笔记

    cscope 相关笔记 cscope 可以用来解析 c 代码,生成引用信息,用户通过查询引用信息来在代码中进行跳转. 使用步骤如下: find 寻找待 index 的文件,并将文件列表写入到 csco ...

  3. windows相关笔记

    windows相关笔记 前言 一. 彻底关闭windows10自动更新 二.开机启动批处理禁用win10更新服务 三.解决win和(linux或mac)双系统时间不同步问题 四.添加环境变量示例ope ...

  4. 矩阵的病态性研究相关笔记

    矩阵的病态性研究相关笔记 一.概念定义 1.1 良态VS病态 病态问题(ill-conditioned problem)是指输出结果相对于输入非常敏感,输入数据哪怕是极少(或者极微妙)的噪声也会导致输 ...

  5. 微环谐振器MRR相关笔记

    微环谐振器MRR相关笔记 这是第一次将自己的笔记上传到博客上,直接全部复制进来好像就成一整个图片了,下次我再排版好一些. 这些是我在科研的途中整理出来的资料,有些直接贴图,有些计算是自己手写的.

  6. [笔记] 最大权闭合子图最大流最小割相关笔记

    最大权闭合子图 对于最大权闭合子图的理论学习并且补充一部分网络流的笔记 有向图中若干个点组成的一个集合\(V\),集合\(V\)的所有出边所连的点也都属于\(V\),这些点构成的图即为闭合子图 点有点 ...

  7. SpringBoot的幕后推手,分布式架构演进+相关笔记参考

    ​ 这两年随着微服务的盛行,SpringBoot框架水到渠成的得到了高曝光,作为程序猿的我们,现在要是不知道一点SpringBoot相关的东西,貌似出去找工作都会被深深地鄙视,不过在我们开始Sprin ...

  8. Javascript使用三大家族和事件来DIY动画效果相关笔记(一)

    1.offset家族 ◆offsetWidth和offsetHeight表示盒子真实的宽度高度,这个真实的宽度包括 四周的边框.四周的padding.及定义的宽度高度或内容撑开的高度和宽度,可以用来检 ...

  9. 记一次mpvue开发完整小程序相关笔记

    通过一个月的小程序开发,记录下一点点小心得······ 先给各位大佬请安了~~ 1.wx.previewImage预览图片 关闭后会再次触发onShow方法: 在全局变量里定义一个全局变量开关来控制, ...

最新文章

  1. C语言用户标准是什么,C语言系统用户标准管理系统.doc
  2. 小米4公布会视频地址
  3. java资源分配算法,java - 资源分配与动态规划算法 - 堆栈内存溢出
  4. JSTL分割字符 fn:split()
  5. HDP 2.6 requires libtirpc-devel
  6. leetcode1111. 有效括号的嵌套深度(栈)
  7. 开玩笑呢?学习KMP算法能改变自我认知? | 原力计划
  8. mysql 内置存储过程_mysql 内置存储过程
  9. matlab转dsp软件,matlab/simulink程序代写 DSP程序开发
  10. oracle月份相减函数,Oracle 日期函数的加减
  11. 如何画OFDM频谱图
  12. searchview怎么改hint大小_老司机偷偷告诉你:改水电一般多少钱?水电改造价格怎么算?...
  13. 张孝祥正在整理Java就业面试题大全
  14. 计算机应用能力考试ppt,全国专业技术人员计算机应用能力考试 PPT 2003 题库版...
  15. Ubuntu简单使用操作
  16. 微信小程序审核不通过的原因,这里整理了10个最常见的
  17. python炫酷动画源代码_Python tkinter实现的图片移动碰撞动画效果【附源码下载】...
  18. 我与照片之乾坤大挪移-瞬间旋转你的照片
  19. python常用接口调用
  20. MSP430F5529-PWM波在串口中的输出及调整

热门文章

  1. ApacheTomcat解析请求参数的过程
  2. selenium 定位不到元素总结
  3. 如何实现网站文件动静分离
  4. 菜鸟崛起 DB Chapter 2 MySQL 5.6的概述与安装
  5. 2017-06-14 Linux 添加密钥key
  6. 把报表的数据导出Excel
  7. hackthon提高
  8. 【转】简述configure、pkg-config、pkg_config_path三者的关系
  9. 2013-10-31 《October 31st, 2013》
  10. oddo docker 安装