Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:07:10+00:00 2026-06-09T13:07:10+00:00

How would I get a users IP address? InetAddress ip; try { ip =

  • 0

How would I get a users IP address?

InetAddress ip;
try {

 ip = InetAddress.getLocalHost();
 System.out.println("Current IP address : " + ip.getHostAddress());

} catch (UnknownHostException e) {

e.printStackTrace();

 }

That returns: 127.0.0.1

I know that is not my IP. It is my local IP address. How do I get a users IP using java..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:07:11+00:00Added an answer on June 9, 2026 at 1:07 pm

    The shortest method is:

    try {
    InetAddress thisIp =InetAddress.getLocalHost();
    System.out.println("IP:"+thisIp.getHostAddress());
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    

    However getLocalHost docs say:

    If there is a security manager, its checkConnect method is called with
    the local host name and -1 as its arguments to see if the operation is
    allowed. If the operation is not allowed, an InetAddress representing
    the loopback address is returned.

    and in some cases InetAddress.getLocalHost() doesn’t consult your interfaces, it simply returns constant 127.0.0.1 (for IPv4))

    I think NetworkInterface.getNetworkInterfaces is what you need to enumerate all the possibilities. Here’s an example which doesn’t show virtual addresses, but works for “main” interfaces:

    import java.net.*;
    import java.util.*;
    
    public class Test
    {
        public static void main(String[] args)
            throws Exception // Just for simplicity
        {
            for (Enumeration<NetworkInterface> ifaces = 
                   NetworkInterface.getNetworkInterfaces();
                 ifaces.hasMoreElements(); )
            {
                NetworkInterface iface = ifaces.nextElement();
                System.out.println(iface.getName() + ":");
                for (Enumeration<InetAddress> addresses =
                       iface.getInetAddresses();
                     addresses.hasMoreElements(); )
                {
                    InetAddress address = addresses.nextElement();
                    System.out.println("  " + address);
                }
            }
        }
    }
    

    Alternatively:

    Try using this (First output should be IP after PC name):

        InetAddress[] localaddr;
    
        String computername = null;
    
        try {
            computername = InetAddress.getLocalHost().getHostName();//get pc name
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        }
    
        System.out.println(computername);
    
        try {
            localaddr = InetAddress.getAllByName(computername);
            for (int i = 0; i < localaddr.length; i++) {
                System.out.println("\n" + localaddr[i].getHostAddress());
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    References:

    • IP Address not obtained in java
    • http://www.roseindia.net/java/java-get-example/get-ip-address.shtml
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get the address string of the current user location. Is
Following is how you would get the current solution directory from an add-in: _applicationObject
What kinds of information can PHP get about users? I would like to make
I am wanting to get the users IP address (the logged in user will
I would like to understand the programmatic way of finding out(logging) IP address &
How would I get the SUM of the mutual field here? SELECT first_user_id, mutual
Using linqtosql, how would I get a colleciton of User objects, if I have
I would like to know what events get executed (if any) after a user
i would get details from two MySQL tables tables structure as shown table:App |AppID|AppName|AppType|
How would get find an average from an array? If I have the array:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.