mac地址问题

时间:2011-06-23 07:12:56

标签: java networking

我想在lan中获取另一台计算机的mac地址,我该怎么办?我正在使用JPCAP

 private void getPackage(JpcapCaptor captor)
  {
      try{
            IPPacket ip = (IPPacket)captor.getPacket();

            for(int a =0 ; a <found.size(); a++ )
            {
                  if(ip.dst_ip.equals(found.get(a)))
                      check = true;

            }

            if(!check)
            {
                 if(ip.dst_ip.isSiteLocalAddress())
                 {
                    host = ip.dst_ip.getHostName();
                      System.out.println("destination ip : " + ip.dst_ip +  " " + ip.dst_ip.getHostName());
                      System.out.println("Source ip : " + ip.src_ip +  " " + ip.src_ip.getHostName());


                                          found.addElement(ip.dst_ip);

                  }
             }

              check = false;
        }catch(Exception ex)
        {
            //Sys.alert("Error" ,"lobby exeption :" +  ex);
            //wegens null reverence exeption
        }
  }

此代码只获取IP地址,但我也想要mac地址

1 个答案:

答案 0 :(得分:3)

http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/javadoc/jpcap/packet/ARPPacket.html

您需要以下方法:

public java.lang.Object getSenderHardwareAddress()

package jpcapExample;
import jpcap.*;
import jpcap.packet.*;
class PacketPrinter implements PacketReceiver {
    public void receivePacket(Packet packet) {
        if (packet instanceof ARPPacket){
            ARPPacket arpp = (ARPPacket) packet;
            Object str = arpp.getSenderHardwareAddress();
            System.out.println("got arp from: " + str.toString());

        }
        System.out.println(packet);
        }
    }
---------------------[ different file ]-------------
    try {
    JpcapCaptor eth0=
        JpcapCaptor.
        openDevice(devices[eth0idx], 65535, false, 20000);
    while (true){
        eth0.processPacket(1233,new PacketPrinter());
    }
} catch (IOException io){
    io.printStackTrace();
}