Host's Non-Local IP Address

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Host's Non-Local IP Address

 
0
  #1
Nov 26th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Host's Non-Local IP Address

 
0
  #2
Nov 26th, 2006
I don't know how its done, but here is an example
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Host's Non-Local IP Address

 
0
  #3
Nov 26th, 2006
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):
  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"....
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Host's Non-Local IP Address

 
0
  #4
Nov 27th, 2006
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
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Host's Non-Local IP Address

 
0
  #5
Nov 27th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Host's Non-Local IP Address

 
0
  #7
Nov 29th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: davido82 is an unknown quantity at this point 
Solved Threads: 0
davido82 davido82 is offline Offline
Newbie Poster
 
0
  #8
30 Days Ago
Originally Posted by ilikerps View 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?
This is how I did it:

  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; 30 Days Ago at 1:04 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC