干嘛要本机MAC啊?只能用RAW socket了。

解决方案 »

  1.   

    还有一个手段么,就是通过  Runtime命令来调 ifconfig 或ipconfig 命令,从命令行的输出里分析得到MAC地址 
      

  2.   

    直接给出代码,略作修改就可以根据InetAddress获取MAC地址
    package learning;import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;public class SocketMac { public static void main(String[] args) {
    try {
    InetAddress adress = InetAddress.getLocalHost();
    NetworkInterface net = NetworkInterface.getByInetAddress(adress);
    byte[] macBytes = net.getHardwareAddress();
    StringBuffer buffer = new StringBuffer();
    for(int i = 0; i < macBytes.length; i++){
    if(i != 0)
    buffer.append("-");
    String str = Integer.toHexString(macBytes[i]&0xff);
    if(str.length() == 0){
    buffer.append("0");
    }
    buffer.append(str);
    }
    System.out.println(buffer.toString());
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (SocketException e) {
    e.printStackTrace();
    }
    }}