我需要连接到我的家庭网络的所有远程计算机的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