运行结果如下,本地主机名是XXl,但获取的是XXL.lan;获取的ip地址无误
这是代码:
package Pers.Pluto.Day26;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
InetAddress address = null; //主机名或者ip地址
// address = InetAddress.getByName("XXL");
// address = InetAddress.getLocalHost();
address = InetAddress.getByName("192.168.1.136");
String name = address.getHostName(); //获取主机名
String hostAddress = address.getHostAddress(); //获取本地ip
System.out.println("主机名:" + name + "\r\n" + "ip:" + hostAddress);
}
}
运行结果如下,本地主机名是XXl,但获取的是XXL.lan