I have find two examples on the web to get the ip address the router has given to my pc. Here is the code:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class tryNet {
public static void displayStuff(String whichHost, InetAddress inetAddr) {
System.out.println("---------------------");
System.out.println("host: " + whichHost);
System.out.println("Canonical host name: " + inetAddr.getCanonicalHostName());
System.out.println("Host Name: " + inetAddr.getHostName());
System.out.println("Host Address: " + inetAddr.getHostAddress());
System.out.println("---------------------");
}
public static void main(String argv[]) {
try {
InetAddress inetAddr = InetAddress.getLocalHost();
displayStuff("localhost", inetAddr);
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
I have read that after having initialized InetAddress inetAddr = InetAddress.getLocalHost(); I can use the method inetAddr.getHostAddress() to get my ip address, the one given by my router (such as write ifconfig in the terminal in ubuntu, or ipconfig in windows) Instead it returns me my loopback address...(127.0.0.1) Why?