How to Create a Ping Program in Java


Ping is a software program that checks Internet connections. It sends a command to the specified Web address to see if a remote computer responds to the test signal. Java programming language includes functions that allow you to add the ping program to the user application. The Java program can check the connection to a Web server if the application uses a remote database as its data source. By creating a ping program in Java applications, you can regularly monitor the availability of Web databases.

  • Click "Start" in Windows and then click "Java JDK."

  • Click "File," select "Open" and then double-click on the Java program in which you want to add a ping program.

  • Add the following code after "import*;" in your program:

    public class Ping {

    // the default port

    static int PORT = 80;

    // representation of a ping target

    static class Target {

    InetSocketAddress address;

    SocketChannel channel;

    Exception failure;

    long connectStart;

    long connectFinish = 0;

    boolean shown = false;

    Target(String host) {

    try {

    address = new InetSocketAddress



    } catch (IOException x) {

    failure = x;



    // the result of a test command

    void show() {

    String result;

    if (connectFinish != 0)

    result = Long.toString(connectFinish -

    connectStart) + "ms";

        else if (failure != null)

    result = failure.toString();


    result = "Timed out";

    System.out.println(address + " : " + result);

    shown = true;



  • Click "File" and "Save" to add the ping program to your Java application.


  • Photo Credit Chad Baker/Photodisc/Getty Images
Promoted By Zergnet


Related Searches

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!