List of usage examples for java.net NetworkInterface isUp
public boolean isUp() throws SocketException
From source file:edu.usc.pgroup.floe.utils.Utils.java
/** * returns the canonical host name. This assumes a unique host name for * each machine in the cluster does not apply. * FixMe: In local cloud environment (local eucalyptus in system mode) * where the DNS server is not running, this might be an issue. * @return The first IPv4 address found for any interface that is up and * running.//w w w. j a va 2 s.co m */ public static String getIpAddress() { String ip = null; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); LOGGER.error("Getting ip"); while (interfaces.hasMoreElements()) { LOGGER.error("Next iface"); NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) { continue; } Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress currentAddr = addresses.nextElement(); if (currentAddr.isLoopbackAddress() || (currentAddr instanceof Inet6Address)) { continue; } ip = currentAddr.getHostAddress(); } } } catch (SocketException e) { LOGGER.error("Error occurred while retrieving hostname" + e.getMessage()); throw new RuntimeException("Error occurred while " + "retrieving hostname" + e.getMessage()); } return ip; }
From source file:com.petalmd.armor.AbstractUnitTest.java
public static String getNonLocalhostAddress() { try {//from w w w . j ava 2 s. c o m for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { final NetworkInterface intf = en.nextElement(); if (intf.isLoopback() || !intf.isUp()) { continue; } for (final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { final InetAddress ia = enumIpAddr.nextElement(); if (ia.isLoopbackAddress() || ia instanceof Inet6Address) { continue; } return ia.getHostAddress(); } } } catch (final SocketException e) { throw new RuntimeException(e); } System.out.println("ERROR: No non-localhost address available, will use localhost"); return "localhost"; }
From source file:com.t_oster.visicut.misc.Helper.java
public static List<String> findVisiCamInstances() { List<String> result = new LinkedList<String>(); // Find the server using UDP broadcast try {/*from w w w.ja v a2 s. c o m*/ //Open a random port to send the package DatagramSocket c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "VisiCamDiscover".getBytes(); //Try the 255.255.255.255 first try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 8888); c.send(sendPacket); } catch (Exception e) { } // Broadcast the message over all the network interfaces Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; // Don't want to broadcast to the loopback interface } for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress broadcast = interfaceAddress.getBroadcast(); if (broadcast == null) { continue; } // Send the broadcast package! try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, 8888); c.send(sendPacket); } catch (Exception e) { } } } //Wait for a response byte[] recvBuf = new byte[15000]; c.setSoTimeout(3000); while (true) { DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); try { c.receive(receivePacket); //Check if the message is correct String message = new String(receivePacket.getData()).trim(); //Close the port! c.close(); if (message.startsWith("http")) { result.add(message); } } catch (SocketTimeoutException e) { break; } } } catch (IOException ex) { } return result; }
From source file:otsopack.commons.network.coordination.spacemanager.HttpSpaceManager.java
private static boolean isUp(NetworkInterface netIf) throws SocketException { try {/*from w w w .j av a 2 s . c o m*/ // In Android <= 8, the method isUp is not available. Therefore, this code will compile // but will not work. We check if it's available or not before calling it NetworkInterface.class.getDeclaredMethod("isUp"); } catch (SecurityException e) { // We can't know: we assume that it's up return true; } catch (NoSuchMethodException e) { // We can't know: we assume that it's up return true; } return netIf.isUp(); }
From source file:adams.core.net.InternetHelper.java
/** * Returns the IP address determined from the network interfaces (using * the IP address of the one with a proper host name). * * @return the IP address// ww w . j a v a 2s . c o m */ public static synchronized String getIPFromNetworkInterface() { String result; List<String> list; Enumeration<NetworkInterface> enmI; NetworkInterface intf; Enumeration<InetAddress> enmA; InetAddress addr; boolean found; result = null; if (m_IPNetworkInterface == null) { list = new ArrayList<>(); found = false; try { enmI = NetworkInterface.getNetworkInterfaces(); while (enmI.hasMoreElements()) { intf = enmI.nextElement(); // skip non-active ones if (!intf.isUp()) continue; enmA = intf.getInetAddresses(); while (enmA.hasMoreElements()) { addr = enmA.nextElement(); list.add(addr.getHostAddress()); if (addr.getHostName().indexOf(':') == -1) { result = addr.getHostAddress(); found = true; break; } } if (found) break; } } catch (Exception e) { // ignored } if (result == null) { if (list.size() > 0) result = list.get(0); else result = "<unknown>"; } m_IPNetworkInterface = result; } else { result = m_IPNetworkInterface; } return result; }
From source file:adams.core.net.InternetHelper.java
/** * Returns the host name determined from the network interfaces. * * @return the host name, null if none found *//*from ww w . jav a 2 s . c om*/ public static synchronized String getHostnameFromNetworkInterface() { String result; List<String> list; Enumeration<NetworkInterface> enmI; NetworkInterface intf; Enumeration<InetAddress> enmA; InetAddress addr; boolean found; result = null; if (m_HostnameNetworkInterface == null) { list = new ArrayList<>(); found = false; try { enmI = NetworkInterface.getNetworkInterfaces(); while (enmI.hasMoreElements()) { intf = enmI.nextElement(); // skip non-active ones if (!intf.isUp()) continue; enmA = intf.getInetAddresses(); while (enmA.hasMoreElements()) { addr = enmA.nextElement(); list.add(addr.getHostName()); if (addr.getHostName().indexOf(':') == -1) { result = addr.getHostName(); found = true; break; } } if (found) break; } } catch (Exception e) { // ignored } if (result == null) { if (list.size() > 0) result = list.get(0); else result = "<unknown>"; } m_HostnameNetworkInterface = result; } else { result = m_HostnameNetworkInterface; } return result; }
From source file:eu.stratosphere.nephele.discovery.DiscoveryService.java
/** * Returns the set of broadcast addresses available to the network interfaces of this host. In case of IPv6 the set * contains the IPv6 multicast address to reach all nodes on the local link. Moreover, all addresses of the loopback * interfaces are added to the set./* www.jav a 2 s. c o m*/ * * @return (possibly empty) set of broadcast addresses reachable by this host */ private static Set<InetAddress> getBroadcastAddresses() { final Set<InetAddress> broadcastAddresses = new HashSet<InetAddress>(); // get all network interfaces Enumeration<NetworkInterface> ie = null; try { ie = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Could not collect network interfaces of host", e); return broadcastAddresses; } while (ie.hasMoreElements()) { NetworkInterface nic = ie.nextElement(); try { if (!nic.isUp()) { continue; } if (nic.isLoopback()) { for (InterfaceAddress adr : nic.getInterfaceAddresses()) { broadcastAddresses.add(adr.getAddress()); } } else { // check all IPs bound to network interfaces for (InterfaceAddress adr : nic.getInterfaceAddresses()) { if (adr == null) { continue; } // collect all broadcast addresses if (USE_IPV6) { try { final InetAddress interfaceAddress = adr.getAddress(); if (interfaceAddress instanceof Inet6Address) { final Inet6Address ipv6Address = (Inet6Address) interfaceAddress; final InetAddress multicastAddress = InetAddress.getByName(IPV6MULTICASTADDRESS + "%" + Integer.toString(ipv6Address.getScopeId())); broadcastAddresses.add(multicastAddress); } } catch (UnknownHostException e) { LOG.error(e); } } else { final InetAddress broadcast = adr.getBroadcast(); if (broadcast != null) { broadcastAddresses.add(broadcast); } } } } } catch (SocketException e) { LOG.error("Socket exception when checking " + nic.getName() + ". " + "Ignoring this device.", e); } } return broadcastAddresses; }
From source file:terrastore.communication.NodeConfiguration.java
private String getPublishAddresses() throws Exception { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); StringBuilder result = new StringBuilder(); while (nics != null && nics.hasMoreElements()) { NetworkInterface nic = nics.nextElement(); if (nic.isUp() && !nic.isLoopback() && !nic.isVirtual() && nic.getInetAddresses().hasMoreElements()) { Enumeration<InetAddress> addresses = nic.getInetAddresses(); while (addresses.hasMoreElements()) { if (result.length() > 0) { result.append(","); }/* w ww . jav a2s . c o m*/ result.append(addresses.nextElement().getHostAddress()); } } } return result.toString(); }
From source file:org.openhab.binding.zway.internal.discovery.ZWayBridgeDiscoveryService.java
private void scan() { logger.debug("Starting scan for Z-Way Server"); ValidateIPV4 validator = new ValidateIPV4(); try {/*w w w .jav a 2s.co m*/ Enumeration<NetworkInterface> enumNetworkInterface = NetworkInterface.getNetworkInterfaces(); while (enumNetworkInterface.hasMoreElements()) { NetworkInterface networkInterface = enumNetworkInterface.nextElement(); if (networkInterface.isUp() && !networkInterface.isVirtual() && !networkInterface.isLoopback()) { for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { if (validator.isValidIPV4(address.getAddress().getHostAddress())) { String ipAddress = address.getAddress().getHostAddress(); Short prefix = address.getNetworkPrefixLength(); logger.debug("Scan IP address for Z-Way Server: {}", ipAddress); // Search on localhost first scheduler.execute(new ZWayServerScan(ipAddress)); String subnet = ipAddress + "/" + prefix; SubnetUtils utils = new SubnetUtils(subnet); String[] addresses = utils.getInfo().getAllAddresses(); for (String addressInSubnet : addresses) { scheduler.execute(new ZWayServerScan(addressInSubnet)); } } } } } } catch (SocketException e) { logger.warn("Error occurred while searching Z-Way servers ({})", e.getMessage()); } }
From source file:org.openhab.binding.vera.internal.discovery.VeraBridgeDiscoveryService.java
private void scan() { logger.debug("Starting scan for Vera controller"); ValidateIPV4 validator = new ValidateIPV4(); try {//ww w . j a v a 2s . c o m Enumeration<NetworkInterface> enumNetworkInterface = NetworkInterface.getNetworkInterfaces(); while (enumNetworkInterface.hasMoreElements()) { NetworkInterface networkInterface = enumNetworkInterface.nextElement(); if (networkInterface.isUp() && !networkInterface.isVirtual() && !networkInterface.isLoopback()) { for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { if (validator.isValidIPV4(address.getAddress().getHostAddress())) { String ipAddress = address.getAddress().getHostAddress(); Short prefix = address.getNetworkPrefixLength(); logger.debug("Scan IP address for Vera Controller: {}", ipAddress); String subnet = ipAddress + "/" + prefix; SubnetUtils utils = new SubnetUtils(subnet); String[] addresses = utils.getInfo().getAllAddresses(); for (String addressInSubnet : addresses) { scheduler.execute(new VeraControllerScan(addressInSubnet)); } } } } } } catch (SocketException e) { logger.warn("Error occurred while searching Vera controller: ", e); } }