List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:org.fusesource.mop.commands.AbstractContainerBase.java
protected String getHostName() throws Exception { String hostname = "localhost"; Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface inet = interfaces.nextElement(); Enumeration<InetAddress> addresses = inet.getInetAddresses(); if (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address.isLoopbackAddress()) { InetAddress localAddress = InetAddress.getByName(address.getHostName()); hostname = localAddress.getHostName(); break; }/*from w w w .j av a2 s. c om*/ } } return hostname; }
From source file:com.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java
public void collectInterfaceAddresses(NetworkInterface aNetworkInterface, List<InetAddress> aResult, InetAddressAcceptor aAcceptor) { for (Enumeration<InetAddress> addresses = aNetworkInterface.getInetAddresses(); addresses .hasMoreElements();) {/*from w w w.j ava2 s . c o m*/ InetAddress address = addresses.nextElement(); if (aAcceptor.acceptInetAddress(aNetworkInterface, address)) { if (fLog.isInfoEnabled()) { String hostAddress = address.getHostAddress(); fLog.info(MessageFormat.format("Found ACCEPTED IP:{0}", hostAddress)); } aResult.add(address); } else { if (fLog.isInfoEnabled()) { String hostAddress = address.getHostAddress(); fLog.info(MessageFormat.format("Found SKIPPED IP:{0}", hostAddress)); } } } }
From source file:eu.musesproject.client.contextmonitoring.sensors.DeviceProtectionSensor.java
public String getIPAddress(boolean useIPv4) { try {/*from w w w. j a va 2s .co m*/ List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
From source file:com.commonsware.android.webserver.template.WebServerService.java
private void raiseReadyEvent() { ServerStartedEvent event = new ServerStartedEvent(); try {/*from w w w . ja va 2 s . com*/ for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces .hasMoreElements();) { NetworkInterface ni = enInterfaces.nextElement(); for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements();) { InetAddress addr = enAddresses.nextElement(); if (addr instanceof Inet4Address) { event.addUrl("http://" + addr.getHostAddress() + ":4999"); } } } } catch (SocketException ex) { ex.printStackTrace(); } EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().postSticky(event); }
From source file:com.example.android.toyvpn.ToyVpnService.java
public String getLocalIpAddress() { String ipv4;//from ww w. j av a 2s . c o m try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); // System.out.println("ip1--:" + inetAddress); // System.out.println("ip2--:" + inetAddress.getHostAddress()); // for getting IPV4 format if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) { String ip = inetAddress.getHostAddress().toString(); // System.out.println("ip---::" + ip); // return inetAddress.getHostAddress().toString(); return ipv4; } } } } catch (Exception ex) { Log.e("IP Address", ex.toString()); } return null; }
From source file:org.apache.nifi.processor.util.listen.AbstractListenEventProcessor.java
@OnScheduled public void onScheduled(final ProcessContext context) throws IOException { charset = Charset.forName(context.getProperty(CHARSET).getValue()); port = context.getProperty(PORT).asInteger(); events = new LinkedBlockingQueue<>(context.getProperty(MAX_MESSAGE_QUEUE_SIZE).asInteger()); final String nicIPAddressStr = context.getProperty(NETWORK_INTF_NAME).evaluateAttributeExpressions() .getValue();//w w w .ja va2 s . c o m final int maxChannelBufferSize = context.getProperty(MAX_SOCKET_BUFFER_SIZE).asDataSize(DataUnit.B) .intValue(); InetAddress nicIPAddress = null; if (!StringUtils.isEmpty(nicIPAddressStr)) { NetworkInterface netIF = NetworkInterface.getByName(nicIPAddressStr); nicIPAddress = netIF.getInetAddresses().nextElement(); } // create the dispatcher and call open() to bind to the given port dispatcher = createDispatcher(context, events); dispatcher.open(nicIPAddress, port, maxChannelBufferSize); // start a thread to run the dispatcher final Thread readerThread = new Thread(dispatcher); readerThread.setName(getClass().getName() + " [" + getIdentifier() + "]"); readerThread.setDaemon(true); readerThread.start(); }
From source file:io.github.gsteckman.rpi_rest.SsdpHandler.java
/** * Constructs a new instance of this class. *///from w w w. j a v a2 s . com private SsdpHandler() { LOG.info("Instantiating SsdpHandler"); try { // Use first IPv4 address that isn't loopback, any, or link local as the server address Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements() && serverAddress == null) { NetworkInterface i = interfaces.nextElement(); Enumeration<InetAddress> addresses = i.getInetAddresses(); while (addresses.hasMoreElements() && serverAddress == null) { InetAddress address = addresses.nextElement(); if (address instanceof Inet4Address) { if (!address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isLoopbackAddress() && !address.isMulticastAddress()) { serverAddress = address; } } } } if (serverAddress == null) { LOG.warn("Server address unknown"); } svc = SsdpService.forAllMulticastAvailableNetworkInterfaces(this); svc.listen(); // setup Multicast for Notify messages notifySocket = new MulticastSocket(); notifySocket.setTimeToLive(TTL); notifyTimer = new Timer("UPnP Notify Timer", true); notifyTimer.scheduleAtFixedRate(new NotifySender(), 5000, MAX_AGE * 1000 / 2); } catch (Exception e) { LOG.error("SsdpHandler in unknown state due to exception in constructor.", e); } }
From source file:copter.ServerConnection.java
private String getIpAddress() { String hostAddress = null;/* w w w. ja v a2s. c o m*/ try { Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces(); for (; n.hasMoreElements();) { NetworkInterface e = n.nextElement(); Enumeration<InetAddress> a = e.getInetAddresses(); for (; a.hasMoreElements();) { InetAddress addr = a.nextElement(); hostAddress = addr.getHostAddress(); break; } break; } } catch (Exception ex) { logger.log(ex.getMessage()); } return hostAddress; }
From source file:com.almende.arum.EventPusher.java
private String getHostAddress() throws SocketException { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); if (!n.isLoopback() && n.isUp() && !n.isVirtual()) { Enumeration<InetAddress> ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); if (i instanceof Inet4Address && !i.isLinkLocalAddress() && !i.isMulticastAddress()) { return i.getHostAddress().trim(); }/*from w w w . j a v a 2s. co m*/ } } } return null; }