List of usage examples for java.net NetworkInterface getNetworkInterfaces
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException
From source file:Main.java
/** * @return The MAC hardware address of the first network interface of this host. *//*from www .ja v a 2s. co m*/ 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"); }
From source file:com.air.mobilebrowser.NetworkUtil.java
public static String getIPAddress() { boolean useIPv4 = true; try {/*from ww w. jav a2s. c o m*/ List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
From source file:impl.underdark.transport.nsd.BnjUtil.java
public static InetAddress getLocalIpAddress() { try {/*from w ww . ja va 2s .com*/ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (inetAddress.isLoopbackAddress()) continue; if (!InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) continue; //if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress() ) { //if (!inetAddress.isLoopbackAddress() // && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()) ) return inetAddress; } // for } // for } catch (SocketException ex) { Logger.error("nsd server failed to get local address"); } return null; }
From source file:Main.java
public static String getIPAdd() { try {//from w w w . j a v a2 s . co m for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress(); } } } } catch (SocketException ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
public static Set<InetAddress> getLocalAddresses() { Set<InetAddress> addresses = new HashSet<InetAddress>(); Enumeration<NetworkInterface> networkInterfaces; try {// www . j av a 2 s . c o m networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { Log.v(TAG, "getNetworkInterfaces(): " + e.getMessage(), e); return null; } while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration<InetAddress> addressEnum = networkInterface.getInetAddresses(); while (addressEnum.hasMoreElements()) { addresses.add(addressEnum.nextElement()); } } return addresses; }
From source file:org.stem.utils.Utils.java
public static List<InetAddress> getIpAddresses() { try {/*from ww w . j a v a2s . c o m*/ List<InetAddress> addrList = new ArrayList<InetAddress>(); Enumeration<NetworkInterface> enumNI = NetworkInterface.getNetworkInterfaces(); while (enumNI.hasMoreElements()) { NetworkInterface ifc = enumNI.nextElement(); if (ifc.isUp()) { Enumeration<InetAddress> enumAdds = ifc.getInetAddresses(); while (enumAdds.hasMoreElements()) { InetAddress addr = enumAdds.nextElement(); addrList.add(addr); } } } return addrList; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** get first valid ipv4 address of this device */ public static String getLocalIpAddress() { try {/*from w w w.j a va 2 s. com*/ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && validIP(inetAddress.getHostAddress())) { return inetAddress.getHostAddress(); } } } } catch (Exception e) { Log.e(TAG, e.toString()); } return null; }
From source file:codeOrchestra.lcs.license.ActivationReporter.java
private static String getFingerPrint() { StringBuilder resultSB = new StringBuilder(); try {//from w w w . j a v a2 s. c om for (final Enumeration<NetworkInterface> interfaces = NetworkInterface .getNetworkInterfaces(); interfaces.hasMoreElements();) { final NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement(); if (networkInterface.isLoopback()) { continue; } byte[] mac = networkInterface.getHardwareAddress(); if (mac == null) { continue; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } if ("00-00-00-00-00-00-00-E0".equals(sb.toString())) { continue; } resultSB.append(sb); if (interfaces.hasMoreElements()) { resultSB.append("|"); } } } catch (Exception e) { // ignore } String result = resultSB.toString(); if (result.endsWith("|")) { return result.substring(0, result.length() - 1); } return result; }
From source file:com.common.utils.NetworkUtils.java
public static String getLocalHostIp() { String ips = "";//getString(R.string.ipaddr); try {//from www . ja v a2s .c o m Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> inet = nif.getInetAddresses(); while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip.getHostAddress())) { ips = ips + nif.getName() + ":" + ip.getHostAddress() + " "; } } } } catch (SocketException e) { e.printStackTrace(); } return ips; }
From source file:Main.java
public static String getIPAddr() { try {/* w w w.j a v a2 s .c o m*/ Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); return inetAddress.getHostAddress().toString(); } } } catch (SocketException e) { e.printStackTrace(); } return null; }