Retrieving the IP address of a website in Java
In this tutorial, we're going to use the method java.net.InetAddress.getByName() To obtain the IP address of a website's web server in Java based on the representation of its IP address or on the domain name e.g. www.google.com.import java.net.InetAddress;Output:
import java.net.SocketException;
import java.net.UnknownHostException;
public class ServerAddress {
public static void main(String[] zero) throws SocketException {
InetAddress ServerAddress;
try{
ServerAddress= InetAddress.getByName("www.yahoo.fr");
//domain name
System.out.println("Yahoo server address = "+
ServerAddress.getHostName());
//Yahoo server IP address
System.out.println("Domain name = "+
ServerAddress.getHostName());
System.out.println("The IP address of the server Yahoo.com = "+
ServerAddress.getHostAddress());
System.out.println("The IP address of the server Yahoo.fr = "+
InetAddress.getByName("www.yahoo.fr").getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
domain name = www.yahoo.comgetHostName(): returns the machine name for this IP address.
The IP address of the server Yahoo.com = 46.228.47.115
The IP address of the server Yahoo.fr = 77.238.184.150
getHostAdress(): returns the IPv4 address of the server.
References:
Doc Oracle: InetAdress
Manipulating IP Addresses in Java
TCP Socket: Client/Server in Java