aspose file tools
The moose likes Sockets and Internet Protocols and the fly likes Way to get IP address through Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Way to get IP address through Java?" Watch "Way to get IP address through Java?" New topic
Author

Way to get IP address through Java?

William Ross
Greenhorn

Joined: Jul 06, 2006
Posts: 12
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.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part2/Chapter13/inetAddress.html


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

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 ]
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 546

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


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Way to get IP address through Java?
 
Similar Threads
Read Client IP Address behind Proxy
Internet Access
prob with weblogic 5.1
Tracking Remote Public IP address
passing IP address to print object