How do Java get local ip for multiple network CARDS

  • 2020-05-09 18:30:32
  • OfStack

Without further ado, I directly attached the code to you, as shown below:


public String getLocalHostName() {
String hostName;
try {
InetAddress addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
} catch (Exception ex) {
hostName = "";
}
return hostName;
}
public String[] getAllLocalHostIP() {
String[] ret = null;
try {
String hostName = getLocalHostName();
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();
}
}
}
} catch (Exception ex) {
ret = null;
}
return ret;
}

The above code is the site to introduce you java access to the multi-network card local ip code, any questions please leave me a message, I will communicate with you in a timely manner, learning and progress together, at the same time, thank you very much for the support of the site website!


Related articles: