wood burning stoves 2.0*
The moose likes Web Services and the fly likes Need to get the TRUE client IP address Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "Need to get the TRUE client IP address" Watch "Need to get the TRUE client IP address" New topic
Author

Need to get the TRUE client IP address

Christopher Swearingen
Greenhorn

Joined: May 02, 2012
Posts: 8
I've been searching the web and CodeRanch forum boards all morning.

I need to get the IP address for the browser's machine and not the host's IP. The reason is that I've got several kiosk machines on the WAN that display a specific intranet page and I need to monitor them. The kiosks all get their IP via DHCP. I want to have a jQuery AJAX call that will pass the IP address and session ID so I can record it in a database. So I either need to get the IP address client-side via Javascript or use the HTTP Request server-side. I need to retrieve the browser's private network IP (e.g. 192.168.1.1, 10.167.1.37, etc.) but the problem is I don't think I can get the local IP via the request?

To test the process I've got a tomcat server running locally on my dev machine and I tried testing request.getLocalAddr(), request.getHeader("x-forwarded-for"), etc. Everything I try returns server info rather than client (127.0.0.1/localhost/null/etc.).

Anyone have any resources they can point me to? Using a third part site like http://jsonip.appspot.com/?callback=getip won't work as that gives the external IP Address.
Christopher Swearingen
Greenhorn

Joined: May 02, 2012
Posts: 8
For any interested I was able to accomplish this. Since all the computers I need to monitor are on the WAN (and behind the same firewall) I created a class that implements ServletRequestAware. That class uses the Request object to get the IP address via .getRemoteAddr(). Then the IP address is fed through java.net.InetAddress.getByName(ip_address) to get the hostname. Worked for me!
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Glad to hear you found a solution for your case. You do realize, of course, that this is not a general solution. These days its rare for a client to have a real IP address, they are usually behind at least one level of NAT routing, so you can't get the real address.
Christopher Swearingen
Greenhorn

Joined: May 02, 2012
Posts: 8
Pat Farrell wrote:Glad to hear you found a solution for your case. You do realize, of course, that this is not a general solution. These days its rare for a client to have a real IP address, they are usually behind at least one level of NAT routing, so you can't get the real address.


Yeah this was one very specific instance where I needed to get this working. But I figure if I came across the need then someone else would too. ;)
 
GeeCON Prague 2014
 
subject: Need to get the TRUE client IP address