Java IP Address Get getIpAddress(String server)

Here you can find the source of getIpAddress(String server)

Description

Perform a lookup of server returning the associated ip address.

License

Open Source License

Parameter

Parameter Description
server the hostname to convert to an ip address.

Return

String the ipAddress for the speceified server. null if the lookup of the server failed and the ip address can not be determined.

Declaration

public static String getIpAddress(String server) 

Method Source Code


//package com.java2s;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    /**//w w w.j  av  a2 s  .co  m
     * Perform a lookup of server returning the associated ip address.
     * DNS must be enabled for this call to succeed.
     *
     * @param server the hostname to convert to an ip address.
     * @return String the ipAddress for the speceified server.  null if
     * the lookup of the server failed and the ip address can not be determined.
     */
    public static String getIpAddress(String server) {
        if (server == null)
            return null;
        try {
            InetAddress addr = InetAddress.getByName(server);
            return addr.getHostAddress();
        } catch (UnknownHostException uhe) {
            return null;
        }
    }
}

Related

  1. getIPAddress()
  2. getIPAddress(boolean useIPv4)
  3. getIPAddress(final String hostName)
  4. getIPAddress(NetworkInterface e)
  5. getIPAddress(String hostname)
  6. getIpAddresses()
  7. getIPAddresses()
  8. getIPAddresses()
  9. getIpAddresses()