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

longgetIP(InetAddress ip)
get IP
byte[] b = ip.getAddress();
return b[0] << 24L & 0xff000000L | b[1] << 16L & 0xff0000L | b[2] << 8L & 0xff00 | b[3] << 0L & 0xff;
StringgetIP(InetAddress ip)
get IP
return ip.getHostAddress().replace("/", "");
StringgetIP(InetAddress ip)
get IP
return ip.getHostAddress().replace("/", "");
longgetIPAsLong(InetAddress address)
A convenient method that accepts an IP address represented by a byte[] of size 4 and returns this as a long representation of the same IP address.
return getIPAsLong(address.getAddress());
StringgetIPAsString(InetAddress addr)
Returns a human readable IP address for the given address.
byte[] ip = addr.getAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ip.length; i++) {
    if (i != 0) {
        sb.append(".");
    sb.append(ip[i] & 0xFF);
return sb.toString();
InetAddressgetIPv4LocalNetMask(InetAddress ip, int netPrefix)
Gets network mask for the IP address and network prefix specified.
try {
    int shiftby = (1 << 31);
    for (int i = netPrefix - 1; i > 0; i--) {
        shiftby = (shiftby >> 1);
    String maskString = Integer.toString((shiftby >> 24) & 255) + "."
            + Integer.toString((shiftby >> 16) & 255) + "." + Integer.toString((shiftby >> 8) & 255) + "."
            + Integer.toString(shiftby & 255);
...
VectorgetIPv6InetAddresses(NetworkInterface networkInterface)
Retrieves a list of all IPv6 addresses assigned to one interface.
Enumeration e;
Vector result = new Vector();
e = networkInterface.getInetAddresses();
while (e.hasMoreElements()) {
    InetAddress currentAddress = (InetAddress) e.nextElement();
    if (currentAddress instanceof Inet6Address) {
        result.add(currentAddress);
return result;
InetAddressgetLocalInetAddress()
get Local Inet Address
NetworkInterface iface = null;
if (System.getProperty("os.name").compareTo("Linux") != 0)
    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e1) {
        e1.printStackTrace();
try {
...
InetAddressgetLocalInetAddress()
Returns an IP address of the device to be used for local communication on the subnet.
try {
    List<InetAddress> addresses = getIPAddresses();
    for (InetAddress a : addresses) {
        if (a instanceof Inet6Address && a.isLinkLocalAddress()) {
            return a;
    for (InetAddress a : addresses) {
...
InetAddressgetLocalInetAddress()
Retrieve the first validated local ip address(the Public and LAN ip addresses are validated).
Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();
while (enu.hasMoreElements()) {
    NetworkInterface ni = enu.nextElement();
    if (ni.isLoopback()) {
        continue;
    Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
    while (addressEnumeration.hasMoreElements()) {
...