• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting IP Address of Machine...

 
Amit Bathla
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an InetAddress you can call getHostAddress()
 
Amit Bathla
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of using getHostAddress()

 
Amit Bathla
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic