List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:com.offbynull.portmapper.common.NetworkUtils.java
/** * Set a {@link MulticastChannel} to listen on all IPv6 interfaces. * @param channel multicast channel to listen on * @throws IOException if there's an error * @throws NullPointerException if any argument is {@code null} */// w w w . ja va 2 s . c o m public static void multicastListenOnAllIpv6InterfaceAddresses(MulticastChannel channel) throws IOException { Validate.notNull(channel); final InetAddress ipv6Group = InetAddress.getByName("ff02::1"); // NOPMD Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface InetAddress addr = addrs.nextElement(); try { if (addr instanceof Inet6Address) { channel.join(ipv6Group, networkInterface); } } catch (IOException ioe) { // NOPMD // occurs with certain interfaces, do nothing } } } }
From source file:com.offbynull.portmapper.common.NetworkUtils.java
/** * Set a {@link MulticastChannel} to listen on all IPv4 interfaces. * @param channel multicast channel to listen on * @throws IOException if there's an error * @throws NullPointerException if any argument is {@code null} *//* ww w . j av a 2 s . com*/ public static void multicastListenOnAllIpv4InterfaceAddresses(MulticastChannel channel) throws IOException { Validate.notNull(channel); final InetAddress ipv4Group = InetAddress.getByName("224.0.0.1"); // NOPMD Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface InetAddress addr = addrs.nextElement(); try { if (addr instanceof Inet4Address) { channel.join(ipv4Group, networkInterface); } } catch (IOException ioe) { // NOPMD // occurs with certain interfaces // do nothing } } } }
From source file:com.vmware.identity.idm.CommonUtil.java
private static String findInetAddress(Predicate<InetAddress> match) throws SocketException { String ipAddress = null;// ww w . j av a2 s . c o m Enumeration<NetworkInterface> it = NetworkInterface.getNetworkInterfaces(); while ((ipAddress == null) && it.hasMoreElements()) { NetworkInterface iface = it.nextElement(); if ((!iface.isLoopback()) && (iface.isUp())) { Enumeration<InetAddress> it2 = iface.getInetAddresses(); while (it2.hasMoreElements()) { InetAddress addr = it2.nextElement(); if (match.matches(addr)) { ipAddress = addr.getHostAddress(); break; } } } } return ipAddress; }
From source file:org.droidupnp.Main.java
private static InetAddress getLocalIpAdressFromIntf(String intfName) { try {/*from www . j a va2s. c o m*/ NetworkInterface intf = NetworkInterface.getByName(intfName); if (intf.isUp()) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) return inetAddress; } } } catch (Exception e) { Log.w(TAG, "Unable to get ip adress for interface " + intfName); } return null; }
From source file:eu.codebits.plasmas.util.NetworkInterfaces.java
/** * @param interfaceName eth0, wlan0 or NULL=use first interface * @param useIPv4/*from w w w. j ava2 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:org.jcommon.com.util.jmx.RmiAdptor.java
static private InetAddress getIPv4InetAddress() throws SocketException, UnknownHostException { String os = System.getProperty("os.name").toLowerCase(); if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) { NetworkInterface ni = NetworkInterface.getByName("eth0"); Enumeration<InetAddress> ias = ni.getInetAddresses(); InetAddress iaddress;//w w w.j a v a 2 s. co m do { iaddress = ias.nextElement(); } while (!(iaddress instanceof Inet4Address)); return iaddress; } return InetAddress.getLocalHost(); // for Windows and OS X it should work well }
From source file:hsyndicate.utils.IPUtils.java
public static Collection<String> getIPAddress() { if (!cachedIPAddr.isEmpty()) { return Collections.unmodifiableCollection(cachedIPAddr); } else {/* ww w . java 2 s . com*/ try { Enumeration e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); if (!i.isLoopbackAddress()) { String hostAddress = i.getHostAddress(); cachedIPAddr.add(hostAddress); } } } } catch (SocketException ex) { LOG.error("Exception occurred while scanning local interfaces", ex); } return Collections.unmodifiableCollection(cachedIPAddr); } }
From source file:org.springframework.cloud.etcd.discovery.EtcdDiscoveryProperties.java
public static InetAddress getIpAddress() { try {/*w w w. ja v a2 s . c o m*/ for (Enumeration<NetworkInterface> enumNic = NetworkInterface.getNetworkInterfaces(); enumNic .hasMoreElements();) { NetworkInterface ifc = enumNic.nextElement(); if (ifc.isUp()) { for (Enumeration<InetAddress> enumAddr = ifc.getInetAddresses(); enumAddr.hasMoreElements();) { InetAddress address = enumAddr.nextElement(); if (address instanceof Inet4Address && !address.isLoopbackAddress()) { return address; } } } } return InetAddress.getLocalHost(); } catch (UnknownHostException e) { ReflectionUtils.rethrowRuntimeException(e); return null; } catch (IOException e) { log.warn("Unable to find non-loopback address", e); return null; } }
From source file:com.beetle.framework.util.OtherUtil.java
/** * ?ip??char=2/*from w ww. j a va2 s . co m*/ * * @return */ public static String getLocalHostIps() { StringBuffer sb = new StringBuffer(); final char flag = 2; try { Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { InetAddress inetAddress = ips.nextElement(); String ip = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && ip.indexOf(":") == -1) { sb.append(ip).append(flag); } } } } catch (Exception e) { return ""; } return sb.toString(); }
From source file:hsyndicate.utils.IPUtils.java
public static Collection<String> getHostAddress() { if (!cachedHostAddr.isEmpty()) { return Collections.unmodifiableCollection(cachedHostAddr); } else {// ww w .java 2 s . c om try { Enumeration e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); String hostAddress = i.getHostAddress(); if (isProperHostAddress(hostAddress)) { if (!cachedHostAddr.contains(hostAddress)) { cachedHostAddr.add(hostAddress); } } String hostName = i.getHostName(); if (isProperHostAddress(hostName)) { if (!cachedHostAddr.contains(hostName)) { cachedHostAddr.add(hostName); } } String canonicalHostName = i.getCanonicalHostName(); if (isProperHostAddress(canonicalHostName)) { if (!cachedHostAddr.contains(canonicalHostName)) { cachedHostAddr.add(canonicalHostName); } } } } } catch (SocketException ex) { LOG.error("Exception occurred while scanning local interfaces", ex); } return Collections.unmodifiableCollection(cachedHostAddr); } }