你好我使用下面的代碼從Java小程序 本地計算機的IP,但我總是得到127.0.0.1,而不是實際IP的如何從Java小程序在本地計算機的IP
public String ip;
public void init()
{
try
{
Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
for (; n.hasMoreElements();)
{
NetworkInterface e = n.nextElement();
Enumeration<InetAddress> a = e.getInetAddresses();
for (; a.hasMoreElements();)
{
InetAddress addr = a.nextElement();
ip = "Really " + addr.getHostAddress();
System.out.println(ip);
}
}
}
catch(Exception ex)
{
}
}
看看http://stackoverflow.com/questions/1510526/get-the-correct-local-ip-adress-from-java-applet – Batty
*「獲取本地機器ip」*爲什麼? –
其中一個客戶端要求 – user3373819