Java Utililty Methods InetAddress from

List of utility methods to do InetAddress from

Description

The list of methods to do InetAddress from are organized into topic(s).

Method

InetAddressinetAddress(String host)
inet Address
try {
    return InetAddress.getByName(host);
} catch (UnknownHostException e) {
    throw new IllegalArgumentException(e);
InetAddressint2InetAddress(int val)
Converts 32 bits int to IPv4 InetAddress.
byte[] value = { (byte) ((val & 0xFF000000) >>> 24), (byte) ((val & 0X00FF0000) >>> 16),
        (byte) ((val & 0x0000FF00) >>> 8), (byte) ((val & 0x000000FF)) };
try {
    return InetAddress.getByAddress(value);
} catch (UnknownHostException e) {
    return null;
InetAddressIntegerToInetAddress(int ipAddress)
Integer To Inet Address
byte[] array = new byte[4];
array[0] = (byte) ((ipAddress >> 24) & 0xFF);
array[1] = (byte) ((ipAddress >> 16) & 0xFF);
array[2] = (byte) ((ipAddress >> 8) & 0xFF);
array[3] = (byte) (ipAddress & 0xFF);
return InetAddress.getByAddress(array);
InetAddressintToInetAddress(int i)
int To Inet Address
return InetAddress.getByAddress(intToAddress(i));
longip2Long(InetAddress ip)
Converts an IP v4 number to a 64bit (long) number, according to the lobby protocol standard.
long res;
byte[] addr = ip.getAddress();
final long f1 = (long) addr[0] << 24;
final long f2 = (long) addr[1] << 16;
final long f3 = (long) addr[2] << 8;
final long f4 = (long) addr[3];
res = f1 + f2 + f3 + f4;
return res;
...
byte[]ipToBytesByInetAddress(String ip)
ip To Bytes By Inet Address
return InetAddress.getByName(ip).getAddress();
longipToLong(InetAddress ip)
ip To Long
byte[] octets = ip.getAddress();
long result = 0;
for (byte octet : octets) {
    result <<= 8;
    result |= octet & 0xff;
return result;
InetAddresslong2InetAddress(long val)
Converts 32 bits int packaged into a 64bits long to IPv4 InetAddress.
if ((val < 0) || (val > 0xFFFFFFFFL)) {
return int2InetAddress((int) val);
InetAddresstoInetAddress(String host)
Converts a hostname or IP address into a InetAddress.
InetAddress addr = null;
if (host.length() > 0) {
    try {
        addr = InetAddress.getByName(host);
        int periodCount = 0;
        if (isMaybeIpAddress(host, periodCount)) {
            String canHostNm = addr.getCanonicalHostName();
            String hostNm = addr.getHostName();
...