List of usage examples for java.net NetworkInterface getNetworkInterfaces
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException
From source file:com.aurel.track.prop.ApplicationBean.java
/** * Retrieve all non-loopback IP addresses from all network interfaces * * @return the array of assigned IP numbers *///w ww. j a va2 s .co m public static InetAddress[] getInetAddress() { List<InetAddress> allIPs = new ArrayList<InetAddress>(); InetAddress[] allAds = null; try { InetAddress candidateAddress = null; // Iterate all NICs (network interface cards)... for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces .hasMoreElements();) { NetworkInterface iface = (NetworkInterface) ifaces.nextElement(); // Iterate all IP addresses assigned to each card... for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) { InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); if (!inetAddr.isLoopbackAddress()) { if (inetAddr.isSiteLocalAddress()) { // Found non-loopback site-local address. Return it // immediately... allIPs.add(inetAddr); continue; } else if (candidateAddress == null) { // Found non-loopback address, but not necessarily // site-local. // Store it as a candidate to be returned if // site-local address is not subsequently found... candidateAddress = inetAddr; // Note that we don't repeatedly assign non-loopback // non-site-local addresses as candidates, // only the first. For subsequent iterations, // candidate will be non-null. } } } } // At this point, we did not find a non-loopback address. // Fall back to returning whatever InetAddress.getLocalHost() // returns... if (allIPs.isEmpty()) { allIPs.add(InetAddress.getLocalHost()); } allAds = new InetAddress[allIPs.size()]; allAds = allIPs.toArray(allAds); } catch (Exception uhn) { LOGGER.error( "An exception occurred trying to get " + "all IP addresses for this host: " + uhn.getMessage()); } return allAds; }
From source file:org.smilec.smile.student.CourseList.java
private String get_IP() { try {//from w ww .j ava 2 s . c o m Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface ni = e.nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { String ipa = ips.nextElement().toString(); if (ipa.startsWith("/")) ipa = ipa.substring(1); if (ipa.indexOf(':') >= 0) { // IPv6. Ignore continue; } if (ipa.equals("127.0.0.1")) { continue; // loopback MY_IP. Not meaningful for out // purpose } Log.d(APP_TAG, ipa); return ipa; } } } catch (SocketException e) { e.printStackTrace(); } return null; }
From source file:com.jagornet.dhcpv6.server.DhcpV6Server.java
private List<InetAddress> getAllIPv6Addrs() { List<InetAddress> ipAddrs = new ArrayList<InetAddress>(); try {/*from w ww .ja va 2 s . co m*/ Enumeration<NetworkInterface> localInterfaces = NetworkInterface.getNetworkInterfaces(); if (localInterfaces != null) { while (localInterfaces.hasMoreElements()) { NetworkInterface netIf = localInterfaces.nextElement(); Enumeration<InetAddress> ifAddrs = netIf.getInetAddresses(); while (ifAddrs.hasMoreElements()) { InetAddress ip = ifAddrs.nextElement(); if (ip instanceof Inet6Address) { ipAddrs.add(ip); } } } } else { log.error("No network interfaces found!"); } } catch (IOException ex) { log.error("Failed to get IPv6 addresses: " + ex); } return ipAddrs; }
From source file:org.apache.axis2.transport.nhttp.ServerWorker.java
/** * Copied from transport.http of Axis2//from www. j a v a2s . com * * Returns the ip address to be used for the replyto epr * CAUTION: * This will go through all the available network interfaces and will try to return an ip address. * First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found * then this will return this will return 127.0.0.1. * This will <b>not<b> consider IPv6 addresses. * <p/> * TODO: * - Improve this logic to genaralize it a bit more * - Obtain the ip to be used here from the Call API * * @return Returns String. * @throws java.net.SocketException */ private static String getIpAddress() throws SocketException { Enumeration e = NetworkInterface.getNetworkInterfaces(); String address = "127.0.0.1"; while (e.hasMoreElements()) { NetworkInterface netface = (NetworkInterface) e.nextElement(); Enumeration addresses = netface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress ip = (InetAddress) addresses.nextElement(); if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) { return ip.getHostAddress(); } } } return address; }
From source file:org.structr.util.StructrLicenseManager.java
private Iterable<NetworkInterface> getNetworkInterfaces() { final List<NetworkInterface> interfaces = new LinkedList<>(); try {/*from w ww .j a v a 2 s . c o m*/ for (final Enumeration<NetworkInterface> enumeration = NetworkInterface .getNetworkInterfaces(); enumeration.hasMoreElements();) { interfaces.add(enumeration.nextElement()); } } catch (SocketException ex) { } return interfaces; }
From source file:org.archive.modules.fetcher.FetchHTTPTests.java
public void testHttpBindAddress() throws Exception { List<InetAddress> addrList = new ArrayList<InetAddress>(); for (NetworkInterface ifc : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (ifc.isUp()) { for (InetAddress addr : Collections.list(ifc.getInetAddresses())) { if (addr instanceof Inet4Address) { addrList.add(addr);//from w w w . j av a 2 s . co m } } } } if (addrList.size() < 2) { fail("unable to test binding to different local addresses: only " + addrList.size() + " addresses available"); } for (InetAddress addr : addrList) { tryHttpBindAddress(addr.getHostAddress()); } }
From source file:com.max2idea.android.fwknop.Fwknop.java
public static String getLocalIpAddress() { try {//from w w w. j av a 2 s . 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()) { Log.v("Internal ip", inetAddress.getHostAddress().toString()); return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("Internal IP", ex.toString()); } return null; }
From source file:com.sentaroh.android.SMBSync2.CommonUtilities.java
public static String getLocalIpAddress() { String result = ""; boolean exit = false; try {/* w w w . jav a2s. 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.toString().indexOf(":")>=0)) { // return inetAddress.getHostAddress().toString(); // } // Log.v("","ip="+inetAddress.getHostAddress()+ // ", name="+intf.getName()); if (inetAddress.isSiteLocalAddress()) { result = inetAddress.getHostAddress(); // Log.v("","result="+result+", name="+intf.getName()+"-"); if (intf.getName().equals("wlan0")) { exit = true; break; } } } if (exit) break; } } catch (SocketException ex) { Log.e(APPLICATION_TAG, ex.toString()); result = "192.168.0.1"; } // Log.v("","getLocalIpAddress result="+result); if (result.equals("")) result = "192.168.0.1"; return result; }
From source file:org.apache.nifi.registry.jetty.JettyServer.java
private void dumpUrls() throws SocketException { final List<String> urls = new ArrayList<>(); for (Connector connector : server.getConnectors()) { if (connector instanceof ServerConnector) { final ServerConnector serverConnector = (ServerConnector) connector; Set<String> hosts = new HashSet<>(); // determine the hosts if (StringUtils.isNotBlank(serverConnector.getHost())) { hosts.add(serverConnector.getHost()); } else { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); if (networkInterfaces != null) { for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) { for (InetAddress inetAddress : Collections.list(networkInterface.getInetAddresses())) { hosts.add(inetAddress.getHostAddress()); }// w w w . j a v a2s . co m } } } // ensure some hosts were found if (!hosts.isEmpty()) { String scheme = "http"; if (properties.getSslPort() != null && serverConnector.getPort() == properties.getSslPort()) { scheme = "https"; } // dump each url for (String host : hosts) { urls.add(String.format("%s://%s:%s", scheme, host, serverConnector.getPort())); } } } } if (urls.isEmpty()) { logger.warn( "NiFi Registry has started, but the UI is not available on any hosts. Please verify the host properties."); } else { // log the ui location logger.info("NiFi Registry has started. The UI is available at the following URLs:"); for (final String url : urls) { logger.info(String.format("%s/nifi-registry", url)); } } }
From source file:fr.inria.ucn.collectors.NetworkStateCollector.java
private JSONArray getIfconfig(Map<String, JSONObject> stats) throws JSONException { JSONArray ifaces = new JSONArray(); // make sure the stats is read networkStats();// w w w . j a v a 2 s .c om Enumeration<NetworkInterface> en = null; try { en = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { Log.d(Constants.LOGTAG, "failed to list interfaces", e); } if (en != null) { while (en.hasMoreElements()) { NetworkInterface intf = en.nextElement(); JSONObject iface = new JSONObject(); iface.put("display_name", intf.getDisplayName()); iface.put("name", intf.getName()); iface.put("is_virtual", intf.isVirtual()); iface.put("stats", stats.get(intf.getName())); try { iface.put("mtu", intf.getMTU()); iface.put("is_loopback", intf.isLoopback()); iface.put("is_ptop", intf.isPointToPoint()); iface.put("is_up", intf.isUp()); } catch (SocketException e) { Log.d(Constants.LOGTAG, "failed to read interface data", e); } JSONArray ips = new JSONArray(); List<InterfaceAddress> ilist = intf.getInterfaceAddresses(); for (InterfaceAddress ia : ilist) { ips.put(ia.getAddress().getHostAddress()); } iface.put("addresses", ips); ifaces.put(iface); } } else { for (String name : stats.keySet()) { JSONObject iface = new JSONObject(); iface.put("name", name); iface.put("stats", stats.get(name)); ifaces.put(iface); } } return ifaces; }