Solved

How to get the PRACTICAL IP address of my linux box?

Posted on 2002-06-24
4
211 Views
Last Modified: 2010-03-31
I have a Linux box using DHCP to get it's IP address.

I am writing a Java program to show the IP address, I used the following code:

myIP = InetAddress.getLocalHost().getHostAddress();

however, the result of the above code is "127.0.0.1" -- brings me no information....

How does the getHostAddress() work? and in what situation it will return a practical IP address instead of the loopback IP?

Thanks!

Bun
0
Comment
Question by:bunwong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 100 total points
ID: 7106570
try this code

        String hostName = new String();
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch(UnknownHostException uhe) {
            System.err.println("Exception getting host: " +
                uhe.toString());
        }
        System.out.println("Hostname is: " + hostName);

        InetAddress[] allInterfaces=null;
        try {
            allInterfaces = InetAddress.getAllByName(hostName);
        } catch(UnknownHostException uhe) {
            System.err.println("Exception getting host: " +
                uhe.toString());
        }

        for(int x=0;x<allInterfaces.length;x++) {
            System.out.println("IP address " + x+1 + ": " +
                allInterfaces[x].getHostAddress());
        }      



(copy and pasted from
http://groups.google.com/groups?q=Java+%22get+all+IP+addresses%22&hl=en&lr=&ie=UTF-8&oe=utf-8&selm=Hl7E7.4903%24Fr3.100304%40news1.oke.nextra.no&rnum=1
)
0
 

Author Comment

by:bunwong
ID: 7107645
Hi Heyhey,

Thank you for your comment. However, the code you gave me does not work.

It is able to get the name "localhost" and the IP address "127.0.0.1", but it does not get the other IP address "192.168.1.104".

The IP address "192.168.1.104" is assigned by a DHCP server, the address works on all my IP related tools, e.g. ping, ftp, telnet, vnc.

bun
0
 
LVL 35

Expert Comment

by:girionis
ID: 8860678
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this
question is:

- points to heyhey_

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question