Professional Documents
Culture Documents
5(a)
Date : 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
Result:
Aim:
To Write the java program for simulating Traceroute command
Algorithm
Program
import java.io.*;
import java.net.*;
import java.lang.*;
class Traceroute
{
public static void main(String args[]){
BufferedReader in;
try{
Runtime r = Runtime.getRuntime();
Process p = r.exec("tracert www.google.com");
String line;
if(p==null)
System.out.println("could not connect");
while((line=in.readLine())!=null){
System.out.println(line);
//in.close();
}
}catch(IOException e){
System.out.println(e.toString());
}
}
}
Output
E:\CSE\JAVA>javac tracert.java
E:\CSE\JAVA>java Traceroute
Tracing route to www.google.com [172.217.160.132]
over a maximum of 30 hops:
1 <1 ms 1 ms 1 ms 192.168.8.1
2 3 ms 5 ms 3 ms 103.76.190.33
3 7 ms 6 ms 7 ms nsg-static-25.50.76.182-airtel.com [182.76.50.25]
4 7 ms 8 ms 7 ms 182.79.198.24
5 6 ms 7 ms 7 ms 72.14.211.198
6 8 ms 9 ms 7 ms 209.85.250.202
7 8 ms 8 ms 8 ms 209.85.241.197
8 12 ms 7 ms 14 ms maa03s29-in-f4.1e100.net [172.217.160.132]
Trace complete.
Result: