Java InetAddress Create getInetAddress(String host)

Here you can find the source of getInetAddress(String host)

Description

Computes a long value for the given hostname (or dot notated IP address).

License

Open Source License

Parameter

Parameter Description
host the hostname or dot notated IP address to get the long IP address for

Return

the long IP address for the host or -1 on error

Declaration

public static final long getInetAddress(String host) 

Method Source Code

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

public class Main {
    /**/*from   www.jav a 2s  .  c o m*/
     * Computes a long value for the given hostname (or dot notated IP
     * address). If <code>host</code> == null the address for the
     * local host will be returned.
     *
     * @param  host  the hostname or dot notated IP address to get
     * the long IP address for
     * @return the long IP address for the host or -1 on error
     */
    public static final long getInetAddress(String host) {

        java.net.InetAddress inaddr;
        try {

            if (host == null) {
                inaddr = java.net.InetAddress.getLocalHost();
            } else {
                inaddr = java.net.InetAddress.getByName(host);
            }

        } catch (java.net.UnknownHostException une) {
            return (-1);
        }

        byte ip[] = inaddr.getAddress();
        int numBytes = ip.length;
        long addr = 0;
        for (int i = 0; i < numBytes; i++) {
            long b = (ip[i] < 0 ? ip[i] + 256 : ip[i]);
            addr += (b << (8 * (numBytes - i - 1)));
        }

        return (addr);
    }
}

Related

  1. getInetAddress(final int[] octets, final int offset, final int length)
  2. getInetAddress(final String addressOrName)
  3. getInetAddress(long ip)
  4. getInetAddress(Socket socket)
  5. getInetAddress(String dottedNotation)
  6. getInetAddress(String hostName)
  7. getInetAddress(String name)
  8. getInetAddress(String str)
  9. getInetAddressByName(String name)