How to Create a Ping Program in Java

Save

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 java.net.*;" 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

    (InetAddress.getByName(host),

                        port);

    } 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();

    else

    result = "Timed out";

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

    shown = true;

    }

    }

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

References

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

Comments

You May Also Like

  • How To Write Java Programs

    Java is an object-oriented programming language originally developed and released by Sun Microsystems in the mid-1990s. Java was developed from the start...

  • How to Test My Computer for Java

    Java is a computer programming language that allows programmers to write instructions using codes in the English language, as opposed to numeric...

  • How to Ping in Javascript

    There are many reasons why you might want to ping an external domain from a script in your web page. You may...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

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