How to Create a Ping Program in Java

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Create a Ping Program in Java
Java language includes functions for testing Internet connections. (Chad Baker/Photodisc/Getty Images)

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.

Skill level:


  1. 1

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

  2. 2

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

  3. 3

    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 connect Start;
    long connect Finish = 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 (connect Finish != 0)
        result = Long.toString(connect Finish -

    connect Start) + "ms";

        else if (failure != null)
        result = failure.toString();
        result = "Timed out";
        System.out.println(address + " : " + result);
        shown = true;
  4. 4

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

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.