List of usage examples for java.net NetworkInterface isLoopback
public boolean isLoopback() throws SocketException
From source file:davmail.exchange.ExchangeSessionFactory.java
/** * Check if at least one network interface is up and active (i.e. has an address) * * @return true if network available//from ww w . j av a 2 s. c o m */ static boolean checkNetwork() { boolean up = false; Enumeration<NetworkInterface> enumeration; try { enumeration = NetworkInterface.getNetworkInterfaces(); while (!up && enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); up = networkInterface.isUp() && !networkInterface.isLoopback() && networkInterface.getInetAddresses().hasMoreElements(); } } catch (NoSuchMethodError error) { ExchangeSession.LOGGER.debug("Unable to test network interfaces (not available under Java 1.5)"); up = true; } catch (SocketException exc) { ExchangeSession.LOGGER.error( "DavMail configuration exception: \n Error listing network interfaces " + exc.getMessage(), exc); } return up; }
From source file:org.cc86.MMC.client.Main.java
public static String serverDiscovery() { String res = "0.0.0.0"; DatagramSocket c;//from w w w . java 2 s . co m // Find the server using UDP broadcast try { //Open a random port to send the package c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "DISCOVER_MMC_REQUEST".getBytes(); //Try the 255.255.255.255 first try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 0xCC86); c.send(sendPacket); l.info("Request packet sent to: 255.255.255.255 (DEFAULT)"); } catch (Exception e) { } // Broadcast the message over all the network interfaces Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface 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) { } l.info("Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName()); } } l.info("Done looping over all network interfaces. Now waiting for a reply!"); //Wait for a response byte[] recvBuf = new byte[15000]; DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); c.receive(receivePacket); //We have a response l.info("Broadcast response from server: " + receivePacket.getAddress().getHostAddress()); //Check if the message is correct String message = new String(receivePacket.getData()).trim(); if (message.equals("DISCOVER_MMC_RESPONSE")) { //DO SOMETHING WITH THE SERVER'S IP (for example, store it in your controller) res = (receivePacket.getAddress() + "").substring(1); } //Close the port! c.close(); } catch (IOException ex) { } return res; }
From source file:org.opendaylight.lispflowmapping.implementation.lisp.MapServer.java
private static InetAddress getLocalAddress() { try {/* w w w. j av a 2 s .c o m*/ Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); LOG.debug("Interface " + current.toString()); if (!current.isUp() || current.isLoopback() || current.isVirtual()) { continue; } Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress current_addr = addresses.nextElement(); // Skip loopback and link local addresses if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress()) { continue; } LOG.debug(current_addr.getHostAddress()); return current_addr; } } } catch (SocketException se) { LOG.debug("Caught socket exceptio", se); } return null; }
From source file:net.straylightlabs.archivo.controller.TelemetryController.java
private static List<String> getNetworkInterfaces() { List<String> nics = new ArrayList<>(); try {/*from w ww .j av a2 s . com*/ for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (nic.isUp()) nics.add(String.format( "name='%s' isLoopback=%b isP2P=%b isVirtual=%b multicast=%b addresses=[%s]\n", nic.getDisplayName(), nic.isLoopback(), nic.isPointToPoint(), nic.isVirtual(), nic.supportsMulticast(), getAddressesAsString(nic))); } } catch (SocketException e) { logger.error("Error fetching network interface list: ", e); } return nics; }
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); }/*w w w. j a v a 2 s. c om*/ System.out.printf("Parent: %s%n", netint.getParent()); System.out.printf("Up? %s%n", netint.isUp()); System.out.printf("Loopback? %s%n", netint.isLoopback()); System.out.printf("PointToPoint? %s%n", netint.isPointToPoint()); System.out.printf("Supports multicast? %s%n", netint.isVirtual()); System.out.printf("Virtual? %s%n", netint.isVirtual()); System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress())); System.out.printf("MTU: %s%n", netint.getMTU()); List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses(); for (InterfaceAddress addr : interfaceAddresses) { System.out.printf("InterfaceAddress: %s%n", addr.getAddress()); } System.out.printf("%n"); Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces(); for (NetworkInterface networkInterface : Collections.list(subInterfaces)) { System.out.printf("%nSubInterface%n"); displayInterfaceInformation(networkInterface); } System.out.printf("%n"); }
From source file:net.grinder.util.NetworkUtils.java
/** * Check if the current machine support IP6 * * @return true if the IP6 is supported. *///from www. j av a 2 s . co m public static boolean isIP6Supported() { final Enumeration<NetworkInterface> networkInterfaces; try { networkInterfaces = getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { final NetworkInterface networkInterface = networkInterfaces.nextElement(); if (networkInterface.isUp() && !networkInterface.isLoopback() && !networkInterface.isPointToPoint()) { final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { final InetAddress inetAddress = inetAddresses.nextElement(); if (inetAddress instanceof Inet6Address) { return true; } } } } } catch (SocketException e) { LOGGER.error("Error while resolving non look back local addresses.", e); } return false; }
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 v a2 s . c o 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 {//w w w. j a v a 2s . com 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 ww w . ja va 2 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:ws.argo.DemoWebClient.Browser.BrowserController.java
private static String getHostIPAddressFromString(String propIPAddress, String niName) { String hostIPAddr = propIPAddress; if (propIPAddress.equals("")) { // If the listenerIPAddress is blank, then try to get the ip address of // the interface try {/* w ww.j a v a2 s. c o m*/ NetworkInterface ni = null; if (niName != null) ni = NetworkInterface.getByName(niName); if (ni == null) { LOGGER.info("Network Interface name not specified or incorrect. Defaulting to localhost"); hostIPAddr = InetAddress.getLocalHost().getHostAddress(); } else { if (!ni.isLoopback()) { Enumeration<InetAddress> ee = ni.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); if (i instanceof Inet4Address) { hostIPAddr = i.getHostAddress(); break; // get the first one an get out of the loop } } } } } catch (SocketException e) { LOGGER.warn("A socket exception occurred."); } catch (UnknownHostException e) { LOGGER.warn("Error finding Network Interface", e); } } return hostIPAddr; }