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.
- I need the client to register its PPP IP if available;
- Otherwise the LAN IP if available;
- Otherwise it must register 127.0.0.1 assuming its the same computer.
Using the code:
System.out.println(Inet4Address.getLocalHost().getHostAddress());
or
System.out.println(InetAddress.getLocalHost().getHostAddress());
My PPP Connection IP address is: 117.204.44.192 but the above returns me 192.168.1.2
EDIT
I am using the following code:
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());
}
}
I am able to get all the IP addresses associated all NetworkInterface
s, but how do I distinguish them? This is the output I am getting:
127.0.0.1
192.168.1.2
192.168.56.1
117.204.44.19
When you are looking for your "local" address, you should note that each machine has not only a single network interface, and each interface could has its own local address. Which means your machine is always owning several "local" addresses.
Different "local" addresses will be automatically chosen to use when you are connecting to different endpoints. For example, when you connect to
google.com
, you are using an "outside" local address; but when you connect to yourlocalhost
, your local address is alwayslocalhost
itself, because localhost is just a loopback.Below is showing how to find out your local address when you are communicating with
google.com
:Posting here tested IP ambiguity workaround code from https://issues.apache.org/jira/browse/JCS-40 (InetAddress.getLocalHost() ambiguous on Linux systems):
You can use java's InetAddress class for this purpose.
Output for my system =
IP of my system is := 10.100.98.228
getHostAddress() returns
OR you can also do
Output =
IP of my system is := RanRag-PC/10.100.98.228
EDIT 1: Updated code, since the previous link, exists no more
ACTUAL VERSION: This stopped working
Hopefully this snippet might help you to achieve this :
A rather simplistic approach that seems to be working...
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?
NetworkInterface.getNetworkInterfaces()
to get all of the known network interfaces on the host, and then iterate over each NI's addresses.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?)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.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.