Monday 7 July 2014

Ping In JAVA

What is ping ?
If you are having connectivity problems, you can use the ping command to check the destination IP address you want to reach and record the results. The ping command displays whether the destination responded and how long it took to receive a reply. If there is an error in the delivery to the destination, the ping command displays an error message.
You can use the ping command to:
  • Ping your computer (by address, not host name) to determine that TCP/IP is functioning. (Pinging your computer does not verify that your network adapter is functioning.)
  • Ping the local router to determine whether the router is running.
  • Ping beyond your local router.
JAVA Code


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Ping {
    public static void main(String[] args) throws UnknownHostException, IOException   {
    Scanner in = new Scanner(System.in);
    String ipAddress ;
    System.out.print("Enter Ip to Ping: ");
    ipAddress=in.nextLine();
    InetAddress inet = InetAddress.getByName(ipAddress);
    for(int i=1;i<201;i++){
    System.out.print(i +"- "+ "Sending Ping Request to " + ipAddress);
    System.out.println(inet.isReachable(5000) ? "  Host is reachable" : "  Host is NOT reachable");}}

}

Note: This code will ping the ip address entered by user upto 200 times.

No comments:

Post a Comment