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?

I don't know how its done, but here is an example

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"....

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.

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";
	    }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.