获取IP地址:

public class IPDemo {


public static void main(String [] args) throws UnknownHostException{
//获取本地主机
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);//Lenovo-sxg/192.168.1.106
//获取本地主机的IP地址
String ipaddress = localHost.getHostAddress();
System.out.println(ipaddress);
//获取本地主机的主机名
String name = localHost.getHostName();
System.out.println(name);
//在给定主机名的情况下确定主机的 IP地址
InetAddress inetAddress = InetAddress.getByName("192.168.1.106");
System.out.println(inetAddress.getHostName()+inetAddress.getHostAddress());
//在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
for (int i = 0; i < allByName.length; i++) {
System.out.println(allByName[i]);
}
}
}

你可能感兴趣的:(IP地址)