java.net.NetworkInterface.getHardwareAddress()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(1577)

本文整理了Java中java.net.NetworkInterface.getHardwareAddress()方法的一些代码示例,展示了NetworkInterface.getHardwareAddress()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NetworkInterface.getHardwareAddress()方法的具体详情如下:
包路径:java.net.NetworkInterface
类名称:NetworkInterface
方法名:getHardwareAddress

NetworkInterface.getHardwareAddress介绍

[英]Returns the hardware address of the interface, if it has one, or null otherwise.
[中]返回接口的硬件地址(如果有),否则返回null。

代码示例

代码示例来源:origin: netty/netty

@Override
  public byte[] run() throws SocketException {
    return intf.getHardwareAddress();
  }
});

代码示例来源:origin: redisson/redisson

@Override
  public byte[] run() throws SocketException {
    return intf.getHardwareAddress();
  }
});

代码示例来源:origin: wildfly/wildfly

@Override
  public byte[] run() throws SocketException {
    return intf.getHardwareAddress();
  }
});

代码示例来源:origin: robovm/robovm

/**
 * Returns the hardware address of the interface, if it has one, or null otherwise.
 *
 * @throws SocketException if an I/O error occurs.
 * @since 1.6
 */
public byte[] getHardwareAddress() throws SocketException {
  // RoboVM note: Changed to call native code instead of reading from /sys/class/net
  return getHardwareAddress(name);
}

代码示例来源:origin: 4thline/cling

public byte[] getHardwareAddress(InetAddress inetAddress) {
  try {
    NetworkInterface iface = NetworkInterface.getByInetAddress(inetAddress);
    return iface != null ? iface.getHardwareAddress() : null;
  } catch (Throwable ex) {
    log.log(Level.WARNING, "Cannot get hardware address for: " + inetAddress, ex);
    // On Win32: java.lang.Error: IP Helper Library GetIpAddrTable function failed
    // On Android 4.0.3 NullPointerException with inetAddress != null
    // On Android "SocketException: No such device or address" when
    // switching networks (mobile -> WiFi)
    return null;
  }
}

代码示例来源:origin: 4thline/cling

/**
 * @return The MAC hardware address of the first network interface of this host.
 */
public static byte[] getFirstNetworkInterfaceHardwareAddress() {
  try {
    Enumeration<NetworkInterface> interfaceEnumeration = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface iface : Collections.list(interfaceEnumeration)) {
      if (!iface.isLoopback() && iface.isUp() && iface.getHardwareAddress() != null) {
        return iface.getHardwareAddress();
      }
    }
  } catch (Exception ex) {
    throw new RuntimeException("Could not discover first network interface hardware address");
  }
  throw new RuntimeException("Could not discover first network interface hardware address");
}

代码示例来源:origin: plantuml/plantuml

private static String getMacAddress() throws IOException {
  final Enumeration<NetworkInterface> net = NetworkInterface.getNetworkInterfaces();
  final StringBuilder result = new StringBuilder();
  while (net.hasMoreElements()) {
    final NetworkInterface element = net.nextElement();
    byte[] mac = element.getHardwareAddress();
    if (mac != null) {
      for (byte b : mac) {
        result.append(String.format("%02x", b));
      }
    }
  }
  return result.toString();
}

代码示例来源:origin: neo4j/neo4j

private static String determineMacAddress()
{
  String formattedMac = "0";
  try
  {
    InetAddress address = InetAddress.getLocalHost();
    NetworkInterface ni = NetworkInterface.getByInetAddress( address );
    if ( ni != null )
    {
      byte[] mac = ni.getHardwareAddress();
      if ( mac != null )
      {
        StringBuilder sb = new StringBuilder( mac.length * 2 );
        Formatter formatter = new Formatter( sb );
        for ( byte b : mac )
        {
          formatter.format( "%02x", b );
        }
        formattedMac = sb.toString();
      }
    }
  }
  catch ( Throwable t )
  {
    //
  }
  return formattedMac;
}

代码示例来源:origin: cSploit/android

@Nullable
public byte[] getLocalHardware() {
 try {
  return mInterface.getHardwareAddress(); //FIXME: #831
 } catch (SocketException e) {
  System.errorLogging(e);
 }
 return null;
}

代码示例来源:origin: SonarSource/sonarqube

@CheckForNull
private static byte[] getMacAddress() throws SocketException {
 Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
 if (en != null) {
  while (en.hasMoreElements()) {
   NetworkInterface nint = en.nextElement();
   if (!nint.isLoopback()) {
    // Pick the first valid non loopback address we find
    byte[] address = nint.getHardwareAddress();
    if (isValidAddress(address)) {
     return address;
    }
   }
  }
 }
 // Could not find a mac address
 return null;
}

代码示例来源:origin: ctripcorp/apollo

NetworkInterface ni = e.nextElement();
sb.append(ni.toString());
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
 ByteBuffer bb = ByteBuffer.wrap(mac);

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws UnknownHostException, SocketException {
 System.out.println("Current IP address : " + InetAddress.getLocalHost().getHostAddress());

 for(NetworkInterface network : IterableEnumeration.make(NetworkInterface.getNetworkInterfaces())) {
  byte[] mac = network.getHardwareAddress();
  if(mac != null) {
   System.out.print("Current MAC address : ");
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < mac.length; i++) {
    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
   }
   System.out.println(sb.toString());
   //Bound InetAddress for interface
   for(InetAddress address : IterableEnumeration.make(network.getInetAddresses())) {
    System.out.println("\tBound to:"+address.getHostAddress());
   }
  }
 }
}

代码示例来源:origin: baomidou/mybatis-plus

/**
 * 数据标识id部分
 */
protected static long getDatacenterId(long maxDatacenterId) {
  long id = 0L;
  try {
    InetAddress ip = InetAddress.getLocalHost();
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    if (network == null) {
      id = 1L;
    } else {
      byte[] mac = network.getHardwareAddress();
      if (null != mac) {
        id = ((0x000000FF & (long) mac[mac.length - 1]) | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
        id = id % (maxDatacenterId + 1);
      }
    }
  } catch (Exception e) {
    logger.warn(" getDatacenterId: " + e.getMessage());
  }
  return id;
}

代码示例来源:origin: bwssytems/ha-bridge

byte[] mac = network.getHardwareAddress();

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) {
 try {
  InetAddress ip = InetAddress.getLocalHost();
  System.out.println("Current IP address : " + ip.getHostAddress());

  Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
  while(networks.hasMoreElements()) {
   NetworkInterface network = networks.nextElement();
   byte[] mac = network.getHardwareAddress();

   if(mac != null) {
    System.out.print("Current MAC address : ");

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
     sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }
    System.out.println(sb.toString());
   }
  }
 } catch (UnknownHostException e) {
  e.printStackTrace();
 } catch (SocketException e){
  e.printStackTrace();
 }
}

代码示例来源:origin: looly/hutool

mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
} catch (SocketException e) {
  throw new UtilException(e);

代码示例来源:origin: looly/hutool

mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
} catch (SocketException e) {
  throw new UtilException(e);

代码示例来源:origin: wildfly/wildfly

@Override
 public byte[] call() throws Exception {
   boolean up = networkInterface.isUp();
   boolean loopback = networkInterface.isLoopback();
   boolean virtual = networkInterface.isVirtual();
   if (loopback || virtual || !up) {
    throw new Exception("not suitable interface");
   }
   byte[] address = networkInterface.getHardwareAddress();
   if (address != null) {
    byte[] paddedAddress = UUIDGenerator.getZeroPaddedSixBytes(address);
    if (UUIDGenerator.isBlackList(address)) {
      throw new Exception("black listed address");
    }
    if (paddedAddress != null) {
      return paddedAddress;
    }
   }
   throw new Exception("invalid network interface");
 }
});

代码示例来源:origin: Graylog2/graylog2-server

private NetworkStats.Interface buildInterface(NetworkInterface networkInterface) throws SocketException {
  final String macAddress = bytesToMacAddressString(networkInterface.getHardwareAddress());
  List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
  final Set<String> addresses = new HashSet<>(interfaceAddresses.size());
  for (InterfaceAddress interfaceAddress : interfaceAddresses) {
    addresses.add(interfaceAddress.getAddress().getHostAddress());
  }
  return NetworkStats.Interface.create(
      networkInterface.getName(),
      addresses,
      macAddress,
      networkInterface.getMTU(),
      null
  );
}

代码示例来源:origin: apache/ignite

/**
 * Returns host identifier by node identifier.
 *
 * @param nodeId Node identifier.
 * @return Host identifier.
 */
private HostIdentifier getHostIdentifier(UUID nodeId) {
  try {
    ClusterGroup grp = ignite.cluster().forNodeId(nodeId);
    return ignite.compute(grp).call(() -> {
      Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
      List<byte[]> macAddrs = new ArrayList<>();
      while (interfaces.hasMoreElements()) {
        NetworkInterface netItf = interfaces.nextElement();
        byte[] macAddr = netItf.getHardwareAddress();
        macAddrs.add(macAddr);
      }
      return new HostIdentifier(macAddrs.toArray(new byte[macAddrs.size()][]));
    });
  }
  catch (ClusterGroupEmptyException e) {
    return null;
  }
}

相关文章

微信公众号

最新文章

更多