This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Getting IP Address of Machine... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting IP Address of Machine..." Watch "Getting IP Address of Machine..." New topic
Author

Getting IP Address of Machine...

Amit Bathla
Ranch Hand

Joined: Feb 11, 2005
Posts: 51
sir please help me to get the IP address of ur machine..
there is a function...

String getHostAddress()

tell me how to use it...

InetAddress = address = InetAddress() returns name of achine...not IP

i want numerical value..
please help

Amit
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If you have an InetAddress you can call getHostAddress()
Amit Bathla
Ranch Hand

Joined: Feb 11, 2005
Posts: 51
Sir ,we used the code as given below

InetAddress address = InetAddress.getLocalHost();

This returns the localHost Name, but we want the numerical IP address, which we want to take as a string.
There is also a method "getHostAddress()" , please tell us how to use this function,
please provide the code.
Thanks.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Here is an example of using getHostAddress()

Amit Bathla
Ranch Hand

Joined: Feb 11, 2005
Posts: 51
ok sir...the code provided by u works fine..


import java.net.*;

public class Test14 {

public static void main(String[] args) throws UnknownHostException {
InetAddress address = InetAddress.getByName("localhost");

System.out.println(address.getHostAddress());
}

}

but this always returns 127.0.0.1 which is not useful..
i want correct ip of my machine..lets say..192.168.4.104....
whatever it is..
please tell..
waitting..
Amit
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
Then don't use the hostname "localhost". Use the host name
that is assigned to the public inteface. Type the command
"hostname" at the command prompt and it will return your
machines "real" hostname.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting IP Address of Machine...
 
Similar Threads
Finding the IP Address of Remote Machines if any connected to my Current machine
Search
IP Address Sniffer Log
how to get IP address in linux
verification of ip-addresses