943,682 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 11239
  • Java RSS
Nov 26th, 2006
0

Host's Non-Local IP Address

Expand Post »
Hello, I was just wondering if it is possible in Java to get the host's IP address, as seen on the Internet, not network or by the computer itself. I mean, getting something like 87.253.145.35 instead of something like 192.168.1.100 or 127.0.0.1.

Or, if not, is there any popular/known server that has a static IP address with which I could open a Socket and have it tell me my IP address?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Nov 26th, 2006
0

Re: Host's Non-Local IP Address

I don't know how its done, but here is an example
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,949 posts
since Aug 2005
Nov 26th, 2006
0

Re: Host's Non-Local IP Address

Ah, well, that won't really work in Java by itself, but, for those who care, this works (by utilizing a website like the one above):
Java Syntax (Toggle Plain Text)
  1. try {
  2. Socket sock = new Socket("www.edpsciences.org", 80);
  3. BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  4. PrintWriter os = new PrintWriter(sock.getOutputStream(), true);
  5.  
  6. // the actual URL is www.edpsciences.org/htbin/ipaddress (a ruby page)
  7. os.println("GET /htbin/ipaddress HTTP/1.0");
  8. os.println(); // two new lines are required to send a command
  9.  
  10. // the page has x number of lines with unnecessary information before it says "Your IP address"....
  11. String line = "";
  12. while(line.indexOf("Your IP address") == -1)
  13. {
  14. line = is.readLine();
  15. }
  16. // do a little substringing, and we'll have our answer (the ip address is inbetween "<B> " and " </B>"
  17. String ipAddr = line.substring(line.indexOf("<B> ")+4, line.indexOf("</B>")-1);
  18. }
  19. catch (Exception e)
  20. {
  21. e.printStackTrace();
  22. }

If I could figure out what the index URL for whatismyip.org is (which has only 1 thing on it: the IP address), that would be even better. I've tried a lot of extensions to "index" and some to "default"....
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Nov 27th, 2006
0

Re: Host's Non-Local IP Address

Hi everyone,

Try looking at the java's InetAddress api, there maybe something useful there for you

http://java.sun.com/j2se/1.5.0/docs/...etAddress.html

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Nov 27th, 2006
0

Re: Host's Non-Local IP Address

Well, I have tried using that, and unless there is something I'm missing, that only gives addresses such as 192.168.1.100 when used with getLocalHost().getHostAddress();. I was looking for the global/public IP address.
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Nov 29th, 2006
0

Re: Host's Non-Local IP Address

All of those do not give the public IP address, except the fourth one (http://forum.java.sun.com/thread.jsp...hreadID=639572). However, the way suggested is just what I had thought of and implemented above.
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Nov 6th, 2009
0
Re: Host's Non-Local IP Address
Click to Expand / Collapse  Quote originally posted by ilikerps ...
Hello, I was just wondering if it is possible in Java to get the host's IP address, as seen on the Internet, not network or by the computer itself. I mean, getting something like 87.253.145.35 instead of something like 192.168.1.100 or 127.0.0.1.

Or, if not, is there any popular/known server that has a static IP address with which I could open a Socket and have it tell me my IP address?
This is how I did it:

Java Syntax (Toggle Plain Text)
  1. public String getMyPublicIP(){
  2. try {
  3. URL autoIP = new URL("http://www.whatismyip.com/automation/n09230945.asp");
  4. BufferedReader in = new BufferedReader( new InputStreamReader(autoIP.openStream()));
  5. String ip_address = (in.readLine()).trim();
  6.  
  7. return ip_address;
  8.  
  9. }catch (Exception e){
  10. e.printStackTrace();
  11.  
  12. return "ERROR";
  13. }
  14. }
Last edited by davido82; Nov 6th, 2009 at 1:04 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davido82 is offline Offline
1 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: location of the 'outfile'
Next Thread in Java Forum Timeline: Incompatible types found (HELP!)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC