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
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…