Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#2 Wrong IP-Adress on Linux

1.0.1
open
Sven Kaffille
5
2006-06-22
2006-06-22
Sven Kaffille
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

  • 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.

     
  • Logged In: NO

    Good answer!

     
  • 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);
    }