Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External IP Address of the client

Sounds funny, but how can I get the external IP address from a client?

I tried few things, but didn't work for me.

in first place I tried

request.getRemoteAddr()

and I am getting the result as: 0:0:0:0:0:0:0:1

in second place I tried

InetAddress ip = InetAddress.getLocalHost();
ip.getHostAddress());

and I am getting the result as: 127.0.0.1

in third place I tried

        URL whatismyip = new URL("http://checkip.dyndns.org:8245/");
        BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

        String IPStrOld = inIP.readLine(); //IP as a String
        String IPStrNewest = IPStrOld.replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", "");
        String IPStr = IPStrNewest.replace("</body></html>", "");

but I get the external IP of the server only

and for the last place

        URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
        BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
        String ip = inIP.readLine();

this is the same, I get the external IP of the server only

So, what's the deal?

like image 591
Denees Avatar asked Aug 12 '11 12:08

Denees


People also ask

What is an external IP address?

An external IP address is the address assigned to you by your ISP (Internet service provider) that the Internet and other computers outside your local network use to identify you.

What is the IP address of the client?

Client IP addresses describe only the computer being used, not the user. If multiple users share the same computer, they will be indistinguishable. Many Internet service providers dynamically assign IP addresses to users when they log in.

What is external and internal IP address?

Your internal IP address is for your local network only, it exists so that your router (the device connecting you to the internet) can tell the difference between your computer, your cell phone, a printer, or other devices are while they are connected to it, while your external IP address is the IP address of your ...


1 Answers

If your client is using NAT (network address translation) it may not have an external address. Most often, in my experience, this is the case. At work, my web requests go through a proxy so the web server can only determine this address. At home I use NAT via a server so this laptop I'm typing on has no external address. The closest thing is what is returned from 'whatismyip', my server address, through which I may sometimes forward ports that go to my laptop.

like image 62
sje397 Avatar answered Sep 19 '22 17:09

sje397