List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:org.wso2.javaagent.JDBCAgentPublisher.java
public static InetAddress getLocalAddress() throws SocketException, UnknownHostException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { return addr; }/* www . jav a 2 s. co m*/ } } return InetAddress.getLocalHost(); }
From source file:org.apache.htrace.core.TracerId.java
/** * <p>Get the best IP address that represents this node.</p> * * This is complicated since nodes can have multiple network interfaces, * and each network interface can have multiple IP addresses. What we're * looking for here is an IP address that will serve to identify this node * to HTrace. So we prefer site-local addresess (i.e. private ones on the * LAN) to publicly routable interfaces. If there are multiple addresses * to choose from, we select the one which comes first in textual sort * order. This should ensure that we at least consistently call each node * by a single name.//from www. j a v a 2 s .com */ static String getBestIpString() { Enumeration<NetworkInterface> ifaces; try { ifaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Error getting network interfaces", e); return "127.0.0.1"; } TreeSet<String> siteLocalCandidates = new TreeSet<String>(); TreeSet<String> candidates = new TreeSet<String>(); while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); for (Enumeration<InetAddress> addrs = iface.getInetAddresses(); addrs.hasMoreElements();) { InetAddress addr = addrs.nextElement(); if (!addr.isLoopbackAddress()) { if (addr.isSiteLocalAddress()) { siteLocalCandidates.add(addr.getHostAddress()); } else { candidates.add(addr.getHostAddress()); } } } } if (!siteLocalCandidates.isEmpty()) { return siteLocalCandidates.first(); } if (!candidates.isEmpty()) { return candidates.first(); } return "127.0.0.1"; }
From source file:org.jkcsoft.java.util.JavaHelper.java
public static InetAddress getUsefulInetAddr() throws UnknownHostException { InetAddress returnInetAddr = InetAddress.getLocalHost(); int usefulCount = 0; try {//from w w w . j a v a2 s . c om Enumeration niEnum = NetworkInterface.getNetworkInterfaces(); while (niEnum.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) niEnum.nextElement(); Enumeration ieEnum = ni.getInetAddresses(); while (ieEnum.hasMoreElements()) { InetAddress inetAddr = InetAddress.getLocalHost(); inetAddr = (InetAddress) ieEnum.nextElement(); log.debug("NIC [" + ni.getDisplayName() + "]" + " Addr hn=[" + inetAddr.getHostName() + "]" + " chn=[" + inetAddr.getCanonicalHostName() + "]" + " ha=[" + inetAddr.getHostAddress() + "]"); // hack to skip addresses often provided by Linux... if (!"127.0.0.1".equals(inetAddr.getHostAddress()) && inetAddr.getHostAddress().indexOf(':') == -1) { if (usefulCount == 0) returnInetAddr = inetAddr; usefulCount++; } else { // } } } } catch (SocketException e) { log.error("getHostName", e); } if (usefulCount == 0) { log.warn("Only the loopback InetAddress could be found"); } if (usefulCount > 1) { log.warn("More than one non-loopback InetAddrss was found; using the first one found."); } log.debug("Returning inet addr [" + returnInetAddr.toString() + "]"); return returnInetAddr; }
From source file:com.buaa.cfs.net.DNS.java
/** * @param nif network interface to get addresses for * * @return set containing addresses for each subinterface of nif, see below for the rationale for using an ordered * set/* ww w. ja va 2 s . c om*/ */ private static LinkedHashSet<InetAddress> getSubinterfaceInetAddrs(NetworkInterface nif) { LinkedHashSet<InetAddress> addrs = new LinkedHashSet<InetAddress>(); Enumeration<NetworkInterface> subNifs = nif.getSubInterfaces(); while (subNifs.hasMoreElements()) { NetworkInterface subNif = subNifs.nextElement(); addrs.addAll(Collections.list(subNif.getInetAddresses())); } return addrs; }
From source file:net.sbbi.upnp.Discovery.java
private static UPNPRootDevice[] discoverDevices(int timeOut, int ttl, int mx, String searchTarget, NetworkInterface ni) throws IOException { if (searchTarget == null || searchTarget.trim().length() == 0) { throw new IllegalArgumentException("Illegal searchTarget"); }//from w ww. j a v a 2 s . c om final Map devices = new HashMap(); DiscoveryResultsHandler handler = new DiscoveryResultsHandler() { public void discoveredDevice(String usn, String udn, String nt, String maxAge, URL location, String firmware) { synchronized (devices) { if (!devices.containsKey(usn)) { try { UPNPRootDevice device = new UPNPRootDevice(location, maxAge, firmware, usn, udn); devices.put(usn, device); } catch (Exception ex) { log.error("Error occured during upnp root device object creation from location " + location, ex); } } } } }; DiscoveryListener.getInstance().registerResultsHandler(handler, searchTarget); if (ni == null) { for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) { NetworkInterface intf = (NetworkInterface) e.nextElement(); for (Enumeration adrs = intf.getInetAddresses(); adrs.hasMoreElements();) { InetAddress adr = (InetAddress) adrs.nextElement(); if (adr instanceof Inet4Address && !adr.isLoopbackAddress()) { sendSearchMessage(adr, ttl, mx, searchTarget); } } } } else { for (Enumeration adrs = ni.getInetAddresses(); adrs.hasMoreElements();) { InetAddress adr = (InetAddress) adrs.nextElement(); if (adr instanceof Inet4Address && !adr.isLoopbackAddress()) { sendSearchMessage(adr, ttl, mx, searchTarget); } } } try { Thread.sleep(timeOut); } catch (InterruptedException ex) { // don't care } DiscoveryListener.getInstance().unRegisterResultsHandler(handler, searchTarget); if (devices.size() == 0) { return null; } int j = 0; UPNPRootDevice[] rootDevices = new UPNPRootDevice[devices.size()]; for (Iterator i = devices.values().iterator(); i.hasNext();) { rootDevices[j++] = (UPNPRootDevice) i.next(); } return rootDevices; }
From source file:com.github.ipaas.ifw.util.IPUtil.java
/** * ??// w w w . j av a 2 s. c om * * @return */ public static List<InetAddress> getLocalAddresses() { if (localAddressList == null) { localAddressList = new ArrayList<InetAddress>(); try { // ??? Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces != null && interfaces.hasMoreElements()) { NetworkInterface interfaceN = interfaces.nextElement(); // ??? Enumeration<InetAddress> ienum = interfaceN.getInetAddresses(); while (ienum.hasMoreElements()) { InetAddress ia = ienum.nextElement(); // ?? localAddressList.add(ia); } } } catch (Exception e) { e.printStackTrace(); } logger.info("???[{}]", localAddressList); } return localAddressList; }
From source file:org.jumpmind.util.AppUtils.java
public static String getIpAddress() { String ipAddress = System.getProperty(SYSPROP_IP_ADDRESS, UNKNOWN); if (UNKNOWN.equals(ipAddress)) { try {/*w ww . j a v a 2 s . co m*/ Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.isLoopbackAddress()) { ipAddress = inetAddress.getHostAddress(); } } } } catch (Exception ex) { log.warn("", ex); } finally { } } if (UNKNOWN.equals(ipAddress)) { try { ipAddress = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException ex) { log.warn("", ex); ipAddress = "127.0.0.1"; } } return ipAddress; }
From source file:org.wso2.carbon.integration.test.ha.HATestCase.java
public static String findAddress(String hostname) throws SocketException { if (hostname.trim().equals("localhost") || hostname.trim().equals("127.0.0.1") || hostname.trim().equals("::1")) { Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); 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.getHostAddress(); }//from w ww . j av a 2 s. c o m } } return "127.0.0.1"; } else { return hostname; } }
From source file:ca.frozen.curlingtv.classes.Utils.java
public static String getLocalIpAddress() { try {//from w ww . j a v a 2 s . c om 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 String getIpAddress(boolean alwaysGetWifi) { try {//from w w w . ja v a 2 s. c o m Enumeration<NetworkInterface> enmNetI = NetworkInterface.getNetworkInterfaces(); while (enmNetI.hasMoreElements()) { NetworkInterface networkInterface = enmNetI.nextElement(); boolean b = alwaysGetWifi ? networkInterface.getDisplayName().equals("wlan0") || networkInterface.getDisplayName().equals("eth0") : true; if (b) { Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); while (inetAddressEnumeration.hasMoreElements()) { InetAddress inetAddress = inetAddressEnumeration.nextElement(); if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) { return "net name is " + networkInterface.getDisplayName() + ",ip is " + inetAddress.getHostAddress(); } } } } } catch (SocketException e) { e.printStackTrace(); } return ""; }