• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting IP Address of Machine...

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

 
Amit Bathla
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic