InetAddress isReachable(NetworkInterface, int, int) in Java

isReachable(NetworkInterface, int, int): This method is available in the java.net.InetAddress class of Java.

Syntax:

boolean java.net.InetAddress.isReachable(NetworkInterface netif, int ttl, int timeout) throws IOException

This method takes three arguments. This method tests whether that address is reachable.

Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible.

Parameters: Three parameters are required for this method.

netif: the NetworkInterface through which the test will be done, or null for any interface.

ttl: the maximum numbers of hops to try or 0 for thedefault.

timeout: the time, in milliseconds, before the call aborts.

Returns:boolean indicating if the address is reachable.

Throws:

1. IllegalArgumentException - if either timeout or ttl are negative.

2. IOException - if a network error occurs.

Approach 1: When no exception

Java

package com.InetAddress;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class InetAddressisReachable2 {
    public static void main(String[] args) throws IOException {

        byte addr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 };
        InetAddress inetAddress = InetAddress.getByAddress(addr);

        int timeout = 10;

        int index = 1;
        NetworkInterface netif = NetworkInterface.getByIndex(index);

        int ttl = 10;

        System.out.println(inetAddress.isReachable(netif, ttl, timeout));
    }
}

Output:

false


Approach 2: IllegalArgumentException

Java

package com.InetAddress;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class InetAddressisReachable2 {
    public static void main(String[] args) throws IOException {

        byte addr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 };
        InetAddress inetAddress = InetAddress.getByAddress(addr);

        int timeout = -10;

        int index = 1;
        NetworkInterface netif = NetworkInterface.getByIndex(index);

        int ttl = 10;

        System.out.println(inetAddress.isReachable(netif, ttl, timeout));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: timeout can't be negative at java.base/java.net.InetAddress.isReachable(InetAddress.java:546) at com.InetAddress.InetAddressisReachable2.main(InetAddressisReachable2.java:20)


Some other methods of InetAddress class

equals(Object)This method compares this object against the specified object.

getAddress()This method returns the raw IP address of this InetAddress object.

InetAddress.getAllByName(String host)This method returns an array of its IP addresses, based on the configured name service on the system.

InetAddress.getByAddress(byte[])This method returns an InetAddress object given the raw IP address.

InetAddress.getByAddress(String, byte[])This method creates an InetAddress based on the provided hostname and IP address.

InetAddress.getByName(String)This method determines the IP address of a host, given the host's name.

getCanonicalHostName()This method gets the fully qualified domain name for this IP address.

getHostAddress()This method returns the IP address string in textual presentation.

getHostName()This method gets the hostname for this IP address.

InetAddress.getLocalHost()This method returns the address of the local host. 

InetAddress.getLoopbackAddress()This method returns the loopback address.

hashCode()This method returns a hashcode for this IP address.

isAnyLocalAddress()Utility routine to check if the InetAddress is a wildcard address.

isLinkLocalAddress()Utility routine to check if the InetAddress is a link-local address.

isLoopbackAddress()Utility routine to check if the InetAddress is a loopback address.

isMCGlobal()Utility routine to check if the multicast address has global scope.

isMCLinkLocal()Utility routine to check if the multicast address has link scope.

isMCNodeLocal()Utility routine to check if the multicast address has node scope.

isMCOrgLocal()Utility routine to check if the multicast address has organization scope.

isMCSiteLocal()Utility routine to check if the multicast address has site scope.

isMulticastAddress()Utility routine to check if the InetAddress is an IP multicast address.

isReachable(int)This method tests whether that address is reachable.

isReachable(NetworkInterface, int, int)This method tests whether that address is reachable.

isSiteLocalAddress()Utility routine to check if the InetAddress is a site-local address.

toString()This method converts this IP address to a String.

No comments:

Post a Comment