a isreachable method has been added to the InetAddress class in j2se5.0. You can use this method to detect if the host is connected . This method has two overloaded forms, which are defined as follows:
public boolean isreachable (int-Timeout) throws IOException public boolean isreachable (NetworkInterface netif, int ttl, int Timeout) throws IOException
The first overloaded form has a timeout parameter, which allows you to set the connection timeout (in milliseconds). The second overloaded form has more than two parameters: Netif and TTL. The Netif parameter allows you to use a NetworkInterface object to determine which network interface the client uses to test the connectivity of the host. TTL refers to the maximum number of connection hops in the test connectivity process (the maximum routing count from the client to the remote host is the maximum connection metric, a route is called a hop, and the interface metric can be set at the bottom of the Advanced TCP/IP Settings dialog box in a Windows network connection). If the maximum connection metric is reached and the remote host is not found, the Isreachable method considers that the client and the remote host are not connected.
The Isreachable method is to determine whether the client and server are connected by connecting to the Echo port of the host. However, using this method on the Internet may not be able to connect to remote hosts (in fact, remote hosts can be connected) because of factors such as arson, so isreachable is not reliable on the internet. But we can apply the Isreachable method to the local area network .
Java network programming from getting started to mastering (12): Using the Isreachable method to detect if a host can be connected