• 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

Way to get IP address through Java?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, is there a way to obtain an IP address of the current box through using Java? I am creating an installation package and require the user type in their IP address. I would like the default adr in the box to be the IP of the current machine. Thanks in advance.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part2/Chapter13/inetAddress.html
 
Marshal
Posts: 28237
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear in mind that the IP address is not an attribute of the computer, it's an attribute of the network adapter. So if a computer has two network adapters (it's quite common these days to have an adapter where you plug an Ethernet cable in as well as a wireless adapter), then it has two IP addresses.

Also bear in mind that the IP address can vary over time. When I'm at work my laptop is assigned an IP address by the office's server, and when I take the laptop home, it's assigned a different IP address by my ISP's server.
[ August 15, 2006: Message edited by: Paul Clapham ]
 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how do you retrieve the external IP on a machine ?
I have 2 IPs, one internal to my home network, and one external, the one that i get on the internet.

Check this schema to see what i mean if it's not clear :
http://forum.portforward.com/YaBB.cgi?board=Knowledge;action=display;num=1116983138

// This retrieves the internal IP address but I want the external IP address
yourAddress=java.net.InetAddress.getLocalHost();

// Maybe something like that ??
netif = java.net.NetworkInterface.getByName("NameOfAMachineWithJVM");

Thanks in advance for your help
 
Paul Clapham
Marshal
Posts: 28237
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing that by "external IP address" you mean the IP address of the router or proxy server through which you are connected to the Internet. If you wanted, you could make an HTTP connection to http://whatismyipaddress.com/ and parse it out of the page you get back. At any rate, go to that page and read the FAQs. See if any of them help you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic