不会的吧!
我这里输出的就是本机的IP,而不是127.0.0.1
并且在《java核心技术卷II》中提到
“如果你仅要求得到localhost的地址,那么你总是得到地址127.0.0.1,
这个地址并不是非常有用,可以使用静态方法getLocalHost来获取你的本地主机地址。
InetAddress address = InetAddress.getLocalHost();”
我这里输出的就是本机的IP,而不是127.0.0.1
并且在《java核心技术卷II》中提到
“如果你仅要求得到localhost的地址,那么你总是得到地址127.0.0.1,
这个地址并不是非常有用,可以使用静态方法getLocalHost来获取你的本地主机地址。
InetAddress address = InetAddress.getLocalHost();”
String add = InetAddress.getLocalHost().getHostName() ;
System.out.println("add = " + add);
add = InetAddress. getByName(add).getHostAddress();
System.out.println("add = " + add);
try {
InetAddress addr = InetAddress.getByName("javaalmanac.com");
byte[] ipAddr = addr.getAddress();
// Convert to dot representation
String ipAddrStr = "";
for (int i=0; i<ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i]&0xFF;
}
} catch (UnknownHostException e) {
}
返回本机地址可用InetAddress.getByName()或InetAddress.getByHost()的静态方法。
看看电力的《Java网络编程》,是本好书。