User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 422,560 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,694 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2776 | Replies: 6 | Solved
Reply
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Question Host's Non-Local IP Address

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,114
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 928
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Host's Non-Local IP Address

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

Solution Re: Host's Non-Local IP Address

  #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):
 try {
			Socket sock = new Socket("www.edpsciences.org", 80);
			BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
			PrintWriter os = new PrintWriter(sock.getOutputStream(), true);

                           // the actual URL is www.edpsciences.org/htbin/ipaddress (a ruby page)
			os.println("GET /htbin/ipaddress HTTP/1.0");
			os.println(); // two new lines are required to send a command
                     	
                           // the page has x number of lines with unnecessary information before it says "Your IP address"....
			String line = "";
			while(line.indexOf("Your IP address") == -1)
			{
				line = is.readLine();
			}
                                                // do a little substringing, and we'll have our answer (the ip address is inbetween "<B> " and " </B>"
			String ipAddr = line.substring(line.indexOf("<B> ")+4, line.indexOf("</B>")-1);
	    } 
	    catch (Exception e)
	    {
	    	e.printStackTrace();
	    }

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  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Host's Non-Local IP Address

  #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  
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Host's Non-Local IP Address

  #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  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
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 
Rep Power: 3
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Host's Non-Local IP Address

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 1:18 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC