1
我在编写虚拟路由器。我的第一个任务是构建一个以太网帧。我目前正在尝试获取MAC源地址。我有代码获取我的机器上使用的所有MAC地址,但我有一个virtualbox主机网络,所以代码也抓取了这个MAC地址。我无法通过编程确定我的以太网帧应该使用哪个MAC地址。这是我当前的代码在java中获取正确的非虚拟MAC地址
private byte[] grabMACAddress(){
try{
InetAddress[] addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
for(int i = 0; i < addresses.length; i++){
NetworkInterface ni = NetworkInterface.getByInetAddress(addresses[i]);
mac = ni.getHardwareAddress();
if (mac != null){
for(int j=0; j < mac.length; j++) {
String part = String.format("%02X%s", mac[j], (j < mac.length - (1)) ? "" : "");
s += part;
}
System.out.println();
}
else{
System.out.println("Address doesn't exist or is not accessible.");
}
}
}
catch(IOException e){
}
System.out.println(s);
return mac;
}
谢谢!我用你的技术连接到公共服务器。它像一个魅力。 –