Finding IP address of a URL in Java
getByName() : Returns the InetAddress of the given host. If the host is a literal IP address, then only its validity is checked. Fetches public IP Address of the host specified. It takes the host as an argument and returns the corresponding IP address.
Input : www.google.com Output : 220.127.116.11 Input : localhost Output : 127.0.0.1
Below programs illustrate how to fetch public IP addresses:
Note: These programs won’t run on online compilers. Use offline compilers like Netbeans, Eclipse, etc, instead.
Program 1: Fetch IP address of any URL
Public IP Address of: www.google.com/18.104.22.168
Program 2: Fetch the public IP address of one’s system
To find public IP, use http://bot.whatismyipaddress.com. It is an online utility, to find the system’s public IP. Open the URL, read a line and print the line.
Public IP Address: 22.214.171.124