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 : 126.96.36.199 Input : localhost Output : 127.0.0.1
Below programs illustrate how to fetch public IP address:
Note: These programs wont 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/188.8.131.52
Program 2: Fetch public IP address of the one’s system
To find public IP, use http://bot.whatismyipaddress.com. It is an online utility, to find system’s public IP. Open the URL, read a line and print the line.
Public IP Address: 184.108.40.206
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.