List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:Main.java
public static String getIPAddr() { try {/*from w w w . jav a2 s. co 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; }
From source file:org.wso2.carbon.analytics.message.tracer.handler.util.PublisherUtil.java
private static InetAddress getLocalAddress() { Enumeration<NetworkInterface> ifaces = null; try {//ww w .ja v a 2s. co m ifaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Failed to get host address", e); } if (ifaces != null) { while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { return addr; } } } } 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 w w . j av a 2 s.c o m * ==> 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:Main.java
private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { System.out.printf("Display name: %s%n", netint.getDisplayName()); System.out.printf("Name: %s%n", netint.getName()); Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { System.out.printf("InetAddress: %s%n", inetAddress); }/*www .j a v a2s . c o m*/ System.out.printf("%n"); }
From source file:com.seadee.library.receiver.NetworkStateReceiver.java
public static String getLocalIpAddress(Context context) { final String IPTAG = "getLocalIpAddress"; String nullAddress = "0.0.0.0"; try {/* w w w. j av a 2s .c o 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(); 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:org.namelessrom.devicecontrol.net.NetworkInfo.java
public static String getIpAddress(final boolean useIPv4) { List<NetworkInterface> interfaces; try {//from www.j a v a 2 s . c o m interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); } catch (Exception e) { interfaces = null; } if (interfaces == null) return "0.0.0.0"; for (NetworkInterface intf : interfaces) { final List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (final InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { final String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { return sAddr; } } else { if (!isIPv4) { final int delim = sAddr.indexOf('%'); // drop ip6 port suffix return ((delim < 0) ? sAddr : sAddr.substring(0, delim)); } } } } } return "0.0.0.0"; }
From source file:com.alibaba.napoli.metamorphosis.network.RemotingUtils.java
public static String getLocalAddress() throws Exception { // ????ip?/*from ww w. ja va2 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:edu.stanford.junction.provider.jx.JunctionProvider.java
public static String getLocalIpAddress() { try {/*from w w w.j a v a2s .c o 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()) { // not ready for IPv6, apparently. if (!inetAddress.getHostAddress().contains(":")) { return inetAddress.getHostAddress().toString(); } } } } } catch (SocketException ex) { Log.e("junction", ex.toString()); } return null; }
From source file:proxy.ElementalHttpGet.java
static List<InetAddress> getAllIp(String networkInterface) { try {// w w w . j a v a 2s. c om NetworkInterface interfaces = NetworkInterface.getByName(networkInterface); Enumeration<InetAddress> inetAddresses = interfaces.getInetAddresses(); List<InetAddress> result = new ArrayList<InetAddress>(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (inetAddress instanceof Inet4Address) { result.add(inetAddress); } } return result; } catch (SocketException e) { throw new RuntimeException(e); } }
From source file:com.ephesoft.dcma.util.NetworkUtil.java
/** * Utility method which will fetch the ip address of the system. * //from w w w . j a v a2s. c o m * @return ip address of the system. */ public static String getSystemIPAddress() { StringBuilder input = new StringBuilder(UtilConstants.INPUT_CONST); boolean first = true; String returnAddress = null; try { for (Enumeration<NetworkInterface> enumer = NetworkInterface.getNetworkInterfaces(); enumer .hasMoreElements();) { NetworkInterface netInterface = enumer.nextElement(); Enumeration<InetAddress> inetEnum = netInterface.getInetAddresses(); while (inetEnum.hasMoreElements()) { InetAddress inetAddress = inetEnum.nextElement(); if (!inetAddress.isLoopbackAddress()) { if (first) { first = false; } else { input.append(UtilConstants.FORWARD_SLASH); } input.append(inetAddress.getHostAddress()); } } } } catch (Exception e) { LOG.error(e.getMessage(), e); returnAddress = IPADDRESS; } if (null == returnAddress) { returnAddress = first ? IPADDRESS : input.toString(); } return returnAddress; }