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:at.tugraz.ist.akm.webservice.requestprocessor.interceptor.HttpClientBackLog.java

private int getClientHash(HttpRequest httpRequest, HttpContext httpContext) {
    HttpInetConnection connection = (HttpInetConnection) httpContext
            .getAttribute(ExecutionContext.HTTP_CONNECTION);
    InetAddress inetAddress = connection.getRemoteAddress();

    Header userAgentHeader = httpRequest.getFirstHeader(HTTP.USER_AGENT);

    if (userAgentHeader != null) {
        String agentString = userAgentHeader.getValue();
        return (inetAddress.getHostAddress() + agentString).hashCode();
    }//  w  w w.  ja v a  2s. com
    return inetAddress.getHostAddress().hashCode();
}

From source file:com.springrts.springls.nat.NatHelpServer.java

/**
 * check UDP server for any new packets/*from   w  ww  .ja  v a  2  s .  co  m*/
 */
private void checkForNewPackets() {

    DatagramPacket packet;
    while ((packet = fetchNextPackage()) != null) {
        InetAddress address = packet.getAddress();
        int clientPort = packet.getPort();
        String data = new String(packet.getData(), packet.getOffset(), packet.getLength());
        LOG.trace("*** UDP packet received from {} from port {}", address.getHostAddress(), clientPort);
        Client client = getContext().getClients().getClient(data);
        if (client == null) {
            continue;
        }
        client.setUdpSourcePort(clientPort);
        client.sendLine(String.format("UDPSOURCEPORT %d", clientPort));
    }
}

From source file:com.epam.reportportal.apache.http.conn.ssl.AbstractVerifier.java

private String normaliseIPv6Address(final String hostname) {
    if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) {
        return hostname;
    }/* w  ww .  j  a v  a  2s  . c  om*/
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above
        log.error("Unexpected error converting " + hostname, uhe);
        return hostname;
    }
}

From source file:com.newrelic.agent.deps.org.apache.http.conn.ssl.AbstractVerifier.java

private String normaliseIPv6Address(final String hostname) {
    if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) {
        return hostname;
    }//from  ww w  . ja va  2 s .c o  m
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above
        log.error("Unexpected error converting " + hostname, uhe);
        return hostname;
    }
}

From source file:org.magnum.dataup.VideoController.java

private String getUrlBaseForLocalServer() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();//from w w  w  .  j  a va  2  s . co m
    InetAddress IP = null;
    try {
        IP = getLocalHostLANAddress();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    System.out.println("IP of my system is := " + IP.getHostAddress());
    String base = "http://" + IP.getHostAddress()
            + ((request.getServerPort() != 80) ? ":" + request.getServerPort() : "");
    System.out.println("dataUrl is :" + base);
    return base;
}

From source file:de.vanita5.twittnuker.util.net.TwidereHostAddressResolver.java

@Override
public String resolve(final String host) throws IOException {
    if (host == null)
        return null;
    if (isValidIpAddress(host))
        return null;
    // First, I'll try to load address cached.
    if (mHostCache.containsKey(host)) {
        if (Utils.isDebugBuild()) {
            Log.d(RESOLVER_LOGTAG, "Got cached address " + mHostCache.get(host) + " for host " + host);
        }//w  w  w  .  j a  v a2  s.c  om
        return mHostCache.get(host);
    }
    // Then I'll try to load from custom host mapping.
    // Stupid way to find top domain, but really fast.
    if (mHostMapping.contains(host)) {
        final String mappedAddr = mHostMapping.getString(host, null);
        mHostCache.put(host, mappedAddr);
        if (Utils.isDebugBuild()) {
            Log.d(RESOLVER_LOGTAG, "Got mapped address " + mappedAddr + " for host " + host);
        }
        return mappedAddr;
    }
    mSystemHosts.reloadIfNeeded();
    if (mSystemHosts.contains(host)) {
        final String hostAddr = mSystemHosts.getAddress(host);
        mHostCache.put(host, hostAddr);
        if (Utils.isDebugBuild()) {
            Log.d(RESOLVER_LOGTAG, "Got mapped address " + hostAddr + " for host " + host);
        }
        return hostAddr;
    }
    final String customMappedHost = findHost(host);
    if (customMappedHost != null) {
        mHostCache.put(host, customMappedHost);
        if (Utils.isDebugBuild()) {
            Log.d(RESOLVER_LOGTAG, "Got mapped address " + customMappedHost + " for host " + host);
        }
        return customMappedHost;
    }
    initDns();
    // Use TCP DNS Query if enabled.
    if (mDns != null && mPreferences.getBoolean(KEY_TCP_DNS_QUERY, false)) {
        final Name name = new Name(host);
        final Record query = Record.newRecord(name, Type.A, DClass.IN);
        if (query == null)
            return host;
        final Message response;
        try {
            response = mDns.send(Message.newQuery(query));
        } catch (final IOException e) {
            return host;
        }
        if (response == null)
            return host;
        final Record[] records = response.getSectionArray(Section.ANSWER);
        if (records == null || records.length < 1)
            throw new IOException("Could not find " + host);
        String hostAddr = null;
        // Test each IP address resolved.
        for (final Record record : records) {
            if (record instanceof ARecord) {
                final InetAddress ipv4Addr = ((ARecord) record).getAddress();
                if (ipv4Addr.isReachable(300)) {
                    hostAddr = ipv4Addr.getHostAddress();
                }
            } else if (record instanceof AAAARecord) {
                final InetAddress ipv6Addr = ((AAAARecord) record).getAddress();
                if (ipv6Addr.isReachable(300)) {
                    hostAddr = ipv6Addr.getHostAddress();
                }
            }
            if (hostAddr != null) {
                mHostCache.put(host, hostAddr);
                if (Utils.isDebugBuild()) {
                    Log.d(RESOLVER_LOGTAG, "Resolved address " + hostAddr + " for host " + host);
                }
                return hostAddr;
            }
        }
        // No address is reachable, but I believe the IP is correct.
        final Record record = records[0];
        if (record instanceof ARecord) {
            final InetAddress ipv4Addr = ((ARecord) record).getAddress();
            hostAddr = ipv4Addr.getHostAddress();
        } else if (record instanceof AAAARecord) {
            final InetAddress ipv6Addr = ((AAAARecord) record).getAddress();
            hostAddr = ipv6Addr.getHostAddress();
        } else if (record instanceof CNAMERecord)
            return resolve(((CNAMERecord) record).getTarget().toString());
        mHostCache.put(host, hostAddr);
        if (Utils.isDebugBuild()) {
            Log.d(RESOLVER_LOGTAG, "Resolved address " + hostAddr + " for host " + host);
        }
        return hostAddr;
    }
    if (Utils.isDebugBuild()) {
        Log.w(RESOLVER_LOGTAG, "Resolve address " + host + " failed, using original host");
    }
    return host;
}

From source file:com.jkoolcloud.tnt4j.utils.Utils.java

/**
 * Resolves the specified host name to its IP Address. If no host name is
 * given, then resolves local host IP Address.
 *
 * @param hostName/* w  w  w  .j a  v  a  2s  .c  om*/
 *            host name to resolve
 * @return string representation of IP Address
 */
public static String resolveHostNameToAddress(String hostName) {
    String hostIp = null;
    InetAddress host;
    try {
        if (isEmpty(hostName))
            host = InetAddress.getLocalHost();
        else
            host = InetAddress.getByName(hostName);

        hostIp = host.getHostAddress();
    } catch (UnknownHostException e) {
    }
    return hostIp;
}

From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.common.TCPStreamProperties.java

public TCPStreamProperties(final InetAddress addr, final int port) {
    this.hostname = addr.getHostAddress();
    this.addr = addr;
    this.port = port;
    addrResolved = true;/*from w  w  w  . j a va2 s  .c  o m*/

    init();
}

From source file:com.clustercontrol.ping.util.ReachAddress.java

/**
 * ????????????/*from  w ww .j av a  2  s . co  m*/
 * 
 * @param addressText
 * @return PING
 */
private boolean isReachable(String addressText) {

    m_message = null;
    m_messageOrg = null;
    m_lost = 0;
    m_average = 0;

    try {
        long max = 0;
        long min = 0;
        long sum = 0;
        int num = 0;
        long start = 0;
        long end = 0;

        // 
        StringBuffer buffer = new StringBuffer();

        InetAddress address = InetAddress.getByName(addressText);
        buffer.append("Pinging " + address.getHostName() + " [" + address.getHostAddress() + "].\n\n");

        int i = 0;
        for (; i < m_sentCount; i++) {

            // Reachability ?? ICMP ??
            boolean isReachable;
            // isReachable?????????
            synchronized (m_syncObj) {
                start = HinemosTime.currentTimeMillis();
                isReachable = address.isReachable(m_timeout);
                end = HinemosTime.currentTimeMillis();
            }

            long time = end - start;

            if (isReachable) {
                buffer.append("Reply from " + address.getHostAddress() + ": ");

                sum += time;

                if (i == 0) {
                    max = time;
                    min = time;
                } else {
                    if (time > max) {
                        max = time;
                    } else if (time < min) {
                        min = time;
                    }
                }
                num++;

                if (time > 0) {
                    buffer.append("time=" + time + "ms\n");
                } else {
                    buffer.append("time<1ms\n");
                }
            } else {
                if (time >= m_timeout) {
                    buffer.append("Request timed out.\n");
                } else {
                    buffer.append(
                            "Reply from " + address.getHostAddress() + ": Destination net unreachable.\n");
                    //                        num++;
                }
            }

            if (i < m_sentCount - 1) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        buffer.append("\nPing statistics for " + address.getHostAddress() + ":\n");
        //
        if (num == 0) {
            m_lost = 100;
        } else {
            m_lost = (i - num) * 100 / i;
        }

        //
        m_message = "Packets: Sent = " + i + ", Received = " + num + ", Lost = " + (i - num) + " (" + m_lost
                + "% loss),";

        buffer.append("\t" + m_message + "\n");

        buffer.append("Approximate round trip times in milli-seconds:\n");

        // ?
        if (num != 0) {
            m_average = sum / num;
        } else {
            m_average = 0;
        }

        buffer.append("\tMinimum = " + min + "ms, Maximum = " + max + "ms, Average = " + m_average + "ms\n");

        m_messageOrg = buffer.toString();
        return true;

    } catch (UnknownHostException e) {
        m_log.warn("isReachable() " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage()
                + e.getClass().getSimpleName() + ", " + e.getMessage(), e);

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage() + " (" + e.getMessage() + ")";
    } catch (IOException e) {
        m_log.warn("isReachable() " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage()
                + e.getClass().getSimpleName() + ", " + e.getMessage(), e);

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_PING.getMessage() + " (" + e.getMessage() + ")";
    }
    return false;
}

From source file:com.mirth.connect.server.api.MirthServlet.java

private boolean isRequestLocal() {
    String remoteAddr = request.getRemoteAddr();

    try {/*from   w w w .  j a v  a 2  s.  c  o m*/
        if (StringUtils.equals(InetAddress.getLocalHost().getHostAddress(), remoteAddr)) {
            return true;
        }
    } catch (UnknownHostException e) {
    }

    try {
        for (InetAddress inetAddress : InetAddress.getAllByName("localhost")) {
            if (StringUtils.equals(inetAddress.getHostAddress(), remoteAddr)) {
                return true;
            }
        }
    } catch (UnknownHostException e) {
    }

    return false;
}