java查看本机hostName可代表的ip列表
import java.net.InetAddress;
public class ent {
public static void main(String[] args) {
String[] s = getAllLocalHostIP();
}
public static String[] getAllLocalHostIP() {
String[] ret = null;
try {
String hostName = InetAddress.getLocalHost().getHostName();
System.out.println("获取到的locasthost的Name为" + hostName + "可代表的IP列表:");
if (hostName.length() > 0) {
InetAddress[] addrs = InetAddress.getAllByName(hostName);
if (addrs.length > 0) {
ret = new String[addrs.length];
for (int i = 0; i < addrs.length; i++) {
ret[i] = addrs[i].getHostAddress();
System.out.println("IP" + i + " : " + ret[i]);
}
}
}
} catch (Exception ex) {
ret = null;
}
return ret;
}
}