0

Why i am gettng IP Addres as 127.0.0.1

This is my sample program for what i am using for getting the IP Address

package com;

import java.net.InetAddress;    
import org.apache.log4j.Logger;

public class Test {
    private static final Logger logger = Logger.getLogger(Test.class);

    public static void main(String args[]) throws Exception {
        String ips = InetAddress.getLocalHost().getHostAddress().trim();
        System.out.println(ips);
    }

}
Lion
  • 18,729
  • 22
  • 80
  • 110
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 9
    because 127.0.0.1 is `localhost`? – Shark Jun 26 '13 at 12:32
  • 1
    See [this question](http://stackoverflow.com/questions/8765578/get-local-ip-address-without-connecting-to-the-internet), it uses interface enumeration to find an interface which isn't the loopback one. – Jacopofar Jun 26 '13 at 12:34
  • The IP address 127.0.0.1 is a special purpose address reserved for use on each computer. 127.0.0.1 is conventionally a computer's loopback address. Network software and utilities can use 127.0.0.1 to access a local computer's TCP/IP network resources. Messages sent to loopback IP addresses like 127.0.0.1 don't reach outside the local area network (LAN) but instead are automatically re-routed by the computer's own network adapter back to the receiving end of the TCP/IP stack. More over, [see](http://superuser.com/q/31824). – Lion Jun 26 '13 at 12:39

7 Answers7

7

That is the IP address from your local server (= localhost). It is used when you are accessing the computer the program is running on..

Mtvw
  • 336
  • 1
  • 7
3
Enumeration e=NetworkInterface.getNetworkInterfaces();

while(e.hasMoreElements())
{
    NetworkInterface n=(NetworkInterface) e.nextElement();
    Enumeration ee = n.getInetAddresses();
    while(ee.hasMoreElements())
    {
        InetAddress i= (InetAddress) ee.nextElement();
        System.out.println(i.getHostAddress());
    }
}

You can use above code to get ip addresses of your machine.

See Source

Community
  • 1
  • 1
Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
1

Your localhost is 127.0.0.1. See the InetAddress#getLocalHost()

Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.

A localhost is, put plainly, the standard hostname given to the address assigned to the loopback network interface. Translated into an IP address, a localhost is always designated as 127.0.0.1.

As per Wikipedia:

In computer networking, localhost means this computer. It is a hostname that the computer's software and users may employ to access the computer's own network services via its loopback network interface. On most computer systems, localhost resolves to the address 127.0.0.1, which is the most-commonly used IPv4 loopback address, and to the IPv6 loopback address ::1.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Note that while the "hostname `localhost`" resolves to 127.0.0.1, the hostname of the local host may resolve to another address. They are two different things. – Joni Jun 26 '13 at 12:42
1

You get 127.0.0.1 on your system because that's what the machine's name resolves to. On other systems you may get a different address.

For example, I get 127.0.1.1 because my machine is called koivu and I have this line in my /etc/hosts:

127.0.1.1   koivu

If I had some other IP address I would get that address instead.

Joni
  • 108,737
  • 14
  • 143
  • 193
1

This one give ip instead of localhost address

import java.net.*;
import java.io.*;

public class GetIPAddress {

    public static void main(String[] args) {
        try {
            InetAddress thisIp = InetAddress.getLocalHost();
            System.out.println("IP:" + thisIp.getHostAddress());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Lion
  • 18,729
  • 22
  • 80
  • 110
Ganesh Rengarajan
  • 2,006
  • 13
  • 26
1

For example this is returning my LAN IP address: System.out.println(InetAddress.getLocalHost().getHostAddress());

You have multiple IP addresses probably and that is causing your troubles.

Balint Bako
  • 2,500
  • 1
  • 14
  • 13
0

Because you are accessing your application from your localhost

fareed
  • 3,034
  • 6
  • 37
  • 65