CS8581 Networks Lab Manual
CS8581 Networks Lab Manual
CS8581 Networks Lab Manual
EX.NO:1 Learn to use commands like tcpdump, netstat, ifconfig, nslookup and traceroute.
Capture ping and traceroute PDUs using a network protocol analyzer and examine
AIM: To Learn to use commands like tcpdump, netstat, ifconfig, nslookup and traceroute
1. Tcpdump
The tcpdump utility allows you to capture packets that flow within your network to assist in
network troubleshooting. The following are several examples of using tcpdump with different
options. Traffic is captured based on a specified filter.
Options Description
-D Print a list of network interfaces.
Specify an interface on which to
-i
capture.
Specify the number of packets to
-c
receive.
-v, -vv, -vvv Increase the level of detail (verbosity).
-w Write captured data to a file.
-r Read captured data from a file.
Many other options and arguments can be used with tcpdump. The following are some specific
examples of the power of the tcpdump utility.
To display all traffic between two hosts (represented by variables host1 and host2):
Provide the protocol as an argument to display only traffic for a specific protocol, for example tcp,
udp, icmp, arp:
# tcpdump protocol
Page | 2
2.Netstat
Netstat is a common command line TCP/IP networking available in most versions of Windows, Linux,
UNIX and other operating systems. Netstat provides information and statistics about protocols in use
and current TCP/IP network connections. The Windows help screen (analogous to a Linux or UNIX for
netstat reads as follows:
Page | 3
3. Ifconfig
In Windows, ipconfig is a console application designed to run from the Windows command
prompt. This utility allows you to get the IP address information of a Windows computer. It also
allows some control over active TCP/IP connections. Ipconfig replaced the older winipcfg utility.
Using ipconfig
From the command prompt, type ipconfig to run the utility with default options. The output of the
default command contains the IP address, network mask, and gateway for all physical and virtual
network adapter
Examples:
Page | 4
4. Nslookup
The nslookup (which stands for name server lookup) command is a network utility program
used to obtain information about internet servers. It finds name server information for domains by
querying the Domain Name System.
Page | 5
SIMULATION OF PING COMMAND
OBJECTIVE:
To simulate ping command using java language.
Page | 6
Page | 7
SIMULATION OF TRACEROUTE COMMAND
OBJECTIVE:
To simulate Traceroute command using java language.
Page | 8
Page | 9
EX.NO 2. Write a HTTP web client program to download a web page using TCP sockets.
Aim:
To write a java program for socket for HTTP for web page upload and download .
Algorithm
Program : Client
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
Page | 10
System.out.println("Sending image to server. ");
OutputStream out = soc.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(bytes.length);
dos.write(bytes, 0, bytes.length); System.out.println("Image
sent to server. "); dos.close();
out.close();
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
soc.close();
}
soc.close();
}
}
Server
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*; import
javax.swing.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket server=null;
Socket socket;
server=new ServerSocket(4000);
System.out.println("Server Waiting for image");
socket=server.accept();
System.out.println("Client connected.");
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
System.out.println("Image Size: " + len/1024 + "KB");
byte[] data = new byte[len];
dis.readFully(data);
Page | 11
dis.close();
in.close();
InputStream ian = new ByteArrayInputStream(data);
BufferedImage bImage = ImageIO.read(ian);
JFrame f = new JFrame("Server");
ImageIcon icon = new ImageIcon(bImage);
JLabel l = new JLabel();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
}
}
Output
When you run the client code, following output screen would appear on client side.
Result:
Thus the program for creating sockets for HTTP web page upload and
download was implemented.
Page | 12
EX.NO 3 Applications using TCP sockets like:
A. Echo client and echo server
B. Chat
C File Transfer
EX.NO 3(A) Applications using TCP sockets like Echo client and Echo server
Aim
To write a java program for application using TCP Sockets Links
Algorithm
1. Start the program.
2. Get the frame size from the user
3. To create the frame based on the user request.
4. To send frames to server from the client side.
5. If your frames reach the server it will send ACK signal to client
otherwise it will send NACK signal to client.
6. Stop the program
EchoServer.java
import java.net.*;
import java.io.*;
public class EServer
{
public static void main(String args[ ])
{
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps; Socket
c=null;
try
{
s=new ServerSocket(9000);
}
catch(IOException e)
{
}
try
{
System.out.println(e);
c=s.accept();
is=new DataInputStream(c.getInputStream( ));
Page | 13
ps=new PrintStream(c.getOutputStream( ));
while(true)
{
line=is.readLine();
ps.println(line);
}}
catch(IOException e)
{
System.out.println(e);
}
}}
EClient.java
import java.net.*;
import java.io.*;
public class EClient
{
public static void main(String arg[])
{
Socket c=null; String line;
DataInputStream is, is1;
PrintStream os;
try
{
InetAddress ia = InetAddress.getLocalHost( );
c=new Socket(ia,9000);
}
catch(IOException e)
{
}
try
{
System.out.println(e);
os=new PrintStream(c.getOutputStream( ));
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream( ));
while(true)
{
System.out.println("Client:");
line=is.readLine( );
os.println(line);
System.out.println("Server:" + is1.readLine( ));
}}
catch(IOException e)
{
System.out.println("Socket Closed!");
Page | 14
}}}
Output
Server
Client
Result:
Thus the java program to concurrently communicate using TCP Sockets was executed
successfully.
Page | 15
B. CHAT
AIM:
To implement a CHAT application, where the Client establishes a connection with the
Server. The Client and Server can send as well as receive messages at the same time. Both the
Client and Server exchange messages.
Concept:
1. It uses TCP socket communication .We have a server as well as a client.
2. Both can be run in the same machine or different machines. If both are running in the machine,
the address to be given at the client side is local host address.
3. If both are running in different machines, then in the client side we need to specify the ip
address of machine in which server application is running.
UDPserver.java
import java.io.*;
import java.net.*;
class UDPserver
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static int clientport=789,serverport=790;
public static void main(String args[ ])throws Exception
{
ds=new DatagramSocket(clientport);
System.out.println("press ctrl+c to quit the program");
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
InetAddress ia=InetAddress.geyLocalHost( );
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData( ),0,p.getLength( ));
System.out.println("Client:" + psx);
System.out.println("Server:");
String str=dis.readLine( );
if(str.equals("end"))
break;
buffer=str.getBytes( );
ds.send(new
DatagramPacket(buffer,str.length( ),ia,serverport));
}
Page | 16
}
}
UDPclient.java
import java io.*;
import java.net.*;
class UDPclient
{
public static DatagramSocket ds;
public static int clientport=789,serverport=790;
public static void main(String args[ ])throws Exception
{
byte buffer[]=new byte[1024];
ds=new DatagramSocket(serverport);
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
System.out.println("server waiting");
InetAddress ia=InetAddress.getLocalHost( );
while(true)
{
System.out.println("Client:");
String str=dis.readLine();
if(str.equals("end"))
break;
buffer=str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData( ),0,p.getLength( ));
System.out.println("Server:" + psx);
}
}
}
Page | 17
OUTPUT:
Server
Client
Page | 18
C.File Transfer
AIM:
Algorithm
Server
Client
Page | 19
Program:
File Client
import java.io.*;
import java.net.*;
import java.util.*;
class Clientfile
{ public static void main(String args[])
{ Try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the file name:");
String str=in.readLine();
dout.writeBytes(str+'\n');
System.out.println("Enter the new file name:");
String str2=in.readLine();
String str1,ss;
FileWriter f=new
FileWriter(str2); char buffer[];
while(true)
{
str1=din.readLine();
if(str1.equals("-1")) break;
System.out.println(str1);
buffer=new char[str1.length()];
str1.getChars(0,str1.length(),buffer,0);
f.write(buffer);
}
f.close();
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}}}
Page | 20
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Serverfile
{
public static void main(String args[])
{
Try
{
ServerSocket obj=new ServerSocket(139);
while(true)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new
DataOutputStream(obj1.getOutputStream());
String str=din.readLine();
FileReader f=new FileReader(str);
BufferedReader b=new BufferedReader(f);
String s;
while((s=b.readLine())!=null)
{
System.out.println(s);
dout.writeBytes(s+'\n');
}
f.close();
dout.writeBytes("-1\n");
}
}
catch(Exception e)
{ System.out.println(e);
}
}}
Page | 21
Output
File content Computer
networks jhfcgsauf
jbsdava
jbvuesagv
client
Enter the file name:
sample.txt
server
Computer networks
jhfcgsauf
jbsdava
jbvuesagv
client
Enter the new file name:
net.txt
Computer networks
jhfcgsauf
jbsdava jbvuesagv
Destination file
Computer networks
jhfcgsauf
jbsdava
jbvuesagv
RESULT
Thus the java program file transfer application using TCP Sockets was executed
Page | 22
EX.NO 4. Implementation of DNS using UDP sockets.
Aim :
Algorithm:
5. If your frames reach the server it will send ACK signal to client
import java.io.*;
import java.net.*;
public class udpdnsserver
{
private static int indexOf(String[] array, String str)
{
str = str.trim();
for (int i=0; i < array.length; i++)
{
if (array[i].equals(str))
return i;
}
return -1;
}
public static void main(String arg[])throws IOException
{
String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com", "facebook.com"};
String[] ip = {"68.180.206.184", "209.85.148.19","80.168.92.140", "69.63.189.16"};
System.out.println("Press Ctrl + C to Quit");
while (true)
Page | 23
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
DatagramPacket recvpack = new DatagramPacket(receivedata, receivedata.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress ipaddress =
recvpack.getAddress(); int port =
recvpack.getPort();
String capsent;
System.out.println("Request for host " + sen);
if(indexOf (hosts, sen) != -1)
capsent = ip[indexOf (hosts, sen)];
else
capsent = "Host Not Found";
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket (senddata, senddata.length,ipaddress,port);
serversocket.send(pack);
serversocket.close();
}
}
}
Page | 24
//UDP DNS Client
import java.io.*;
import java.net.*;
public class udpdnsclient
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); DatagramSocket clientsocket = new
DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int portaddr = 1362;
System.out.print("Enter the hostname :
"); String sentence = br.readLine();
Senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length,
ipaddress,portaddr);
clientsocket.send(pack);
Page | 25
OUTPUT
Server
javac udpdnsserver.java
java udpdnsserver
Press Ctrl + C to Quit Request for host yahoo.com
Request for host cricinfo.com
Request for host youtube.com
Client
javac udpdnsclient.java
java udpdnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184
java udpdnsclient
Enter the hostname : cricinfo.com
IP Address: 80.168.92.140
java udpdnsclient
Enter the hostname : youtube.com
Result:
Page | 26
EX.NO 5(a) Program for Address Resolution Protocol (ARP) using TCP
Aim:
To implement a java program for ARP protocols using TCP
ALGORITHM:
Client
Server
Program
Client:
import java.io.*;
import
java.net.*;
import
java.util.*; class
Clientarp
{
public static void main(String args[])
{
try
{
Page | 27
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in)); Socket clsct=new Socket("127.0.0.1",139);
DataInputStream din=new
DataInputStream(clsct.getInputStream()); DataOutputStream
dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the Logical address(IP):");
String
str1=in.readLine();
dout.writeBytes(str1+'
\n'); String
str=din.readLine();
System.out.println("The Physical Address is: "+str);
clsct.close();
}
catch (Exception e)
{
System.out.printl
n(e);
} }
}
Server:
import
java.io.*;
import
java.net.*;
import
java.util.*;
class
Serverarp
{
public static void main(String args[])
{
try
Page | 28
{
ServerSocket obj=new
ServerSocket(139); Socket
obj1=obj.accept();
while(true)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new
DataOutputStream(obj1.getOutputStream()); String
str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(str.equals(ip[i]))
{
dout.writeBytes(mac[i]+'\n');
break;
}}
obj.close();
}}
catch(Exception e)
{
System.out.println(e);
}}
}
Output:
E:\networks>java Serverarp
E:\networks>java Clientarp
Enter the Logical address(IP):
165.165.80.80
The Physical Address is: 6A:08:AA:C2
Result:
Thus the ARP protocol using TCP Sockets program was executed.
Page | 29
EX.NO 5(b) Program for Reverse Address Resolution Protocol (RARP) using UDP
Aim:
To write a java program for simulating RARP protocols using UDP
ALGORITHM
Client
Server
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrarp12
{
public static void main(String args[])
{
try
{
DatagramSocket client=new DatagramSocket(); InetAddress
addr=InetAddress.getByName("127.0.0.1");
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Page | 30
System.out.println("Enter the Physical address (MAC):")
client.receive(receiver);
String s=new String(receiver.getData());
System.out.println("The Logical Address is(IP): "+s.trim());
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrarp12
{
public static void main(String args[])
{
try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new String(receiver.getData());
Page | 31
String s=str.trim();
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(s.equals(mac[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender=newDatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;
}
}
break;
}
}
catch(Exception e)
{
System.out.println(e);
} }
}
Output:
I:\ex>java Serverrarp12
I:\ex>java Clientrarp12
Enter the Physical address
(MAC): 6A:08:AA:C2
The Logical Address is(IP): 165.165.80.80
Result :
Thus the RARP program using UDP was executed.
Page | 32
Page | 33
Figure 2.1 shows the basic architecture of NS2. NS2 provides users with an executable
command ns which takes on input argument, the name of a Tcl simulation scripting file. Users are
feeding the name of a Tcl simulation script (which sets up a simulation) as an input argument of an
NS2 executable command ns.
In most cases, a simulation trace file is created, and is used to plot graph and/or to create
animation. NS2 consists of two key languages: C++ and Object-oriented Tool Command Language
(OTcl). While the C++ defines the internal mechanism (i.e., a backend) of the simulation objects, the
OTcl sets up simulation by assembling and configuring the objects as well as scheduling discrete
events (i.e., a frontend).
Page | 34
Page | 35
Page | 36
Congestion Control
AIM:
Program:
proc finish { }
global ns nr nf
$ns flush-trace
close $nf
close $nr
exit 0
$ns run
Output:
RESULT:
Thus the Congestion Control Algorithm was Simulated and studied.
Page | 39
Page | 40
#Create a simulator object
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exit 0
Page | 41
#Give node position (for NAM)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
$ns run
Page | 43
Output:
Result:
Thus the behaviour of TCP was observed and the basic terminologies of TCP transmission
were understood.
Page | 44
Page | 45
#Create a simulator object
Page | 46
# Create a CBR traffic source and attach it to udp0
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exit 0
Page | 47
}
$ns run
Page | 48
OUTPUT:
Result:
Thus the behaviour of TCP was observed and the basic terminologies of TCP transmission
were understood.
Page | 49
SIMULATION OF DISTANCE VECTOR ROUTING ALGORITHM
AIM:
To simulate and study the Distance Vector routing algorithm using simulation.
SOFTWARE REQUIRED: NS-2
THEORY:
Distance Vector Routing is one of the routing algorithm in a Wide Area Network for computing
shortest path between source and destination. The Router is one main devices used in a wide area network.
The main task of the router is Routing. It forms the routing table and delivers the packets depending upon the
routes in the tableeither directly or via an intermediate devices.
Each router initially has information about its all neighbors. Then this information will be shared among nodes.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute nam on trace file.
4. Create n number of nodes using for loop
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
9. Choose distance vector routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
Page | 50
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
Page | 52
Output:
RESULT:
Thus the Distance vector Routing Algorithm was Simulated and studied.
Page | 53
SIMULATION OF LINK STATE ROUTING ALGORITHM
AIM:
To simulate and study the link state routing algorithm using simulation.
THEORY:
In link state routing, each router shares its knowledge of its neighborhood with every other router in
the internet work.
(i) Knowledge about Neighborhood: Instead of sending its entire routing table a router sends info about its
neighborhood only.
(ii) To all Routers: each router sends this information to every other router on the internet work not just to its
neighbor .It does so by a process called flooding.
(iii)Information sharing when there is a change: Each router sends out information about the neighbors when
there is change.
PROCEDURE:
The Dijkstra algorithm follows four steps to discover what is called the shortest path tree(routing
table) for each router:
The algorithm begins to build the tree by identifying its roots. The root router’s trees the router itself. The
algorithm then attaches all nodes that can be reached from the root. The algorithm compares the tree’s
temporary arcs and identifies the arc with the lowest cumulative cost. This arc and the node to which it
connects are now a permanent part of the shortest path tree.
The algorithm examines the database and identifies every node that can be reached from its chosen node.
These nodes and their arcs are added temporarily to the tree. The last two steps are repeated until every node
in the network has become a permanent part of the tree.
ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace file, and execute nam on trace file.
4. Create n number of nodes using for loop
5. Create duplex links between the nodes
6. Setup UDP Connection between n(0) and n(5)
7. Setup another UDP connection between n(1) and n(5)
8. Apply CBR Traffic over both UDP connections
9. Choose Link state routing protocol to transmit data from sender to receiver.
10. Schedule events and run the program.
Page | 54
Coding:
Page | 55
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
Output:
RESULT:
Thus the Link State Routing Algorithm was Simulated and studied.
Page | 56
SIMULATION OF DISTANCE VECTOR ROUTING PROTOCOL USING NS2
OBJECTIVE
Acquire tcl scripting skills to simulate distance vector routing protocol using NS2 simulator.
OUTCOME
Students become expertise to write tcl script to simulate distance vector routing protocol using
NS2 simulator.
PROGRAM
set ns [new Simulator]
set nf [open out.nam w]
$ns namtrace-all $nf
set tr [open out.tr w]
$ns trace-all $tr
proc finish {} {
global nf ns tr
$ns flush-trace
close $tr
exec nam out.nam &
exit 0
}
$ns run
OUTPUT:
CONCLUSION :
Thus tcl programs were executed to simulate the performance of distance vector routing protocol
using ns2.
Page | 58
PERFORMANCE ANALYSIS OF ROUTING PROTOCOL USING NS2
AIM:
To simulate Distance Vector Multicast Routing Protocol in NS2.
PROGRAM:
#define options
set val(chan) Channel/WirelessChannel ;
set val(prop) Propogation/TwoRayGround ;
set val(netif) Phy/WirelessPhy ;
set val(mac) Mac/802_11 ;
set val(ifq) Queue/DropTail/PriQueue ;
set val(ll) LL ;
set val(ant) Antenna/OmniAntenna ;
set val(ifqlen) 50 ;
set val(nn) 3 ;
set val(rp) DSDV ;
set val(x) 500;
set val(y) 400;
set val(stop) 150;
create-god $val(nn)
#generation of movements
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
$ns run
Page | 61
OUTPUT:
RESULT:
Thus Distance Vector Multicast Routing Protocol is simulated in NS2.
Page | 62
SIMULATION OF ERROR CORRECTION CODE ( CRC )
Page | 63
Page | 64
Page | 65
Page | 66
Page | 67