Java – Getting the IP address of the current machine using Java


I am trying to develop a system where there are different nodes that are run on different system or on different ports on the same system.

Now all the nodes create a Socket with a target IP as the IP of a special node known as a bootstrapping node. The nodes then create their own ServerSocket and start listening for connections.

The bootstrapping node maintains a list of Nodes and returns them on being queried.

Now what I need is the node must register its IP to the bootstrapping node. I tried using cli.getInetAddress() once the client connects to the ServerSocket of bootstrapping node but that didn't work.

  1. I need the client to register its PPP IP if available;
  2. Otherwise the LAN IP if available;
  3. Otherwise it must register assuming its the same computer.

Using the code:




My PPP Connection IP address is: but the above returns me


I am using the following code:

Enumeration e = NetworkInterface.getNetworkInterfaces();
    NetworkInterface n = (NetworkInterface) e.nextElement();
    Enumeration ee = n.getInetAddresses();
    while (ee.hasMoreElements())
        InetAddress i = (InetAddress) ee.nextElement();

I am able to get all the IP addresses associated all NetworkInterfaces, but how do I distinguish them? This is the output I am getting:

Best Answer

This could be a bit tricky in the most general case.

On the face of it, InetAddress.getLocalHost() should give you the IP address of this host. The problem is that a host could have lots of network interfaces, and an interface could be bound to more than one IP address. And to top that, not all IP addresses will be reachable outside of your machine or your LAN. For example, they could be IP addresses for virtual network devices, private network IP addresses, and so on.

What this means is that the IP address returned by InetAddress.getLocalHost() might not be the right one to use.

How can you deal with this?

  • One approach is to use NetworkInterface.getNetworkInterfaces() to get all of the known network interfaces on the host, and then iterate over each NI's addresses.
  • Another approach is to (somehow) get the externally advertized FQDN for the host, and use InetAddress.getByName() to look up the primary IP address. (But how do you get it, and how do you deal with a DNS-based load balancer?)
  • A variation of the previous is to get the preferred FQDN from a config file or a command line parameter.
  • Another variation is to get the preferred IP address from a config file or a command line parameter.

In summary, InetAddress.getLocalHost() will typically work, but you may need to provide an alternative method for the cases where your code is run in an environment with "complicated" networking.

I am able to get all the IP addresses associated all Network Interfaces, but how do i distinguish them?

  • Any address in the range is a "loopback" address. It is only visible to "this" host.
  • Any address in the range is a private (aka site local) IP address. These are reserved for use within an organization. The same applies to addresses, and through
  • Addresses in the range are link local IP addresses. These are reserved for use on a single network segment.
  • Addresses in the range through are multicast addresses.
  • The address is the broadcast address.
  • Anything else should be a valid public point-to-point IPv4 address.

In fact, the InetAddress API provides methods for testing for loopback, link local, site local, multicast and broadcast addresses. You can use these to sort out which of the IP addresses you get back is most appropriate.