您的位置:首页 > 理论基础 > 计算机网络

java 网络 InetAddress和NetworkInterface

2016-07-21 11:27 323 查看
1.InetAddress是ip地址的高级表示。

(1)getByName静态方法,接受要查找的主机名作为参数。

(2)getHostName返回包含主机名的字符串,如果没有主机名则返回ip

(3)getHostAddress返回主机的ip

2.NetworkInterface表示物理硬件和虚拟地址

使用NetworkInterface返回本地ip

Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface nif = netInterfaces.nextElement();
Enumeration<InetAddress> iparray = nif.getInetAddresses();
while (iparray.hasMoreElements()) {
InetAddress ip = iparray.nextElement();
System.out.println(ip.getHostAddress());
}
}


输出结果为:

127.0.0.1
192.168.0.107
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: