Monday, 21st June 2010
The new frontier for learning Java

Ping a server jdk1.5

From WikiJava

Jump to: navigation, search


Since JDK1.5, java.net.InetAddress.isReachable(int) can be used to check if a server is reachable or not.

This example shows how to perform a pseudo ping in Java

Contents

the article

isReachable() will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host. But most Internet sites have disabled the service or blocked the requests (except some university web sites like the example above) .

ReachableTest.java

import java.io.*;
import java.net.*;
 
public class ReachableTest {
 public static void main(String args[]) {
     try {
       InetAddress address = InetAddress.getByName("web.mit.edu");
       System.out.println("Name: " + address.getHostName());
       System.out.println("Addr: " + address.getHostAddress());
       System.out.println("Reach: " + address.isReachable(3000));
     }
     catch (UnknownHostException e) {
       System.err.println("Unable to lookup web.mit.edu");
     }
     catch (IOException e) {
       System.err.println("Unable to reach web.mit.edu");
     }
   }
}

See Also

Ping a server, Real's Java How To

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: