How to Ping a Server in Java

Thanks Réal Gagnon 🙂


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");

     }

   }

}

Advertisements

11 thoughts on “How to Ping a Server in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s