1

Tried to use the getHardwareAddress() method to get the mac address of my network card, however according to NetworkInterface.getNetworkInterfaces() I have over 20 network interfaces. Ive narrowed it down to one by checking other attributes, but is this going to work in all (or at least most cirumstances) and operating systems, only tried on a WIndows 7 PC.

And if a computer has two network cards, can I find out which one is actually being used for a current internet connection ?

Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
   NetworkInterface ni = e.nextElement();
   if(ni!=null && ni.getHardwareAddress()!=null &&ni.getInetAddresses().hasMoreElements() && ni.supportsMulticast())
   {
      String macAddress   = Hex.encodeHexString(ni.getHardwareAddress());
      System.out.println("MACADDRESSIS"+macAddress);
   }
}
4

1 回答 1

0

如果您有 InetAddress,则可以使用NetworkInterface.getByInetAddress().

于 2012-09-03T14:24:46.163 回答