List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:org.apache.flink.runtime.taskmanager.TaskManager.java
/** * Determines the IP address of the interface from which the TaskManager can connect to the given JobManager * IP address.//from w w w . ja v a2 s. c o m * * @param jobManagerAddress The socket address to connect to. * @return The IP address of the interface that connects to the JobManager. * @throws IOException If no connection could be established. */ private static InetAddress getTaskManagerAddress(InetSocketAddress jobManagerAddress) throws IOException { AddressDetectionState strategy = AddressDetectionState.ADDRESS; while (true) { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = e.nextElement(); Enumeration<InetAddress> ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = ee.nextElement(); switch (strategy) { case ADDRESS: if (hasCommonPrefix(jobManagerAddress.getAddress().getAddress(), i.getAddress())) { if (tryToConnect(i, jobManagerAddress, strategy.getTimeout())) { LOG.info("Determined " + i + " as the TaskTracker's own IP address"); return i; } } break; case FAST_CONNECT: case SLOW_CONNECT: boolean correct = tryToConnect(i, jobManagerAddress, strategy.getTimeout()); if (correct) { LOG.info("Determined " + i + " as the TaskTracker's own IP address"); return i; } break; default: throw new RuntimeException("Unkown address detection strategy: " + strategy); } } } // state control switch (strategy) { case ADDRESS: strategy = AddressDetectionState.FAST_CONNECT; break; case FAST_CONNECT: strategy = AddressDetectionState.SLOW_CONNECT; break; case SLOW_CONNECT: throw new RuntimeException("The TaskManager is unable to connect to the JobManager (Address: '" + jobManagerAddress + "')."); } if (LOG.isDebugEnabled()) { LOG.debug("Defaulting to detection strategy {}", strategy); } } }
From source file:org.apereo.portal.PortalInfoProviderImpl.java
protected String getDefaultNetworkInterfaceName() { this.logger//w ww . j av a 2 s . co m .info("Attempting to resolve serverName by iterating over NetworkInterface.getNetworkInterfaces()"); //Fail back to our best attempt at resolution final Enumeration<NetworkInterface> networkInterfaceEnum; try { networkInterfaceEnum = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { logger.warn("Failed to get list of available NetworkInterfaces.", e); return null; } //Use a local variable here to try and return the first hostName found that doesn't start with localhost String name = null; while (networkInterfaceEnum.hasMoreElements()) { final NetworkInterface networkInterface = networkInterfaceEnum.nextElement(); for (Enumeration<InetAddress> inetAddressEnum = networkInterface.getInetAddresses(); inetAddressEnum .hasMoreElements();) { final InetAddress inetAddress = inetAddressEnum.nextElement(); name = inetAddress.getHostName(); if (!name.startsWith("localhost")) { return name; } } } return name; }
From source file:com.reversemind.hypergate.server.HyperGateServer.java
private String getIpAddress() { Set<String> ipSet = new TreeSet<String>(); Enumeration<NetworkInterface> n = null; try {/*from w ww . jav a2s .c o m*/ n = NetworkInterface.getNetworkInterfaces(); for (; n.hasMoreElements();) { NetworkInterface e = n.nextElement(); Enumeration<InetAddress> a = e.getInetAddresses(); for (; a.hasMoreElements();) { InetAddress inetAddress = a.nextElement(); if (inetAddress.getHostAddress() != null) { ipSet.add(inetAddress.getHostAddress()); } } } if (ipSet != null && ipSet.size() > 0) { for (String ip : ipSet) { if (!ip.equals("127.0.0.1") & !ip.equalsIgnoreCase("localhost")) { return ip; } } } } catch (SocketException se) { LOG.error("Socket exception", se); } return "localhost"; }
From source file:org.inaetics.pubsub.demo.config.Configurator.java
private String getIp() { try {/* w w w .j ava 2 s .c o m*/ Enumeration<NetworkInterface> nEnumeration = NetworkInterface.getNetworkInterfaces(); while (nEnumeration.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) nEnumeration.nextElement(); if (networkInterface.getName().equals("eth1")) { Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress inetAddress = (InetAddress) addresses.nextElement(); if (inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress(); } } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "localhost"; }
From source file:com.scottieknows.test.cassandra.CassandraClusterManager.java
/** * @return {@link Map} of interface name to address *///from w ww . j a v a2 s . co m Map<String, String> getIpsStartingWith(String prefix) { if (prefix == null) { return Collections.emptyMap(); } try { Map<String, String> rtn = new HashMap<>(); Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface i = networkInterfaces.nextElement(); Enumeration<InetAddress> addrs = i.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); String hostAddress = addr.getHostAddress(); if (hostAddress.startsWith(prefix)) { rtn.put(i.getName(), hostAddress); } } } return rtn; } catch (SocketException e) { throw new RuntimeException("could not retrieve network interfaces: " + e, e); } }
From source file:com.xperia64.cosi.CosiActivity.java
public String getLocalIpAddress() { try {/*w w w. j a va 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(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress(); } } } } catch (SocketException ex) { ex.printStackTrace(); } return null; }
From source file:com.mgmtp.perfload.perfmon.PerfMon.java
void writeHeader() { String hostName = PerfMonUtils.getHostName(); StringBuilder metaBuffer = new StringBuilder(200); metaBuffer.append(DATE_FORMAT.format(System.currentTimeMillis())); metaBuffer.append(SEPARATOR);/*w w w. j av a2 s .co m*/ metaBuffer.append("meta"); metaBuffer.append(SEPARATOR); metaBuffer.append("perfMon ").append(PerfMonUtils.getPerfMonVersion()); metaBuffer.append(SEPARATOR); metaBuffer.append(hostName); LOG.info("Hostname: {}", hostName); try { LOG.info("CPUs: {}", sigar.getCpuList().length); LOG.info("Total RAM: {} MB", sigar.getMem().getRam()); LOG.info("Uptime: {}", Uptime.getInfo(sigar)); } catch (SigarException ex) { LOG.error("Error retrieving system information from Sigar.", ex); } try { InetAddress[] networkIPAddresses = PerfMonUtils.getNetworkIPAddresses(); if (networkIPAddresses.length > 0) { LOG.info("Local IP addresses:"); metaBuffer.append(SEPARATOR); for (int i = 0; i < networkIPAddresses.length; ++i) { InetAddress ip = networkIPAddresses[i]; if (i > 0) { metaBuffer.append(','); } metaBuffer.append(ip); LOG.info(ip.toString()); } } } catch (UnknownHostException ex) { LOG.error("Error retrieving IP addresses of the localhost.", ex); } try { LOG.info("Full list of network interfaces:"); List<NetworkInterface> networkInterfaces = PerfMonUtils.getNetworkInterfaces(); for (NetworkInterface ni : networkInterfaces) { for (Enumeration<InetAddress> en = ni.getInetAddresses(); en.hasMoreElements();) { LOG.info("\t\t{}", en.nextElement().toString()); } } } catch (SocketException ex) { LOG.error("Error retrieving network interfaces.", ex); } outputHandler.writeln(metaBuffer.toString()); }
From source file:org.pepstock.jem.node.affinity.SystemInfo.java
/** * Returns the list of inet addresses scanning the network interfaces of machine * @return the list of inet addresses scanning the network interfaces of machine *//* w w w . j a va2 s . c om*/ private List<InetAddress> loadInetAddress() { List<InetAddress> list = new ArrayList<InetAddress>(); try { // scans all network interfaces Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); // scans all ip addresses Enumeration<InetAddress> addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { // adds Ip address to the collection InetAddress addr = addresses.nextElement(); list.add(addr); } } } catch (Exception e) { // debug LogAppl.getInstance().debug(e.getMessage(), e); } return list; }
From source file:nu.nethome.home.items.net.H2DatabaseTCPServer.java
public String getExternalIPAddress() { String result = null;/* w w w . j a v a 2s. c o m*/ Enumeration<NetworkInterface> interfaces2 = null; try { interfaces2 = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { logger.severe("Can't get network interfaces: " + e.getMessage()); return ""; } if (interfaces2 != null) { while (interfaces2.hasMoreElements() && StringUtils.isEmpty(result)) { NetworkInterface i = interfaces2.nextElement(); Enumeration<InetAddress> addresses2 = i.getInetAddresses(); while (addresses2.hasMoreElements() && (result == null || result.isEmpty())) { InetAddress address = addresses2.nextElement(); if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) { result = address.getHostAddress(); } } } } return result; }
From source file:org.wso2.bam.integration.tests.archive.CassandraDataArchiveTestCase.java
private InetAddress getLocalHostAddress() throws SocketException { 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; }/* w w w . jav a2 s . com*/ } } return null; }