65

I am trying to print the IP adress of the logged user in my webApplication. If a user connects from another PC (which is under the same network, as the web application is running in my pc) using the IP address 192.168.10.120:8080/WebApplication the code request.getRemoteAddr() or request.getLocalAddr()) returns his IP address. When I log in from my pc which run the web application, I get this IP address 0:0:0:0:0:0:0:1.

Why is that? And what's the difference between these commands (which should I use?).Thank you a lot!

yaylitzis
  • 5,354
  • 17
  • 62
  • 107
  • 14
    localhost would return that IP. On other computers you will see actual IP. – Makky Jul 31 '13 at 07:38
  • 1
    Thanks @Makky do you know the difference between those 2 commands? – yaylitzis Jul 31 '13 at 07:39
  • `getLocalAddr()` gets the IP address of the machine to receive the request. [Source](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getLocalAddr()). In contrast, `getRemoteAddr()` obtains the IP address of the machine to request the resource [Source](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRemoteAddr()). – Michael Wiggins May 18 '15 at 10:58
  • 1
    instead of giving url as "http://localhost:port/" provide url as "http://IP:port/" You will get remote address. – Atul Kumar Khare Sep 15 '17 at 06:22
  • This doesn't answer the question, and there are already accepted answers. – ziggystar Sep 15 '17 at 06:50
  • surprisingly enough, this helped – Cargeh May 08 '18 at 12:01
  • This helped too for debugging purpose. – wonsuc Dec 10 '18 at 06:58

5 Answers5

67

As we move over to IPv6 from IPv4, they are changing the loopback address (localhost) to 0:0:0:0:0:0:0:1 from 127.0.0.1 thats why you are getting this address.

As for the functions:

  • getRemoteAddr() returns the clients IP

  • getLocalAddr() returns the IP of the server the application is running on

Javasick
  • 2,753
  • 1
  • 23
  • 35
Robert Pounder
  • 1,490
  • 1
  • 14
  • 29
64

In your case, as you are trying to access it on your local machine,so it will return that value. But let one of your friend access it, and you will receive the expected result with getRemoteAddr

From the javadoc:

getRemoteAddr - Returns the IP address of the client or last proxy that sent the request

getLocalAddr - Returns the IP address of the interface on which the request was received.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
9

getRemoteAddr() solves your purpose but if client is behind any proxy then you will get IP address of proxy :

Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.

getLocalAddr() returns the IP of the server the application is running on.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
1

If you want to get 127.0.0.1 instead of 0:0:0:0:0:0:0:1 start your application server process with the following property CATALINA_OPTS="$CATALINA_OPTS -Djava.net.preferIPv4Stack=true"

Ivan JDev
  • 11
  • 3
0

If using java and you need to know the client IP address or the IP address of the localmachine if the application is running on localhost:

String ip = "unknown"; 
try{
    ip = request.getRemoteAddr();
    if(ip.equals("0:0:0:0:0:0:0:1") || ip.equals("127.0.0.1")) {
        InetAddress hostAddress = InetAddress.getLocalHost();
        ip = hostAddress.getHostAddress();
    }
} catch (UnknownHostException e) {
    log.info("got unknown host");
    ip = "unknown";
}