我需要連接到我的家庭網絡的所有遠程計算機的MAC地址。我可以閱讀我自己的機器的MAC地址使用下面的代碼,但我不知道如何獲得我的網絡上的其他機器的MAC地址。或者是否有可能。
import java.net.InetAddress;
import java.net.NetworkInterface;
class Test {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
}
}
輸出(我的電腦的6byte MAC地址)
30-60-77-12-CD-99
爲了什麼目的?除了顯示它之外,您可以使用Java中的MAC地址做任何有用的事情。 – EJP