Java Utililty Methods InetAddress Create

List of utility methods to do InetAddress Create

Description

The list of methods to do InetAddress Create are organized into topic(s).

Method

StringgetHostAddress(InetAddress anAddress)
Normalized version of java.net.InetAddress#getHostAddress() that handles IPv6 addresss formatting using the style of IETF RFC 2732 and also handles removal of IPv6 scoping identifiers.
String hostAddress;
if (anAddress instanceof Inet6Address) {
    hostAddress = anAddress.getHostAddress();
    int percentAt = hostAddress.indexOf('%');
    if (-1 == percentAt) {
        hostAddress = "[" + hostAddress + "]";
    } else {
        hostAddress = "[" + hostAddress.substring(0, percentAt) + "]";
...
StringgetHostAddress(InetAddress host)
get Host Address
String hostAddr = host.getHostAddress();
if (host instanceof Inet6Address) {
    if (hostAddr.lastIndexOf('%') >= 0) {
        hostAddr = hostAddr.substring(0, hostAddr.lastIndexOf('%'));
return hostAddr;
StringgetHostName(InetAddress address)
This method tries to determine the hostname of the given InetAddress without triggering a reverse DNS lookup.
String result;
String hostAddress = address.getHostAddress();
String inetAddr = address.toString();
int index1 = inetAddr.lastIndexOf('/');
int index2 = inetAddr.indexOf(hostAddress);
if (index2 == index1 + 1) {
    if (index1 == 0) {
        result = hostAddress;
...
StringgetHostName(InetAddress ia)
get Host Name
return ia.getHostName();
StringgetHostNameReliably(final String requestingHost, final InetAddress site, final URL requestingUrl)
get Host Name Reliably
String host = requestingHost;
if (host == null) {
    if (site != null) {
        host = site.getHostName();
    } else if (requestingUrl != null) {
        host = requestingUrl.getHost();
host = host == null ? "" : host;
return host;
StringgetHostNameWithoutDomain(final InetAddress addr)
get Host Name Without Domain
final String hostName = addr.getHostName();
final int pos = hostName.indexOf('.');
if (pos == -1) {
    return hostName;
} else {
    return hostName.substring(0, pos);
InetAddressgetInetAddress()
get Inet Address
try {
    return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
    hostName = "unknown host!";
return null;
StringgetInetAddress()
get Inet Address
try {
    InetAddress inet = InetAddress.getLocalHost();
    return inet.getHostAddress();
} catch (UnknownHostException e) {
    e.printStackTrace();
    return "";
InetAddressgetInetAddress()
get Inet Address
InetAddress currentAddress = null;
try {
    InetAddress addrs[] = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
    for (InetAddress addr : addrs) {
        if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
            currentAddress = addr;
            break;
    if (currentAddress == null || currentAddress.getHostAddress() == null
            || currentAddress.getHostAddress().length() == 0) {
        try {
            Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
            for (NetworkInterface iface : Collections.list(ifaces)) {
                Enumeration<InetAddress> raddrs = iface.getInetAddresses();
                for (InetAddress raddr : Collections.list(raddrs)) {
                    if (!raddr.isLoopbackAddress() && raddr.isSiteLocalAddress()) {
                        currentAddress = raddr;
                        break;
        } catch (SocketException e) {
            e.printStackTrace();
} catch (UnknownHostException e) {
if (currentAddress != null && currentAddress.getHostAddress() != null
        && currentAddress.getHostAddress().length() > 0) {
    return currentAddress;
return null;
InetAddressgetInetAddress()
Return the local host address
InetAddress address = null;
try {
    address = getFirstNonLoopbackAddress(false, false);
} catch (SocketException e) {
    e.printStackTrace();
if (address == null) {
    address = InetAddress.getLocalHost();
...