Example usage for java.net InetAddress getAddress

List of usage examples for java.net InetAddress getAddress

Introduction

In this page you can find the example usage for java.net InetAddress getAddress.

Prototype

public byte[] getAddress() 

Source Link

Document

Returns the raw IP address of this InetAddress object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] ipAddr = new byte[] { 127, 0, 0, 1 };
    InetAddress addr = InetAddress.getByAddress(ipAddr);
    System.out.println(Arrays.toString(addr.getAddress()));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java-tips.org");
    byte[] ipAddr = addr.getAddress();

    // Convert to dot representation
    String ipAddrStr = "";
    for (int i = 0; i < ipAddr.length; i++) {
        if (i > 0) {
            ipAddrStr += ".";
        }//  w  w  w  .ja  va 2s. c  o  m
        ipAddrStr += ipAddr[i] & 0xFF;
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getLocalHost();

    // Get IP Address
    byte[] ipAddr = addr.getAddress();

    // Get hostname
    String hostname = addr.getHostName();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] ipAddr = new byte[] { 127, 0, 0, 1 };
    InetAddress addr = InetAddress.getByAddress("Localhost", ipAddr);
    System.out.println(Arrays.toString(addr.getAddress()));
}

From source file:DNSLookup.java

public static void main(String args[]) {
    try {//from   ww w . j a va  2  s  .  c o  m
        InetAddress host;
        if (args.length == 0) {
            host = InetAddress.getLocalHost();
        } else {
            host = InetAddress.getByName(args[0]);
        }
        System.out.println("Host:'" + host.getHostName() + "' has address: " + host.getHostAddress());
        byte bytes[] = host.getAddress();
        int fourBytes[] = new int[bytes.length];
        for (int i = 0, n = bytes.length; i < n; i++) {
            fourBytes[i] = bytes[i] & 255;
        }
        System.out.println("\t" + fourBytes[0] + "." + fourBytes[1] + "." + fourBytes[2] + "." + fourBytes[3]);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Calculates the network mask value from the address
 * /*  w  w w  .j  av  a 2 s  . c  o  m*/
 * @param address
 *            the network mask
 * @return
 */
public static int networkMaskFromInetAddress(InetAddress address) {
    byte[] addrs = address.getAddress();
    int result = 0;
    int i = 24;
    for (byte value : addrs) {
        result += value << i;
        i -= 8;
    }
    return result;
}

From source file:com.wso2telco.gsma.authenticators.IPRangeChecker.java

/**
 * Ip to long.//from  w w w  .jav  a 2  s.  c  o m
 *
 * @param ip the ip
 * @return the long
 */
public static long ipToLong(InetAddress ip) {
    byte[] octets = ip.getAddress();
    long result = 0;
    for (byte octet : octets) {
        result <<= 8;
        result |= octet & 0xff;
    }
    return result;
}

From source file:Main.java

public static int inetAddressToInt(InetAddress inetaddress) throws IllegalArgumentException {
    byte[] abyte0 = inetaddress.getAddress();
    if (abyte0.length == 4) {
        return ((((abyte0[3] & MotionEventCompat.ACTION_MASK) << 24)
                | ((abyte0[2] & MotionEventCompat.ACTION_MASK) << 16))
                | ((abyte0[1] & MotionEventCompat.ACTION_MASK) << 8))
                | (abyte0[0] & MotionEventCompat.ACTION_MASK);
    }/*w  w w.j a v a 2 s .c o m*/
    throw new IllegalArgumentException("Not an IPv4 address");
}

From source file:org.apache.synapse.commons.throttle.module.utils.Utils.java

private static long ipToLong(InetAddress ip) {
    byte[] octets = ip.getAddress();
    long result = 0;
    for (byte octet : octets) {
        result <<= 8;// ww w  .ja va 2s .com
        result |= octet & 0xff;
    }
    return result;
}

From source file:Main.java

/**
 * Transforms dotted IP address to long number.
 *
 * @param ipStr Dotted ip ie. "127.0.0.1"
 * @return Long number ie. "2130706433"// ww w .j  a  v  a 2  s . c  o m
 */
public static long ip2Long(String ipStr) {
    long result = 0;
    try {
        InetAddress ip = InetAddress.getByName(ipStr);
        byte[] octets = ip.getAddress();
        for (byte octet : octets) {
            result <<= 8;
            result |= octet & 0xff;
        }
    } catch (Exception e) {
    }
    return result;
}