java.net.InetAddress
Last updated
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("openai.com");
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
System.out.println("Is Reachable: " + address.isReachable(2000));
}
}Host Name: openai.com
IP Address: 104.18.30.59
Is Reachable: trueimport java.net.InetAddress;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) {
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("Local Host: " + localHost);
System.out.println("Host Name: " + localHost.getHostName());
System.out.println("Host Address: " + localHost.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}Local Host: my-machine/192.168.1.10
Host Name: my-machine
Host Address: 192.168.1.10