Finding IP Address of a URL in Java

Learn via video courses
Topics Covered

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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more