Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Exp 4 PDF

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

EXPERIMENT-4

Objective: Write a code simulating PING and TRACEROUTE commands

Theory:
In computer networks, data is sent in small blocks known as packets. Each packet is transmitted
individually and may also follow a different route to reach the destination. Once all these packets
of the original message reach the destination, they are re-assembled to form the original message.
But, sometimes, it may happen that the webserver is down, network congestion, or some other
technical glitch is there, that may prevent the message from reaching the destination. To diagnose
such congestions and network failures, we use two common programs namely Ping and
Traceroute.
Ping – It is a utility that helps one to check if a particular IP address is accessible or not. Ping
works by sending a packet to the specified address and waits for the reply. It also measures round
trip time and reports errors.
Ping is also used in checking if the computers on a local network are active. For this, the user has
to go in command prompt and type: ping 127.0.0.1, and if the address is active, the ping would
return a message like this :
Pinging 127.0.0.1 with 32 bytes of data
Reply from 127.0.0.1: bytes=32 time<10ms TTL=32
Reply from 127.0.0.1: bytes=32 time<10ms TTL=32
Reply from 127.0.0.1: bytes=32 time<10ms TTL=32
Reply from 127.0.0.1: bytes=32 time<10ms TTL=32
The IP address 127.0.0.1 is the address of the local host and would receive a ping reply even if the
sender is not connected to the internet.
Traceroute – It is a utility that traces a packet from your computer to the host, and will also show
the number of steps (hops) required to reach there, along with the time by each step. Traceroute
works by sending the packets of data with low survival time (Time to Live – TTL) which specifies
how many steps (hops) can the packet survive before it is returned. When a packet can’t reach the
final destination and expires at an intermediate step, that node returns the packet and identifies
itself. So, by increasing the TTL gradually, Traceroute is able to identify the intermediate hosts. If
any of the hops come back with “Request timed out”, it denotes network congestion and a reason
for slow loading Web pages and dropped connections.

The main difference between Ping and Traceroute is that Ping is a quick and easy utility to tell if
the specified server is reachable and how long will it take to send and receive data from the server
whereas Traceroute finds the exact route taken to reach the server and time taken by each step
(hop).

Algorithm:

Server

Step1: Start the program.


Step2: Import necessary packages.
Step3: Initialize the ping server with both sockets as null value.
Step4: Start the server socket.
Step5: At the client give the IP address of the server(by using ifconfig command in command
prompt).
Step6: The client program is then started by starting socket.
Step7: At the receiver end, the client is pinged and traced. Step8: Stop the program.

Client
Step1: Start the program.
Step2: Import necessary packages.
Step3: Initialize the ping client with both sockets as null value.
Step4: Start the socket.
Step5: Get the IP address of the server.
Step6: Ping the server.
Step7: At the receiver end, the server is pinged and traced.
Step8: Stop the program.

pingclient.java
/*…localhostport name and 5555-port number…*/
Socket s=new Socket("127.0.0.1",5555);

/*… Get an input file handle from the socket and read the input…*/
DataInputStream dis=new DataInputStream(s.getInputStream()); PrintStream out=new
PrintStream(s.getOutputStream()); while(c<4){
…returns the current time in milliseconds…*/
t1=System.currentTimeMillis();
str="Welcome to network programming
world"; out.println(str);
/*…readline() method read a line of text…*/
System.out.println(dis.readLine());
t2=System.currentTimeMillis();
System.out.println(";TTL="+(t2-t1)+"ms"); c++;

pingserver.java

/*…ServerSocket object is used to establish the communication with clients…*/


ServerSocketss=new ServerSocket(5555);

/*…accept(): Used to accept the client request…*/


Socket
s=ss.accept(); int
c=0; while(c<4) {
/*… Get an input file handle from the socket and read the input…*/
DataInputStream dis=new DataInputStream(s.getInputStream());
PrintStream out=new PrintStream(s.getOutputStream());
/*…readline() method read a line of text…*/
String str=dis.readLine();
out.println("Reply from"+InetAddress.getLocalHost()+";Length"+str.length()); c++;
Sample Output:

You might also like