DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Host's Non-Local IP Address (http://www.daniweb.com/forums/thread62812.html)

ilikerps Nov 26th, 2006 4:06 pm
Host's Non-Local IP Address
 
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?

Ancient Dragon Nov 26th, 2006 8:53 pm
Re: Host's Non-Local IP Address
 
I don't know how its done, but here is an example

ilikerps Nov 26th, 2006 10:18 pm
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):
 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"....

freesoft_2000 Nov 27th, 2006 10:43 am
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

ilikerps Nov 27th, 2006 4:16 pm
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.

freesoft_2000 Nov 28th, 2006 3:17 pm
Re: Host's Non-Local IP Address
 
Hi everyone,

see the below threads

http://forum.java.sun.com/thread.jsp...62802&tstart=0

http://forum.java.sun.com/thread.jsp...hreadID=592585

http://forum.java.sun.com/thread.jsp...hreadID=520999

http://forum.java.sun.com/thread.jsp...hreadID=639572

http://sharkysoft.com/tutorials/jsa/content/040.html

http://forum.java.sun.com/thread.jsp...hreadID=143409

http://www.java-tips.org/java-se-tip...an-applet.html

http://forum.java.sun.com/thread.jsp...hreadID=218845

http://forum.java.sun.com/thread.jsp...hreadID=269203

Richard West

ilikerps Nov 29th, 2006 7:42 am
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.

davido82 Nov 6th, 2009 1:03 pm
Quote:

Originally Posted by ilikerps (Post 280829)
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:

public String getMyPublicIP(){
        try {
            URL autoIP = new URL("http://www.whatismyip.com/automation/n09230945.asp");
            BufferedReader in = new BufferedReader( new InputStreamReader(autoIP.openStream()));
            String ip_address = (in.readLine()).trim();

            return ip_address;

        }catch (Exception e){
                    e.printStackTrace();

            return "ERROR";
            }
    }


All times are GMT -4. The time now is 7:45 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC