participate


Java Programming - Accessing IP of computer
<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
Blanchy123
Posts:28
Registered: 2/7/07
Accessing IP of computer   
Apr 18, 2007 8:27 AM

 
How can I determine my IP address that over users can use to connect to over the internet?

The only way I can think of doing is connecting to a website like http://www.whatismyipaddress.com/ and parsing out the IP address, are there any methods which can tell me the IP automatically?
 
mlk
Posts:10,971
Registered: 8/15/01
Re: Accessing IP of computer   
Apr 18, 2007 8:30 AM (reply 1 of 5)  (In reply to original post )

 
[url=http://java.sun.com/j2se/1.4.2/docs/api/java/net/InetAddress.html#getLocalHost()]java.net.InetAddress.getLocalHost()[/url]
 
RBK
Posts:5
Registered: 1/17/07
Re: Accessing IP of computer   
Apr 18, 2007 8:32 AM (reply 2 of 5)  (In reply to original post )

 
You should use the InetAddress class in the java.net package:

InetAddress localIP = InetAddress.getLocalHost();
System.out.println("My IP:" + localIP.getHostAddress());
 
Blanchy123
Posts:28
Registered: 2/7/07
Re: Accessing IP of computer   
Apr 18, 2007 8:38 AM (reply 3 of 5)  (In reply to #2 )

 
You should use the InetAddress class in the java.net
package:

InetAddress localIP =
InetAddress.getLocalHost();
System.out.println("My IP:" +
localIP.getHostAddress());

This returns My IP:192.168.1.100, which is my local IP, I need the IP that other users on the internet can use to connect to my computer.
 
KathyMcDonnell
Posts:528
Registered: 27/02/07
Re: Accessing IP of computer   
Apr 18, 2007 8:42 AM (reply 4 of 5)  (In reply to #3 )

 
This returns My IP:192.168.1.100

192.168.1.100 is a private IP address.
You are clearly behind a NAT, or your ISP put up a NAT or Proxy in front of its users.
Explain your setup, then perhaps there's a way to use Java to get that information.

http://en.wikipedia.org/wiki/Private_network
 
DrClap
Posts:39,424
Registered: 4/30/99
Re: Accessing IP of computer   
Apr 18, 2007 9:25 AM (reply 5 of 5)  (In reply to #4 )

 
For example, my computer's IP address is 10.20.something. That's its address in my company's network. When I go to whatismyip.com it tells me that my address is 204.something, which is the IP address of the company's proxy server.

However, to answer the question you didn't ask, nobody can connect to my computer via that 204.something address. So as Kathy says, please explain your requirements.
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 25
  • Guests : 132

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums