List of usage examples for java.net NetworkInterface getNetworkInterfaces
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException
From source file:org.nuxeo.ecm.core.management.statuses.NuxeoInstanceIdentifierHelper.java
public static String generateHardwareUID() throws Exception { String hwUID = ""; String javaVersion = System.getProperty("java.version"); Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); while (ifs.hasMoreElements()) { NetworkInterface ni = ifs.nextElement(); if (javaVersion.contains("1.6")) { // ni.getHardwareAddress() only in jdk 1.6 Method[] methods = ni.getClass().getMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase("getHardwareAddress")) { byte[] hwAddr = (byte[]) method.invoke(ni); if (hwAddr != null) { hwUID = hwUID + "-" + Base64.encodeBytes(hwAddr); }/*from w ww . ja v a2 s .com*/ break; } } } else { Enumeration<InetAddress> addrs = ni.getInetAddresses(); while (addrs.hasMoreElements()) { hwUID = hwUID + "-" + Base64.encodeBytes(addrs.nextElement().getAddress()); } } } return hwUID; }
From source file:com.seadee.degree.service.NetworkStateReceiver.java
public static String getLocalIpAddress(Context context) { final String IPTAG = "getLocalIpAddress"; String nullAddress = "0.0.0.0"; try {/*from w ww.j a v a 2 s . com*/ if (SettingVarible.networkstate != SettingVarible.NETWORKSTATE.NONETWORK) { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); String ip_address = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address)) return ip_address; } } } } catch (SocketException ex) { Log.e(IPTAG, ex.toString()); } return nullAddress; }
From source file:LocalAddress.java
/** * Return an address of a non-loopback interface on the local host * /*from ww w.j a v a 2 s . c o m*/ * @return address * the InetAddress of the local host */ public static InetAddress getLocalAddress() { InetAddress addr = null; try { addr = InetAddress.getLocalHost(); // OK - is this the loopback addr ? if (!addr.isLoopbackAddress()) { return addr; } // plan B - enumerate the network interfaces Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface netIf = (NetworkInterface) ifaces.nextElement(); Enumeration addrs = netIf.getInetAddresses(); while (addrs.hasMoreElements()) { addr = (InetAddress) addrs.nextElement(); //System.out.println( "enum: " + addr.getHostAddress() ); if (addr instanceof Inet6Address) { // probably not what we want - keep looking continue; } // chose (arbitrarily?) first non-loopback addr if (!addr.isLoopbackAddress()) { return addr; } } } // nothing so far - last resort return getReflectedAddress(); } catch (UnknownHostException uhE) { // deal with this } catch (SocketException sockE) { // can deal? } return null; }
From source file:Main.java
public static String getLocalIpAddress() { try {//from ww w .ja v a 2s . c o m String ipv4; List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni : nilist) { List<InetAddress> ialist = Collections.list(ni.getInetAddresses()); for (InetAddress address : ialist) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) { return ipv4; } } } } catch (SocketException ex) { } return null; }
From source file:NetworkUtil.java
/** * @return the current environment's IP address, taking into account the Internet connection to any of the available * machine's Network interfaces. Examples of the outputs can be in octats or in IPV6 format. * <pre>//from w ww .ja va2s .c om * ==> wlan0 * * fec0:0:0:9:213:e8ff:fef1:b717%4 * siteLocal: true * isLoopback: false isIPV6: true * 130.212.150.216 <<<<<<<<<<<------------- This is the one we want to grab so that we can. * siteLocal: false address the DSP on the network. * isLoopback: false * isIPV6: false * * ==> lo * 0:0:0:0:0:0:0:1%1 * siteLocal: false * isLoopback: true * isIPV6: true * 127.0.0.1 * siteLocal: false * isLoopback: true * isIPV6: false * </pre> */ public static String getCurrentEnvironmentNetworkIp() { if (currentHostIpAddress == null) { Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { InetAddress addr = address.nextElement(); // log.debug("Inetaddress:" + addr.getHostAddress() + " loop? " + addr.isLoopbackAddress() + " local? " // + addr.isSiteLocalAddress()); if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress() && !(addr.getHostAddress().indexOf(":") > -1)) { currentHostIpAddress = addr.getHostAddress(); } } } if (currentHostIpAddress == null) { currentHostIpAddress = "127.0.0.1"; } } catch (SocketException e) { // log.error("Somehow we have a socket error acquiring the host IP... Using loopback instead..."); currentHostIpAddress = "127.0.0.1"; } } return currentHostIpAddress; }
From source file:com.alibaba.napoli.metamorphosis.network.RemotingUtils.java
public static String getLocalAddress() throws Exception { // ????ip?/*from w w w . ja v a 2 s . c o m*/ final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); InetAddress ipv6Address = null; while (enumeration.hasMoreElements()) { final NetworkInterface networkInterface = enumeration.nextElement(); final Enumeration<InetAddress> en = networkInterface.getInetAddresses(); while (en.hasMoreElements()) { final InetAddress address = en.nextElement(); if (!address.isLoopbackAddress()) { if (address instanceof Inet6Address) { ipv6Address = address; } else { // ipv4 return normalizeHostAddress(address); } } } } // ipv4?ipv6 if (ipv6Address != null) { return normalizeHostAddress(ipv6Address); } final InetAddress localHost = InetAddress.getLocalHost(); return normalizeHostAddress(localHost); }
From source file:nfinity.FindPeer.java
public String ScanNetwork() { try {/*from w w w. j av a2 s . c o m*/ Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) { Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { if (!inetAddress.getClass().toString().equals("class java.net.Inet6Address")) { if (!inetAddress.isLoopbackAddress()) { try { SubnetUtils utils = new SubnetUtils(inetAddress.getHostAddress() + "/" + netint.getInterfaceAddresses().get(1).getNetworkPrefixLength()); String[] allIps = utils.getInfo().getAllAddresses(); return ConnectIP(allIps, inetAddress); } catch (IllegalArgumentException ex) { int prefix = NetworkInterface.getByInetAddress(inetAddress).getInterfaceAddresses() .get(0).getNetworkPrefixLength(); if (!inetAddress.isLoopbackAddress()) { System.out .println("IP: " + inetAddress.getHostAddress() + " Prefix: " + prefix); SubnetUtils utils = new SubnetUtils( inetAddress.getHostAddress() + "/" + prefix); String[] allIps = utils.getInfo().getAllAddresses(); return ConnectIP(allIps, inetAddress); } } } } } } } catch (SocketException ex) { System.out.println("Connection failed " + ex.getMessage()); } return "Peer not found"; }
From source file:eu.codebits.plasmas.util.NetworkInterfaces.java
/** * @param interfaceName eth0, wlan0 or NULL=use first interface * @param useIPv4// w w w . ja v a2 s. c o m * @return address or empty string */ @SuppressLint("DefaultLocale") public static String getIPAddress(String interfaceName, boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } 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 (SocketException ex) { } return ""; }
From source file:com.splout.db.common.GetIPAddresses.java
/** * Returns all available IP addresses./* www . j ava 2s . c om*/ * <p/> * In error case or if no network connection is established, we return an empty list here. * <p/> * Loopback addresses are excluded - so 127.0.0.1 will not be never returned. * <p/> * The "primary" IP might not be the first one in the returned list. * * @return Returns all IP addresses (can be an empty list in error case or if network connection is missing). * @throws SocketException * @since 0.1.0 */ public static Collection<InetAddress> getAllLocalIPs() throws SocketException { LinkedList<InetAddress> listAdr = new LinkedList<InetAddress>(); Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); if (nifs == null) return listAdr; while (nifs.hasMoreElements()) { NetworkInterface nif = nifs.nextElement(); // We ignore subinterfaces - as not yet needed. Enumeration<InetAddress> adrs = nif.getInetAddresses(); while (adrs.hasMoreElements()) { InetAddress adr = adrs.nextElement(); if (adr != null && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress())) { log.info("Available site local address: " + adr); listAdr.add(adr); } } } return listAdr; }
From source file:cn.leancloud.diamond.server.utils.SystemConfig.java
private static String getHostAddress() { String address = "127.0.0.1"; try {/*from w ww. j a v a 2 s. c o m*/ Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface ni = en.nextElement(); Enumeration<InetAddress> ads = ni.getInetAddresses(); while (ads.hasMoreElements()) { InetAddress ip = ads.nextElement(); if (!ip.isLoopbackAddress() && ip.isSiteLocalAddress()) { return ip.getHostAddress(); } } } } catch (Exception e) { } return address; }