Java獲取網卡信息

gd7g 9年前發布 | 4K 次閱讀 Java

InterfaceAddress 類表示一個由名稱和分配給此接口的 IP 地址列表組成的網絡接口。它用于標識加入多播組的本地接口。 接口通常是按名稱(如 "le0")區分的。

NetworkParameterDemo.java

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class NetworkParameterDemo { public static void main(String[] args) throws Exception { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface ni = en.nextElement(); printParameter(ni);

}

}

public static void printParameter(NetworkInterface ni) throws SocketException { System.out.println(" Name = " + ni.getName()); System.out.println(" Display Name = " + ni.getDisplayName()); System.out.println(" Is up = " + ni.isUp()); System.out.println(" Support multicast = " + ni.supportsMulticast()); System.out.println(" Is loopback = " + ni.isLoopback()); System.out.println(" Is virtual = " + ni.isVirtual()); System.out.println(" Is point to point = " + ni.isPointToPoint()); System.out.println(" Hardware address = " + ni.getHardwareAddress()); System.out.println(" MTU = " + ni.getMTU());

System.out.println("\nList of Interface Addresses:");
List<InterfaceAddress> list = ni.getInterfaceAddresses();
Iterator<InterfaceAddress> it = list.iterator();

while (it.hasNext()) {
  InterfaceAddress ia = it.next();
  System.out.println(" Address = " + ia.getAddress());
  System.out.println(" Broadcast = " + ia.getBroadcast());
  System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
  System.out.println("");
}

} }</pre>

 本文由用戶 gd7g 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!