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 8449505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:43:38+00:00 2026-06-10T10:43:38+00:00

I am working on a VERY SIMPLE chat client and server and I need

  • 0

I am working on a VERY SIMPLE chat client and server and I need to have a message on the server like this : (“IP: ” + ipAddr)
My problem is when trying to get the ipAddr of the server.

I want it to work exactly like “curl ifconfig.me” in the Terminal on Mac.

If I do curl ifconfig.me on my computer it will return : 76.xx.xxx.xxx
Parts hidden to protect myself.

I need my program to return this.
Currently with this code :

        try {

    InetAddress thisIp = InetAddress.getLocalHost();

    System.err.println("IP: "+ thisIp.getHostAddress());

    }
    catch(Exception e) {

    System.err.println("Error!");

    }

}

It will return 127.0.0.1, The localhost IP addr. Can anyone help me with figuring out a program to do this?

Thanks!

  • 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-10T10:43:40+00:00Added an answer on June 10, 2026 at 10:43 am

    ifconfig.me is a website, so when you curl it you are getting an external website telling you what it sees as you IP address. this information is not available inside the JVM (or really the computer) you need to request your ipaddress from something outside of your network to see your public ip address.

    Now if you want to fetch the content of a website in Java you can do this:

    note that this (updated) example will simulate exactly what cURL sends to the ifconfig.me site, so you will get the expected response. If you don’t send a User-Agent that looks like cURL then ifconfig.me will just send you the full HTML document that it sends to a web browser

    public static void main( String[] args ) throws IllegalStateException, IOException
    {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://ifconfig.me/");
        request.setHeader("User-Agent","curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/0.9.8h zlib/1.2.3 libssh2/0.15-CVS");
        HttpResponse response = client.execute(request);
    
        // Get the response
        String addr = IOUtils.toString(new InputStreamReader(response.getEntity().getContent()));
        System.out.println(addr);
    }
    

    Now this code uses the HttpComponents and IOUtils.toString() from Apache Commons IO. These projects will help make your chat client and server even simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually I'm working on very simple FTP server. Now I have problem with sending
I'm working on SQL server 2005 and I have a very simple stored procedure:
I'm working on writing a very simple client/server application as an excuse to start
I have a very simple client server code written java(server listens on some port
I am trying to get jscrollpane working on a very simple page, but for
I'm working on a very simple iPhone app, that in the end will have
I have a very simple code, which annoyingly was working and for the life
This is a very simple, (hopefully) question. I am new to working with DateTime
I am working on this very simple method I know I am very close
I am working on a very simple application, using MVC2 Preview 1. I have

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.