Example usage for java.net InetAddress getHostAddress

List of usage examples for java.net InetAddress getHostAddress

Introduction

In this page you can find the example usage for java.net InetAddress getHostAddress.

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation.

Usage

From source file:com.jsystem.j2autoit.AutoItAgent.java

private static void startAutoItWebServer(int port) {

    if (webServer != null) {
        webServer = null;// w  w w.  ja  v  a 2  s  .  c o m
        System.gc();
    }
    InetAddress addr = null;
    try {
        addr = InetAddress.getLocalHost();
        webServer = new WebServer(port, addr);
        Log.info("Setting J2AutoIt Agent to use address: " + addr.getHostAddress() + ":" + port + NEW_LINE);
    } catch (Exception exception) {
        webServer = new WebServer(port);
        Log.info("Setting J2AutoIt Agent to use port: " + port + NEW_LINE);
    }
    try {
        PropertyHandlerMapping phm = new PropertyHandlerMapping();
        phm.addHandler("autoit", AutoItAgent.class);
        XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
        xmlRpcServer.setHandlerMapping(phm);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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/*w  w w  .j a v a 2  s.  co 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:com.tingtingapps.securesms.mms.LegacyMmsConnection.java

@SuppressWarnings("TryWithIdenticalCatches")
protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio)
        throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }//from   w w w.  java  2s.co  m
        Log.w(TAG, "returning vacuous success since MMS radio is not in use");
        return true;
    }

    if (inetAddress == null) {
        throw new IOException("Unable to lookup host: InetAddress.getByName() returned null.");
    }

    byte[] ipAddressBytes = inetAddress.getAddress();
    if (ipAddressBytes == null) {
        Log.w(TAG, "resolved IP address bytes are null, returning true to attempt a connection anyway.");
        return true;
    }

    Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        final Method requestRouteMethod = manager.getClass().getMethod("requestRouteToHostAddress",
                Integer.TYPE, InetAddress.class);
        final boolean routeToHostObtained = (Boolean) requestRouteMethod.invoke(manager,
                MmsRadio.TYPE_MOBILE_MMS, inetAddress);
        Log.w(TAG, "requestRouteToHostAddress(" + inetAddress + ") -> " + routeToHostObtained);
        return routeToHostObtained;
    } catch (NoSuchMethodException nsme) {
        Log.w(TAG, nsme);
    } catch (IllegalAccessException iae) {
        Log.w(TAG, iae);
    } catch (InvocationTargetException ite) {
        Log.w(TAG, ite);
    }

    final int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
    final boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
    Log.w(TAG, "requestRouteToHost(" + ipAddress + ") -> " + routeToHostObtained);
    return routeToHostObtained;
}

From source file:org.onebusaway.webapp.actions.StatusAction.java

@Override
public String execute() {

    Map<String, Object> configuration = _configurationService.getConfiguration(_forceRefresh, ERROR);
    _model.putAll(configuration);//from  w w w .  j  a v  a  2s  .c om

    try {
        InetAddress address = InetAddress.getLocalHost();
        _model.put("hostAddress", address.getHostAddress());
        _model.put("hostName", address.getHostName());
    } catch (Exception ex) {
        _log.warn("error determining hostname", ex);
    }

    return SUCCESS;
}

From source file:com.adito.core.InterfacesMultiSelectListDataSource.java

public Collection<LabelValueBean> getValues(SessionInfo session) {
    ArrayList l = new ArrayList();
    try {/*from  w  ww .  j ava2  s.  c  o m*/
        // TODO make this localised
        l.add(new LabelValueBean("All Interfaces", "0.0.0.0"));
        for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
            NetworkInterface ni = (NetworkInterface) e.nextElement();
            for (Enumeration e2 = ni.getInetAddresses(); e2.hasMoreElements();) {
                InetAddress addr = (InetAddress) e2.nextElement();
                l.add(new LabelValueBean(addr.getHostAddress(), addr.getHostAddress()));
            }
        }
    } catch (Throwable t) {
        log.error("Failed to list network interfaces.", t);
    }
    return l;
}

From source file:jeffaschenk.tomcat.instance.generator.builders.TomcatInstanceBuilderHelper.java

/**
 * Get this Instances Default IP Address.
 *
 * @return String containing IP Address or 'localhost' if system command fails.
 *//*from  w  w  w . j  av  a 2 s  . c  o m*/
protected static final String getThisDefaultInetAddress() {
    try {
        InetAddress ipAddress = InetAddress.getLocalHost();
        return ipAddress.getHostAddress();
    } catch (Exception e) {
        return "localhost";
    }
}

From source file:com.bigdata.dastor.client.RingCache.java

public RingCache() {
    for (InetAddress seed : DatabaseDescriptor.getSeeds()) {
        seeds_.add(seed.getHostAddress());
    }
    refreshEndPointMap();
}

From source file:net.dfs.server.filespace.creator.impl.FileSpaceCreatorImpl.java

/**
 * getSpace will simply creates a security manager. Establishes a RMI connection 
 * which will be used in connection to the remote nodes. And finally, will create 
 * a virtual Space and return an instance of it to the caller. An 
 * IOException or a ClassNotFoundException will be thrown on a failure.
 * /* ww w . ja  v  a  2  s.  c om*/
 * @param host the address of the node in which the Space needed to be created.
 * @return the newly created Space.
 */
@SuppressWarnings("unchecked")
public JavaSpace getSpace(InetAddress host, InetAddress requester) {

    try {
        log.info("Space Reqested by " + requester.getHostAddress());

        SecurityPolicyManager.securityManager();
        LookupLocator lookup = new LookupLocator("jini://localhost");
        ServiceRegistrar registrar = lookup.getRegistrar();

        Class[] types = new Class[] { JavaSpace.class };
        JavaSpace space = (JavaSpace) registrar.lookup(new ServiceTemplate(null, types, null));

        log.info("Space Returned to " + requester.getHostAddress());

        return space;

    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.exit(1);
    }
    return null;
}

From source file:org.esigate.extension.http.DNSTest.java

private List<String> toIPs(InetAddress[] inetAddresses) {
    List<String> ips = new ArrayList<>();
    for (InetAddress inetAddress : inetAddresses) {
        ips.add(inetAddress.getHostAddress());

    }/*from  ww  w . j  a  v  a 2  s.  com*/
    return ips;
}

From source file:jetbrains.buildServer.clouds.azure.asm.connector.AzureApiConnector.java

private static ArrayList<ConfigurationSet> createConfigurationSetList(int port, String serverLocation) {
    ArrayList<ConfigurationSet> retval = new ArrayList<>();
    final ConfigurationSet value = new ConfigurationSet();
    value.setConfigurationSetType(ConfigurationSetTypes.NETWORKCONFIGURATION);
    final ArrayList<InputEndpoint> endpointsList = new ArrayList<>();
    value.setInputEndpoints(endpointsList);

    if (port > 0) {
        InputEndpoint endpoint = new InputEndpoint();
        endpointsList.add(endpoint);//from   w ww .  j  a  v a 2s.  com
        endpoint.setLocalPort(port);
        endpoint.setPort(port);
        endpoint.setProtocol("TCP");
        endpoint.setName(AzurePropertiesNames.ENDPOINT_NAME);
        final EndpointAcl acl = new EndpointAcl();
        endpoint.setEndpointAcl(acl);

        final URI serverUri = URI.create(serverLocation);
        List<InetAddress> serverAddresses = new ArrayList<>();
        try {
            serverAddresses.addAll(Arrays.asList(InetAddress.getAllByName(serverUri.getHost())));
            serverAddresses.add(InetAddress.getLocalHost());
        } catch (UnknownHostException e) {
            LOG.warn("Unable to identify server name ip list", e);
        }
        final ArrayList<AccessControlListRule> aclRules = new ArrayList<>();
        acl.setRules(aclRules);
        int order = 1;
        for (final InetAddress address : serverAddresses) {
            if (!(address instanceof Inet4Address)) {
                continue;
            }
            final AccessControlListRule rule = new AccessControlListRule();
            rule.setOrder(order++);
            rule.setAction("Permit");
            rule.setRemoteSubnet(address.getHostAddress() + "/32");
            rule.setDescription("Server");
            aclRules.add(rule);
        }
    }

    retval.add(value);
    return retval;
}