Finding IP Address of a URL in Java
Overview
An IP address (Internet Protocol address) is a string of numbers that uniquely identifies any device on a network. Computers use IP addresses to connect to the internet and other networks. In simple words, an IP address is a unique identifier assigned to your device that allows it to send and receive data packets over the internet. An IP address is a string of integers separated by periods (.). They are expressed as four pairs - for example, 255.255.255.255, where each set can vary from 0 to 255.
Find the Local IP Address
Let's look at two of the most commonly used methods to get the local IP address
Local Address with Java Net Library
This method utilizes the Java Net Library to make a reliable UDP at random port 12345.
getByName():- This function returns the InetAddress of the given host. Only its validity is evaluated if the host is a literal IP address. This function also retrieves the host's public IP address. It accepts the host as an argument and returns its IP address.
- Here, we use Google's primary DNS as our destination host.
- At this point, the Java Net Library validates the address format, thus, the address itself may be unreachable.
- We are using the socket.connect() method, we establish a UDP connection on some random port number 12345.
- It configures all of the variables required for data transmission and reception, including the machine's local address, without requesting the destination host.
Note:- This method works perfectly on Linux and Windows machines but fails to return the expected IP address on macOS.
Local Address With Socket Connection
In this method, we search for the IP address using a socket connection over a reliable internet connection:
- We simply obtained the host address by connecting to google.com over port 80.
- We can create a socket connection using any other URL if it is reachable.
Find the Public IP Address
A public IP address is one that can be accessed directly via the internet and is assigned to your network router by your internet service provider (ISP). For Instance, multiple hosts on the same router have the same public IP address.
Now we connect to the http://myexternalip.com/raw URL and analyze the response. We use the following code to get our desired result.
Program 1: Fetch IP address of any URL
Output
Program 2: Fetch the Public IP Address of one’s System
In this method, we use http://bot.whatismyipaddress.com to find our public IP address. It is an online tool for determining the system's public IP address. This method simply opens the URL, reads the line, and prints it.
Output
Note:- Both these programs will not work with online compilers. Instead, use offline compilers such as Netbeans, Eclipse, and others.
:::
Conclusion
- An IP address (Internet Protocol address) is a string of numbers that uniquely identifies any device on a network.
- In simple words, an IP address is a unique identifier assigned to your device that allows it to send and receive data packets over the internet.
- IP addresses are not generated randomly. They are generated mathematically and assigned by the IANA (Internet Assigned Numbers Authority), an ICANN (Internet Corporation for Assigned Names and Numbers) department.
- getByName() function returns the InetAddress of the given host. Only its validity is evaluated if the host is a literal IP address.
- A public IP address is one that can be accessed directly via the internet and is assigned to your network router by your internet service provider (ISP).
- We can also use http://bot.whatismyipaddress.com to find our public IP address. It is an online tool for determining the system's public IP address.