Q. Write a Java program to implement the ping command.


Ans.

import java.io.*;
import java.net.*;

public class ping
{
 public static void main (String args[])
 {
  System.out.println("Pinging Status");
  String ipAddress="www.google.com";
  try
   {
    InetAddress inet=InetAddress.getByName("www.google.com");
    System.out.println("Sending the Ping request to"+ipAddress);
    boolean status=inet.isReachable(5000);
    if(status)
    System.out.println("Status: Destination host is reachable");
    else
    System.out.println("Status: Destination host is unreachable");
    }
   catch(IOException e)
   {
    System.out.println("Host does not exist");
    }
  }
}

Output:

javac ping.java (//enter)
java ping (//enter)
ping status
Sending ping request to www.google.com
Status: Destination host is reachable 

socket programming, socket meaning
Free image Downloaded from unsplash.com