Java Utililty Methods InetAddress Check

List of utility methods to do InetAddress Check

Description

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

Method

booleanisLocalAddress(InetAddress address)
Cheke whether the given address is local address or not.
if (address == null) {
    return false;
try {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
...
booleanisLocalIP(final InetAddress ip1, final InetAddress ip2, final int mask)
is Local IP
byte[] x = ip1.getAddress();
byte[] y = ip2.getAddress();
if (x == y) {
    return true;
if (x == null || y == null) {
    return false;
if (x.length != y.length) {
    return false;
final int bits = mask & 7;
final int bytes = mask >>> 3;
for (int i = 0; i < bytes; i++) {
    if (x[i] != y[i]) {
        return false;
final int shift = 8 - bits;
return !(bits != 0 && x[bytes] >>> shift != y[bytes] >>> shift);
booleanisLocalIpAddress(final InetAddress ipAddress)
is Local Ip Address
return ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && !isV6IpAddress(ipAddress);
booleanisLoopbackAddress(InetAddress address)
is Loopback Address
return isLoopbackAddress(address.getAddress());
booleanisLoopbackIp(InetAddress addr)
See if given address is loopback or not
boolean result = false;
if (addr instanceof Inet4Address) {
    result = isLoopbackIp((Inet4Address) addr);
} else if (addr instanceof Inet6Address) {
    result = isLoopbackIp((Inet6Address) addr);
return result;
booleanisMulticastAddress(InetAddress ipAddr)
is Multicast Address
int firstOctet = ipAddr.getAddress()[0];
if (firstOctet < 0) {
    firstOctet += 255;
return ((firstOctet >= 224) && (firstOctet < 240));
booleanisOnNetwork(InetAddress host, InetAddress network, byte[] mask)
Given an InetAddress instance that specifies a network mask and an InetAddress instance for a specific host figure out whether the host is included in the network mask.
boolean result = true;
byte[] hostAddr = host.getAddress();
byte[] networkAddr = network.getAddress();
for (int i = 0; i < networkAddr.length; i++) {
    if ((hostAddr[i] & mask[i]) != networkAddr[i]) {
        result = false;
        break;
return result;
booleanisPortFree(int port, InetAddress addr)
is Port Free
return isTCPPortFree(port, addr) && isUDPPortFree(port, addr);
booleanisPortInUse(InetAddress address, int port, int count)
Checks to see if the given local port number is being used.
boolean inUse = isPortInUse(address, port);
while (inUse && count > 0) {
    try {
        Thread.sleep(500);
    } catch (Exception e) {
    inUse = isPortInUse(address, port);
    count--;
...
booleanisPortOpen(InetAddress address, int port)
is Port Open
Socket socket = null;
try {
    socket = new Socket();
    socket.connect(new InetSocketAddress(address, port), 1000);
    return true;
} catch (IOException e) {
    return false;
} finally {
...