InetAddress 类的一个实例就代表一个具体的 ip 地址。
public class InetAddressTest {
public static void main(String[] args) {
try {
// getByName 获取指定IP
InetAddress inet1 = InetAddress.getByName("10.19.155.104");
System.out.println(inet1); // /10.19.155.104
InetAddress inet2 = InetAddress.getByName("www.bilibili.com");
System.out.println(inet2); // www.bilibili.com/121.194.11.73
// getLocalHost 获取本地IP
InetAddress inet3 = InetAddress.getLocalHost();
System.out.println(inet3); // xcx/10.19.155.104
System.out.println(inet3.getHostAddress()); // /10.19.155.104
System.out.println(inet3.getHostName()); // xcx
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}