Java Host Resolve resolveHost(String hostname)

Here you can find the source of resolveHost(String hostname)

Description

Resolves hostname and returns ip address as a string.

License

Open Source License

Declaration

public static String resolveHost(String hostname) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**// w  w  w .ja  v a 2s.  c o m
     * Resolves hostname and returns ip address as a string.
     */
    public static String resolveHost(String hostname) {
        try {
            InetAddress addr = InetAddress.getByName(hostname);
            byte[] ipAddr = addr.getAddress();
            StringBuilder ipAddrStr = new StringBuilder(15);
            for (int i = 0; i < ipAddr.length; i++) {
                if (i > 0)
                    ipAddrStr.append('.');
                ipAddrStr.append(ipAddr[i] & 0xFF);
            }
            return ipAddrStr.toString();
        } catch (UnknownHostException uhex) {
            return null;
        }
    }
}

Related

  1. resolve(String host, String path)
  2. resolveHostname()
  3. resolveHostname()
  4. resolveHostName()
  5. resolveHostName(final String hostName)