Java Utililty Methods Host Address Get

List of utility methods to do Host Address Get

Description

The list of methods to do Host Address Get are organized into topic(s).

Method

StringgetHost(final String address)
Returns host for the specified address.
final URL url = getURL(address);
return url != null ? url.getHost() : null;
StringgetHostAddress()
Returns the textual presentation of the local host IP address.
return java.net.InetAddress.getLocalHost().getHostAddress();
StringgetHostAddress()
get Host Address
String strIP = "";
try {
    InetAddress thisIp = InetAddress.getLocalHost();
    strIP = thisIp.getHostAddress();
} catch (Exception e) {
    e.printStackTrace();
    strIP = "";
return strIP;
StringgetHostAddress()
get Host Address
try {
    return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException uhe) {
    throw new Error("Couldn't get this host address, which is needed to register in the database");
StringgetHostAddress()
get Host Address
try {
    return (InetAddress.getLocalHost()).getHostAddress();
} catch (UnknownHostException uhe) {
    String host = uhe.getMessage();
    if (host != null) {
        int colon = host.indexOf(':');
        if (colon > 0) {
            return host.substring(0, colon);
...
StringgetHostAddress()
Get the ip address of local host.
DatagramSocket ds = new DatagramSocket();
ds.connect(InetAddress.getByName(DUMMY_OUT_IP), 80);
InetAddress localAddress = ds.getLocalAddress();
if (localAddress.getHostAddress().equals("0.0.0.0")) {
    localAddress = InetAddress.getLocalHost();
return localAddress.getHostAddress();
StringgetHostAddress()
get Host Address
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    final NetworkInterface current = interfaces.nextElement();
    if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
        continue;
    final Enumeration<InetAddress> addresses = current.getInetAddresses();
    while (addresses.hasMoreElements()) {
...
short[]getHostAddress()
Gets the host address, works around byte[] getAddress() looking negative
short[] address = new short[4];
try {
    InetAddress netAddress = InetAddress.getLocalHost();
    String ipAddress = netAddress.getHostAddress();
    int beginIndex = 0;
    int endIndex = ipAddress.indexOf('.');
    address[0] = (short) Integer.parseInt(ipAddress.substring(beginIndex, endIndex));
    beginIndex = endIndex + 1;
...
StringgetHostAddress()
get Host Address
List<String> addresses = new LinkedList<>();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface n = (NetworkInterface) interfaces.nextElement();
    Enumeration<InetAddress> ee = n.getInetAddresses();
    while (ee.hasMoreElements()) {
        InetAddress i = (InetAddress) ee.nextElement();
        if (i instanceof Inet4Address) {
...
InetAddressgetHostAddress()
Get the host IP address Use env(IFACE) to select an interface or env(IP) to select a specific address to prefer IPv6 use: java.net.preferIPv6Stack=true
boolean ipv6 = false;
String pv4 = System.getProperty("java.net.preferIPv4Stack");
String pv6 = System.getProperty("java.net.preferIPv6Stack");
if (pv4 != null && pv4.equals("false")) {
    ipv6 = true;
if (pv6 != null && pv6.equals("true")) {
    ipv6 = true;
...