Here you can find the source of getInetAddress(String host)
Parameter | Description |
---|---|
host | the hostname or dot notated IP address to get the long IP address for |
public static final long getInetAddress(String host)
//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); } }