我試圖執行以下代碼。我是Java新手,所以這是我第一次在java.net中。有一個在程序中沒有錯誤,但我得到本地主機地址192.168.56.1作爲我的,而IP是192.168.2.10getLocalHost()顯示錯誤的IP地址
import java.net.*;
class InetAddressDemo
{
public static void main(String[] args)
{
try
{
InetAddress address = InetAddress.getLocalHost();
System.out.println("\nLocalhost Address : " + address + "\n");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
你可以在這裏找到一個很好的解釋關於那個http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java –