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:org.jivesoftware.smack.ConnectionConfiguration.java

/**
 * Returns the host to use when establishing the connection. The host and port to use
 * might have been resolved by a DNS lookup as specified by the XMPP spec (and therefore
 * may not match the {@link #getServiceName service name}.
 *
 * @return the host to use when establishing the connection.
 *//*  w  w w. ja  v a  2 s.  c  o m*/
public String getHost() {
    // ?IP???IP?
    if (!InetAddressUtils.isIPv4Address(host) && !InetAddressUtils.isIPv6Address(host)) {
        try {
            InetAddress address = InetAddress.getByName(host);
            host = address.getHostAddress();
            Log.d(LOG_TAG, "transform host name to host address:" + host);
        } catch (UnknownHostException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
    }
    return host;
}

From source file:com.momathink.common.tools.ToolMonitor.java

/**
 * ?ip/*from ww  w .  j a v a2 s.c  om*/
 * @return  : "111.111.111.111"
 */
private String getRealIp() {
    String netip = null;// IP
    try {
        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        boolean finded = false;// ?IP
        while (netInterfaces.hasMoreElements() && !finded) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                ip = address.nextElement();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {// IP
                    netip = ip.getHostAddress();
                    finded = true;
                    break;
                }
            }
        }
    } catch (Exception e) {
    }
    return netip;
}

From source file:com.shreymalhotra.crazyflieserver.MainActivity.java

public String getLocalIpAddress() {
    try {//from w w w . j  av a2  s  .  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.isLinkLocalAddress() &&
                // inetAddress.isSiteLocalAddress() ) {
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    String ipAddr = inetAddress.getHostAddress();
                    return ipAddr;
                }
            }
        }
    } catch (SocketException ex) {
        Log.d("CrazyFlieServer", ex.toString());
    }
    return null;
}

From source file:com.bigdata.dastor.db.HintedHandOffManager.java

private void deliverHintsToEndpoint(InetAddress endPoint)
        throws IOException, DigestMismatchException, InvalidRequestException, TimeoutException {
    queuedDeliveries.remove(endPoint);//from w w w .j  a  v  a  2 s .  c om

    // BIGDATA: sleep a little, because sometimes, the immediate sending will fail.
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
    }

    logger_.info("Hinted handoff START for endPoint " + endPoint.getHostAddress());
    long startTime = System.currentTimeMillis();

    // 1. For each table and cf, scan through SystemTable.HintsColumnFamily to find all rows 
    //    of this edpoint.
    // 2. For each key (column) send application data to the endpoint.
    // 3. Delete the key (column) from SystemTable.HintsColumnFamily.
    // 4. Now force a flush
    // 5. Do major compaction to clean up all deletes etc.
    rowsReplayed = 0; // BIGDATA for JMX: change int to long
    deliveringEndPoint = endPoint; // BIGDATA for JMX

    ColumnFamilyStore hintStore = Table.open(Table.SYSTEM_TABLE).getColumnFamilyStore(HINTS_CF);
    delivery: for (String tableName : DatabaseDescriptor.getNonSystemTables()) // BIGDATA: ignore the system table
    {
        deliveringTable = tableName; // BIGDATA for JMX            
        Set<String> cfs = Table.open(tableName).getColumnFamilies();
        for (String cf : cfs) {
            deliveringCf = cf;
            long rowsReplayedOfCf = 0;
            byte[] startColumn = ArrayUtils.EMPTY_BYTE_ARRAY;
            String hintKey = makeHintKey(endPoint.getHostAddress(), tableName, cf);
            while (true) {
                QueryFilter filter = new SliceQueryFilter(hintKey, new QueryPath(HINTS_CF), startColumn,
                        ArrayUtils.EMPTY_BYTE_ARRAY, false, PAGE_SIZE);
                ColumnFamily hintColumnFamily = ColumnFamilyStore
                        .removeDeleted(hintStore.getColumnFamily(filter), Integer.MAX_VALUE);
                if (pagingFinished(hintColumnFamily, startColumn))
                    break;
                Collection<IColumn> keys = hintColumnFamily.getSortedColumns();

                for (IColumn keyColumn : keys) {
                    String keyStr = new String(keyColumn.name(), "UTF-8");

                    if (logger_.isDebugEnabled())
                        logger_.debug(
                                String.format("Hinted handoff SENDING key %s to endpoint %s for kfsf %s:%s",
                                        keyStr, endPoint.getHostAddress(), tableName, cf));

                    if (sendMessage(endPoint, tableName, cf, keyStr)) {
                        deleteHintKey(hintKey, keyColumn.name());
                        rowsReplayed++;
                        rowsReplayedOfCf++;
                    } else {
                        logger_.warn(String.format(
                                "Hinted handoff STOP, could not complete hinted handoff to %s when sending %s for kfsf %s:%s",
                                endPoint.getHostAddress(), keyStr, tableName, cf));
                        // BIGDATA: here, the hinted data cannot be completely sent to the endPoint.
                        // It may because the endPoint is down again.
                        // the break will stop this hinted-handoff, and only another endpoint up/down can trigger another hinted-handoff.
                        break delivery;
                    }

                    startColumn = keyColumn.name();
                }

                logger_.info(String.format("Hinted handoff PROGRESS, have sent %s/%s rows to %s for kfsf %s:%s",
                        rowsReplayedOfCf, rowsReplayed, endPoint.getHostAddress(), tableName, cf));
            }

            if (rowsReplayedOfCf > 0) {
                logger_.info(String.format(
                        "Hinted handoff DONE of %s rows to endpoint %s for kscf %s:%s, used time(ms):%s",
                        rowsReplayedOfCf, endPoint.getHostAddress(), tableName, cf,
                        System.currentTimeMillis() - startTime));
            }
        }
    }

    if (rowsReplayed > 0) {
        try {
            hintStore.forceBlockingFlush(); // BIGDATA: changed to blockingFlush
            CompactionManager.instance.submitMajor(hintStore, 0, Integer.MAX_VALUE).get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    logger_.info(String.format("Hinted handoff FINISHED of %s rows to endpoint %s, used time(ms):%s",
            rowsReplayed, endPoint.getHostAddress(), System.currentTimeMillis() - startTime));
    // BIGDATA: clean JMX
    rowsReplayed = 0;
    deliveringEndPoint = null;
    deliveringTable = null;
    deliveringCf = null;
}

From source file:fi.tut.fast.MulticastProducer.java

@Override
protected void doStart() throws Exception {
    super.doStart();

    InetAddress addr = null;
    Class addrClass;//from   ww w  .j  a v a  2s. com
    if (endpoint.getMulticastGroup() instanceof Inet4Address) {
        addrClass = Inet4Address.class;
    } else {
        addrClass = Inet6Address.class;
    }

    for (Enumeration<InetAddress> as = endpoint.getInterface().getInetAddresses(); as.hasMoreElements();) {
        InetAddress a = as.nextElement();
        if (addrClass.isInstance(a)) {
            addr = a;
        }
    }

    if (addr == null) {
        addr = endpoint.getInterface().getInetAddresses().nextElement();
    }

    LOG.info(String.format("Starting Producer Endpoint on interface %s (%s) ",
            endpoint.getInterface().getDisplayName(), addr.getHostAddress()));

    s = new MulticastSocket(new InetSocketAddress(addr.getHostAddress(), endpoint.getSourcePort()));
    s.setReuseAddress(true);
    s.setNetworkInterface(endpoint.getInterface());
    s.setLoopbackMode(true);
    //      s.setBroadcast(true);

    //      s.joinGroup(endpoint.getMulticastGroup());
    //      s.connect(endpoint.getMulticastGroup(), endpoint.getAddress().getPort());
    //      s.bind(new InetSocketAddress(endpoint.getMulticastGroup(), 0));
}

From source file:com.jcn.dlna.sdk.dms.httpserver.HttpServer.java

synchronized public void startServer() {
    if (isStarted) {
        return;/*from  w  w  w  .j a va2 s  .  c o m*/
    }
    InetAddress localInetAddress = getLocalInetAddress();
    if (localInetAddress == null || !WifiReceiver.getInstance().isConnected()) {
        log.severe("Can't start server, can't find local network interface IP address to bind to");
        return;
    }
    try {
        listenerThread = new ListenerThread(localInetAddress, getListenPort(), getParams(),
                getHandlerRegistry());
        listenerThread.start(); // Don't need non-daemon status, we are on
        // Android
        log.info("HTTP server start finish");
        log.info("address-->" + localInetAddress.getHostAddress() + ":" + getLocalPort());
        isStarted = true;
    } catch (IOException ex) {
        log.severe("Can't start server, error binding listener socket: " + ex);
    }
}

From source file:com.lfv.yada.net.client.ClientNetworkManager.java

private InetAddress getLocalHostFix() throws SocketException {
    // Hack for getting the localhost inetaddress on linux systems
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface networkInterface = (NetworkInterface) e.nextElement();
        Enumeration e2 = networkInterface.getInetAddresses();
        while (e2.hasMoreElements()) {
            InetAddress ip = (InetAddress) e2.nextElement();
            if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                return ip;
            }//from  ww w . java 2  s .c om
        }
    }
    return null;
}

From source file:com.gbcom.system.controller.SysInfoController.java

/**
 * ?//from w  w  w . j  a va2s. c  om
 * 
 * @param model
 *            Model
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @throws Exception
 *             Exception
 */
@RequestMapping
public void dbBackup(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {

    try {
        Date date = new Date();
        String product = OemManager.getInstance().getOem().getVendor().getProduct();
        // LinkedHashMap<String, String> map = CmUtil.getLocalAddress();
        String ip = "";
        /*
         * for ( Entry<String, String> entry : map.entrySet()) { ip =
         * entry.getValue(); }
         */
        InetAddress addr = InetAddress.getLocalHost();
        ip = addr.getHostAddress();
        String fileName = product + "-" + ip + "-" + date.getTime() + ".sql";
        String path = SystemUtils.USER_HOME + File.separator + fileName;
        SqlExportManager sqlService = new SqlExportManager();
        boolean isSuccess = sqlService.exportSql(path);
        if (isSuccess) {
            sendSuccessJSON(response, "??", path);
        } else {
            sendFailureJSON(response, "?");
            logger.info("?");
        }
    } catch (Exception e) {
        super.processException(response, e);
    }
}

From source file:cn.apputest.ctria.section2.Lucha2Activity.java

public String getLocalIpAddressGPRS() {
    try {/*from w w w . ja va 2 s .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()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreferenceIpAddress", "ex.toString()");
    }
    return null;
}