Menu

#2 Wrong IP-Adress on Linux

1.0.1
open
5
2017-12-11
2006-06-22
No

I was using openchord library, and when I tried to
connect using a remote network, it displayed an unknown
error. I ran an application in Linux, and I saw when
creating a new network that the IP was 127.0.0.1. The
functionjava.net.InetAddress.getLocalHost().getHostAddress()
in Linux returns 127.0.0.1 instead of the real IP. I
modified the source code and made a new function to
obtain the IP.

private String getHostAddress() throws Exception{
try{

if(System.getProperty("os.name").equals("Linux")){
Enumeration e =
NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface netface =
(NetworkInterface)e.nextElement();
if
(!netface.getName().equals("lo")){
Enumeration e2 =
netface.getInetAddresses();

while (e2.hasMoreElements()){
e2.nextElement();
InetAddress ip =
(InetAddress) e2.nextElement();
return
ip.toString().substring(1);
}
}
}
throw new Exception("Unknow Exception");
}else
return
java.net.InetAddress.getLocalHost().getHostAddress();
}
catch(Exception e){
throw new Exception("Could not create url
for this host!", e);
}
}

The new method to obtain the ip address of a node was
put into class
de.uniba.wiai.lspi.chord.console.command.RemoteChordNetworkAccess:

Discussion

  • Nobody/Anonymous

    Logged In: NO

    This is a common problem: it is due to the order of ip adresses in the /etc/hosts file on Linux systems. The localhost entry 127.0.0.1 is always the first listed one and Java will always take the first one. We had the same problem using an implementation of Bunshin DHT.

     
  • Nobody/Anonymous

    Logged In: NO

    Good answer!

     
  • Nobody/Anonymous

    Logged In: NO

    while (e2.hasMoreElements()){
    e2.nextElement();
    InetAddress ip =
    (InetAddress) e2.nextElement();
    return
    ip.toString().substring(1);
    }

    No, this code should be as follow:

    e2.nextElement();
    while (e2.hasMoreElements()){
    InetAddress ip =
    (InetAddress) e2.nextElement();
    return
    ip.toString().substring(1);
    }

     
  • Jan Scholz

    Jan Scholz - 2017-12-11

    I fixed the issue

    you get the the first reasonable ipv4 address with this snippet, works on windows and linux
    the changed file can be downloaded here

    String host = "";
                Enumeration<NetworkInterface> nets = java.net.NetworkInterface.getNetworkInterfaces();
                start:
                for (NetworkInterface netint : Collections.list(nets)) {
                    for (InetAddress addr : Collections.list(netint.getInetAddresses())) {
                        if(!addr.isLinkLocalAddress() && !addr.isLoopbackAddress()) {
                           // change to Inet6Address if you prefer ip6
                            if (addr instanceof java.net.Inet4Address) { 
                                host = addr.getHostAddress().toString();    
                                break start;
                            }
                        }
                    }
                }
    
     

    Last edit: Jan Scholz 2017-12-11

Log in to post a comment.

MongoDB Logo MongoDB