Tuesday 30 March 2021

Write a code simulating PING and TRACEROUTE commands

 

Write a code simulating PING command  

 Aim:

 To Write the java program for simulating ping command

 Algorithm

Step 1: start the program.

Step 2: Include necessary package in java.

Step 3: To create a process object p to implement the ping command.

Step 4: declare one BufferedReader stream class object.

Step 5: Get thedetails of the server

         5.1: length of the IP address.

         5.2: time required to get the details.

         5.3: send packets , receive packets and lost packets.

         5.4: minimum ,maximum and average times.

Step 6: print the results.

Step 7:Stop the program.

 

Program:

 

import java.io.*;

import java.net.*;

class pingserver

{

public static void main(String args[])

{

try

{

String str;

System.out.print(" Enter the IP Address to be Ping : ");

BufferedReader buf1=new BufferedReader(new

InputStreamReader(System.in));

String ip=buf1.readLine();

Runtime H=Runtime.getRuntime();

Process p=H.exec("ping " + ip);

InputStream in=p.getInputStream();

BufferedReader buf2=new BufferedReader(new

InputStreamReader(in));

while((str=buf2.readLine())!=null)

{

System.out.println(" " + str);

}

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

}

 

Output:

Enter the IP address to the ping:192.168.0.1

 

Pinging 192.168.0.1: with bytes of data =32

 

Reply from 192.168.0.11:bytes=32 time<1ms TTL =128

Reply from 192.168.0.11:bytes=32 time<1ms TTL =128

Reply from 192.168.0.11:bytes=32 time<1ms TTL =128

Reply from 192.168.0.11:bytes=32 time<1ms TTL =128

 

Ping statistics for 192.168.0.1

Packets: sent=4,received=4,lost=0(0% loss),approximate round trip time in milli seconds:

Minimum=1

ms,maximum=4ms,average=2ms

 

TRACEROUTE commands-

import java.io.BufferedReader;

import java.io.InputStreamReader;


public class traceroutecmd

{

     public static void runSystemCommand(String command)

     {

          try

          {

              Process p = Runtime.getRuntime().exec(command);

              BufferedReader inputStream = new BufferedReader(

                        new InputStreamReader(p.getInputStream()));


              String s = "";

              while ((s = inputStream.readLine()) != null)

                   System.out.println(s);

          }

          catch (Exception e)

          {

          }

     }


     public static void main(String[] args)

     {  

          // String ip = "www.google.co.in";

          // String ip = "127.0.0.1";

          String ip = "www.cp-algorithms.com";

          runSystemCommand("tracert " + ip);

     }

}

No comments:

Post a Comment

The Future of Web Development: Why Next.js is Going Viral

  Are you ready to level up your web development game? Look no further than Next.js, the latest sensation in the world of web development th...