java.net.InetAddress 가져오기;
java.net.NetworkInterface 가져오기;
import java.net.SocketException;
import java.util.Enumeration;
공개 클래스 InternetTest {
공개 정적 무효 메인(String[] args) {
String netCard = "lo";
노력하다 {
열거<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
if (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下会取到父接口??
열거<NetworkInterface> subnetInterfaces = netInterface
.getSubInterfaces();
동안(subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = 서브넷 인터페이스
.nextElement();
System.out.println(subnetInterface.getName());
Enumeration<InetAddress> 하위 주소 = netInterface
.getInetAddresses();
동안(하위 주소.hasMoreElements()) {
InetAddress 하위 주소 = subaddresses.nextElement();
System.out.println(subaddress.getHostAddress());
}
}
// 打印接口下所有IP
System.out.println(netInterface.getName());
열거<InetAddress> 주소 = netInterface
.getInetAddresses();
동안(addresses.hasMoreElements()) {
InetAddress 주소 = Address.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} 잡기(SocketException e) {
e.printStackTrace();
}
}
}