Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Tutorial: Simple Ways to Get Your IP Address in Java

  1. #1
    Junior Member
    Join Date
    Dec 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tutorial: Simple Ways to Get Your IP Address in Java

    Method 1: Fetching Local IP Address
    Java’s built-in networking libraries make it straightforward to retrieve your local IP address. Here’s a simple example:
    <code>
    import java.net.InetAddress;
    import java.net.UnknownHostException;

    public class GetLocalIPAddress {
    public static void main(String[] args) {
    try {
    // Get the local host address
    InetAddress localHost = InetAddress.getLocalHost();
    System.out.println("Local IP Address: " + localHost.getHostAddress());
    } catch (UnknownHostException e) {
    System.err.println("Error retrieving local IP address: " + e.getMessage());
    }
    }
    }
    </code>

    This method uses InetAddress.getLocalHost() to retrieve the IP address of the local machine.
    The getHostAddress() method converts it to a readable String.
    Note: This gives you the local machine’s IP address, which might be 127.0.0.1 if you’re running on localhost.

    Fetching Public IP Address Using api.ipquery.io
    To retrieve your public IP address, you’ll need to use an external service like api.ipquery.io. This API is free and doesn’t require an API key. Java’s HTTP client makes it easy to consume REST APIs.

    Here’s how you can do it:
    <code>
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class GetPublicIPAddress {
    public static void main(String[] args) {
    String apiURL = "https://api.ipquery.io/";

    try {
    URL url = new URL(apiURL);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");

    int responseCode = connection.getResponseCode();
    if (responseCode == 200) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder response = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
    response.append(line);
    }
    reader.close();

    System.out.println("Public IP Address (Response): " + response.toString());
    } else {
    System.err.println("Error: Unable to fetch public IP. Response code: " + responseCode);
    }
    } catch (Exception e) {
    System.err.println("Error retrieving public IP address: " + e.getMessage());
    }
    }
    }
    </code>

    The URL returns your public IP address in plain text.
    We use Java’s HttpURLConnection to make a GET request.
    The response is read using a BufferedReader and printed to the console.


    You can parse the JSON response from the API using a library like Jackson or Gson if you need specific fields like ip, city, or country.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tutorial: Simple Ways to Get Your IP Address in Java

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Else if statement tutorial in Java!! New beginner java tutorial video!!
    By OrbtialStudios in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 23rd, 2018, 05:41 PM
  2. Simple Address Formater
    By Easterncomputers in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 13th, 2014, 07:48 AM
  3. how to study java the right ways ?
    By javabigkid in forum The Cafe
    Replies: 2
    Last Post: October 22nd, 2013, 10:56 AM
  4. Simple Address Book Program Not Working....
    By programmer101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2013, 01:21 AM
  5. Replies: 1
    Last Post: November 11th, 2011, 07:46 PM

Tags for this Thread