java 网络 InetAddress和NetworkInterface

来源:互联网 发布:java生成app 编辑:程序博客网 时间:2024/05/21 14:42

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.1192.168.0.107

0 0
原创粉丝点击