Iii Cse CS8581 Networks Lab
Iii Cse CS8581 Networks Lab
Ex.No. 1 COMMANDS
Aim:
To use commands like tcpdump, netstat, ifconfig, nslookup and traceroute. Capture ping and
traceroute PDUs using a network protocol analyzer and examine.
1. Tcpdump
Tcpdump is a command line utility that allows you to capture and analyze network traffic going
through your system.
Procedure
$ which tcpdump
/usr/sbin/tcpdump
$ su
CS8581_Networks_Lab
1
4931_Grace College of Engineering, Thoothukudi
Filtering packets
To filter packets based on protocol, specifying the protocol in the command line. For example, capture
ICMP packets only by using this command:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
06:15:07.800786 IP localhost.localdomain > ec2-54-204-39-132.compute-1.amazonaws.com: ICMP
echo request, id 8180, seq 13, length 64
06:15:08.063488 IP ec2-54-204-39-132.compute-1.amazonaws.com > localhost.localdomain: ICMP
echo reply, id 8180, seq 13, length 64
CS8581_Networks_Lab
2
4931_Grace College of Engineering, Thoothukudi
5 packets captured
5 packets received by filter
0 packets dropped by kernel
2. netstat
netstat (network statistics) is a command line tool for monitoring network connections both incoming
and outgoing as well as viewing routing tables, interface statistics etc.
3. ifconfig
It displays the details of a network interface card like IP address, MAC Address, and the status of a
network interface card
CS8581_Networks_Lab
3
4931_Grace College of Engineering, Thoothukudi
4. nslookup
nslookup (stands for “Name Server Lookup”) is a useful command for getting information from
DNS server. It is a network administration tool for querying the Domain Name System (DNS) to obtain
domain name or IP address mapping or any other specific DNS record.
Non-authoritative answer:
Name: annauniv.edu
Address: 103.70.60.38
Non-authoritative answer:
206.26.217.172.in-addr.arpa name = maa03s23-in-f14.1e100.net.
206.26.217.172.in-addr.arpa name = maa03s23-in-f14.1e100.net.
206.26.217.172.in-addr.arpa name = maa03s23-in-f206.1e100.net.
206.26.217.172.in-addr.arpa name = maa03s23-in-f206.1e100.net.
Non-authoritative answer:
Name: annauniv.edu
Address: 103.70.60.38
annauniv.edu text = "v=spf1 ip4:103.70.60.40 -all"
annauniv.edu mail exchanger = 0 sonic.annauniv.edu.
annauniv.edu
origin = ns.annauniv.edu
mail addr = root.annauniv.edu
serial = 20170907
refresh = 300
CS8581_Networks_Lab
4
4931_Grace College of Engineering, Thoothukudi
retry = 900
expire = 604800
minimum = 86400
annauniv.edu nameserver = ns.annauniv.edu.
Non-authoritative answer:
annauniv.edu nameserver = ns.annauniv.edu.
5. traceroute
The traceroute command is used in Linux to map the journey that a packet of information undertakes
from its source to its destination.
Usage:
traceroute [ -46dFITnreAUDV ] [ -f first_ttl ] [ -g gate,... ] [ -i device ] [ -m max_ttl ] [ -N squeries ] [ -
p port ] [ -t tos ] [ -l flow_label ] [ -w waittime ] [ -q nqueries ] [ -s src_addr ] [ -z sendwait ] [ --
fwmark=num ] host [ packetlen ]
Options:
-4 Use IPv4
-6 Use IPv6
-d --debug Enable socket level debugging
-F --dont-fragment Do not fragment packets
CS8581_Networks_Lab
5
4931_Grace College of Engineering, Thoothukudi
Capture ping and traceroute PDUs using a network protocol analyzer and examine.
Wireshark is free & Open source network packet analyzer that is used for network analysis,
troubleshooting, etc.
Wireshark is quite similar to tcpdump, the major difference between the two is that Wireshark has a
graphical interface with built-in filtering options, which make it easy to use.
To Open Wireshark
# sudo wireshark
In a konsole execute
# ping www.sudo.com
# traceroute www.google.com
CS8581_Networks_Lab
6
4931_Grace College of Engineering, Thoothukudi
Result:
Thus commands like tcpdump, netstat, ifconfig, nslookup and traceroute was used. Ping and
traceroute PDUs using a network protocol analyzer was captured and examined.
CS8581_Networks_Lab
7
4931_Grace College of Engineering, Thoothukudi
8
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
}
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}
}
Output
F:\Abarna\Lab>javac Download.java
F:\ Abarna \Lab>java Download
Downloading File From: http://tutorialspoint.com/java_dip/images/digital_image_processing.jpg
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1097
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1744
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 2048
Buffer Read of length: 1440
Buffer Read of length: 2048
Buffer Read of length: 2048
9
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Result:
Thus created a socket for HTTP in JAVA for web page download.
10
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Ex.No.3a APPLICATION USING TCP SOCKETS - ECHO CLIENT AND ECHO SERVER
Aim:
To write a program in Java to implement an applications using TCP Sockets like echo
client and echo server
Algorithm
1. Start the Program
2. In Server
a) Create a server socket and bind it to port.
b) Listen for new connection and when a connection arrives, accept it.
c) Read the data from client.
d) Echo the data back to the client.
e) Close all streams.
f) Close the server socket.
g) Stop.
3. In Client
a) Create a client socket and connect it to the server’s port number.
b) Send user data to the server.
c) Display the data echoed by the server.
d) Close the input and output streams.
e) Close the client socket.
f) Stop.
4. Stop the program
Program
Server.java
import java.net.*;
import java.lang.*;
import java.io.*;
public class Server
{
public static final int PORT = 4000;
public static void main( String args[])
11
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
{
ServerSocket sersock = null;
Socket sock = null;
try
{
sersock = new ServerSocket(PORT);
System.out.println("Server Started :"+sersock);
try
{
sock = sersock.accept();
System.out.println("Client Connected :"+ sock);
DataInputStream ins = new DataInputStream(sock.getInputStream());
System.out.println(ins.readLine());
PrintStream ios = new PrintStream(sock.getOutputStream());
ios.println("Hello from server");
ios.close();
sock.close();
}
catch(SocketException se)
{
System.out.println("Server Socket problem "+se.getMessage());
}
}
catch(Exception e)
{
System.out.println("Couldn't start " + e.getMessage()) ;
}
System.out.println(" Connection from : " + sock.getInetAddress());
}
12
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
}
Client.java
import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;
class client
{
public static void main(String args[])
{
Socket sock=null;
DataInputStream dis=null;
PrintStream ps=null;
System.out.println(" Trying to connect");
try
{
sock= new Socket(InetAddress.getLocalHost(),Server.PORT);
ps= new PrintStream(sock.getOutputStream());
ps.println(" Hi from client");
DataInputStream is = new DataInputStream(sock.getInputStream());
System.out.println(is.readLine());
}
catch(SocketException e)
{
System.out.println("SocketException " + e);
}
catch(IOException e)
{
System.out.println("IOException " + e);
}
finally
13
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
{
try
{
sock.close();
}
catch(IOException ie)
{
System.out.println(" Close Error :" + ie.getMessage());
}
}
}
}
Output
In server window:
F:\ Abarna \Lab>javac Server.java
F:\ Abarna \Lab>java Server
Server Started :ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=4000]
Client Connected :Socket[addr=/192.168.1.18,port=1815,localport=4000]
Hi from client
Connection from : /192.168.1.18
In client window:
F:\ Abarna\Lab>javac client.java
F:\ Abarna\Lab>java client
Trying to connect
Hello from server
Result:
Thus a program in Java implemented an applications using TCP Sockets like echo client
and echo server
14
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
15
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
PrintWriter toClient;
BufferedReader fromUser, fromClient;
try
{
ServerSocket Srv = new ServerSocket(4000);
System.out.print("\nServer started\n");
Socket Clt = Srv.accept();
System.out.println("Client connected");
toClient = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromClient = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg;
while(true)
{
CltMsg= fromClient.readLine();
if(CltMsg.equals("end"))
break;
else
{
System.out.println("Server : " +CltMsg);
System.out.print("Message to Client : ");
SrvMsg = fromUser.readLine();
toClient.println(SrvMsg);
}
}
System.out.println("\nClient Disconnected");
fromClient.close();
toClient.close();
fromUser.close();
Clt.close();
16
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Srv.close();
}
catch (Exception E)
{
System.out.println(E.getMessage());
}
}
}
tcpchatclient.java
import java.io.*;
import java.net.*;
class tcpchatclient
{
public static void main(String args[])throws Exception
{
Socket Clt;
PrintWriter toServer;
BufferedReader fromUser, fromServer;
try
{
Clt = new Socket(InetAddress.getLocalHost(),4000);
toServer = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromServer = new BufferedReader(new InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new InputStreamReader(System.in));
String CltMsg, SrvMsg;
System.out.println("Type \"end\" to Quit");
while (true)
{
System.out.print("Message to Server : ");
CltMsg = fromUser.readLine();
17
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
toServer.println(CltMsg);
if (CltMsg.equals("end"))
break;
SrvMsg = fromServer.readLine();
System.out.println("Client : " + SrvMsg);
}
}
catch(Exception E)
{
System.out.println(E.getMessage());
}
}
}
Output
In server window:
F:\ Abarna\ Lab>javac tcpchatserver.java
F:\ Abarna\Lab>java tcpchatserver
Server started
Client connected
Server : hai
Message to Client : hello
Client Disconnected
In client window:
F:\ Abarna\Lab>javac tcpchatclient.java
F:\ Abarna\Lab>java tcpchatclient
Type "end" to Quit
Message to Server : hai
Client : hello
Message to Server : end
Result:
Thus a program in Java implemented an application using TCP Sockets like chat.
18
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
19
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
20
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
{
ServerSocket serverSocket = new ServerSocket(4000);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
File transferFile = new File ("send.txt");
byte [] bytearray = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files...");
os.write(bytearray,0,bytearray.length);
os.flush();
socket.close();
System.out.println("File transfer complete");
}
catch(Exception e)
{
}
}
}
Output :
[Create a file named send.txt in the current working directory. After performing file transfer the
content in send.txt is received in the client under the name received.txt in the current working
directory]
In server window
F:\ Abarna\Lab>javac ftserver.java
F:\ Abarna\Lab>java ftserver
Accepted connection : Socket[addr=/192.168.1.18,port=2379,localport=4000]
Sending Files...
File transfer complete
21
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
In client window
Result :
Thus a program in Java implemented an application using TCP Sockets like file transfer.
22
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
23
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
{
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 = 8080;
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);
DatagramPacket recvpack =new DatagramPacket(receivedata,
receivedata.length);
clientsocket.receive(recvpack);
String modified = new String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close();
}
}
dnsserver.java
import java.io.*;
import java.net.*;
public class dnsserver
{
24
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
25
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length,ipaddress,port);
serversocket.send(pack);
serversocket.close();
}
}
}
Output
In server window
F:\ Abarna\Lab>javac dnsserver.java
F:\ Abarna\Lab>java dnsserver
Press Ctrl + C to Quit
Request for host yahoo.com
In client window
F:\ Abarna\Lab>javac dnsclient.java
F:\ Abarna\Lab>java dnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184
Result :
Thus a program in Java performed Simulation of DNS using UDP sockets.
26
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Aim:
To write a program in java to simulate ARP protocols.
Algorithm:
1. Start the program
2. In Client
a. Start the program
b. Using socket connection is established between client and server.
c. Get the IP address to be converted into MAC address.
d. Send this IP address to server.
e. Server returns the MAC address to client.
3. In Server
a. Start the program
b. Accept the socket which is created by the client.
c. Server maintains the table in which IP and corresponding MAC addresses are stored.
d. Read the IP address which is send by the client.
e. Map the IP address with its MAC address and return the MAC address to client.
4. Stop the program
Program
Clientarp.java
import java.io.*;
import java.net.*;
import java.util.*;
class Clientarp
{
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 Logical address(IP):");
27
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
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.println(e);
}
}
}
Serverarp.java
import java.io.*;
import java.net.*;
import java.util.*;
class Serverarp
{
public static void main(String args[])
{
try
{
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"};
28
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
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
F:\ Abarna\2020-2021\Odd\Network Lab>java Serverarp
F:\ Abarna\2020-2021\Odd\Network Lab>java Clientarp
Enter the Logical address (IP): 165.165.80.80
The Physical Address is: 6A:08: AA: C2
Result:
Thus a program in java simulated ARP protocols.
29
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
30
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
31
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
32
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
INTRODUCTION :
Network Simulator (Version 2), widely known as NS2, is simply an eventdriven simulation
tool that has proved useful in studying the dynamic nature of communication networks. Simulation of
wired as well as wireless network functions and protocols (e.g., routing algorithms, TCP, UDP) can
be done using NS2. In general, NS2 provides users with a way of specifying such network protocols
and simulating their corresponding behaviors. Due to its flexibility and modular nature, NS2 has
gained constant popularity in the networking research community since its birth in 1989. Ever since,
several revolutions and revisions have marked the growing maturity of the tool, thanks to substantial
contributions from the players in the field. Among these are the University of California and Cornell
University who developed the REAL network simulator, 1 the foundation which NS is based on.
Since 1995 the Defense Advanced Research Projects Agency (DARPA) supported
development of NS through the Virtual InterNetwork Testbed (VINT) project currently the National
Science Foundation (NSF) has joined the ride in development. Last but not the least, the group of
Researchers and developers in the community are constantly working to keep NS2 strong and
versatile.
BASIC ARCHITECTURE :
Figure shows the basic architecture of NS2. NS2 provides users with executable command ns
which take 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
33
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
OTcl sets up simulation by assembling and configuring the objects as well as scheduling discrete
events (i.e., a frontend).
The C++ and the OTcl are linked together using TclCL. Mapped to a C++ object, variables in
the OTcl domains are sometimes referred to as handles. Conceptually, a handle (e.g., n as a Node
handle) is just a string (e.g., _o10) in the OTcl domain, and does not contain any functionality.
Instead, the functionality (e.g., receiving a packet) is defined in the mapped C++ object (e.g., of class
Connector). In the OTcl domain, a handle acts as a frontend which interacts with users and other OTcl
objects. It may define its own procedures and variables to facilitate the interaction. Note that the
member procedures and variables in the OTcl domain are called instance procedures (instprocs) and
instance variables (instvars), respectively. Before proceeding further, the readers are encouraged to
learn C++ and OTcl languages.
NS2 provides a large number of built-in C++ objects. It is advisable to use these C++ objects
to set up a simulation using a Tcl simulation script. However, advance users may find these objects
insufficient. They need to develop their own C++ objects, and use a OTcl configuration interface to
put together these objects. After simulation, NS2 outputs either text-based or animation-based
simulation results. To interpret these results graphically and interactively, tools such as NAM
(Network AniMator) and XGraph are used. To analyze a particular behavior of the network, users can
extract a relevant subset of text-based data and transform it to a more conceivable presentation.
CONCEPT OVERVIEW
ns uses two languages because simulator has two different kinds of things it needs to do. On
one hand, detailed simulations of protocols require a systems programming language which can
efficiently manipulate bytes, packet headers and implement algorithms that run over large data sets.
For these tasks run-time speed is important and turn-around time (run simulation, find bug, fix bug,
recompile, re-run) is less important. On the other hand, a large part of network research involves
slightly varying parameters or configurations, or quickly exploring number of scenarios.
In these cases, iteration time (change the model and re-run) is more important. Since
configuration runs once (at the beginning of the simulation), run-time of this part of the task is less
important. ns meets both of these needs with two languages, C++ and OTcl.
Nodes
set n0 [$ns node]
set n1 [$ns node]
Links and queuing
$ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type>
<queue_type>: DropTail, RED, etc.
$ns duplex-link $n0 $n1 1Mb 10ms RED
Creating a larger topology
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms RED
}
Link failures
$ns rtmodel-at <time> up|down $n0 $n1
Creating UDP connection
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $n0 $udp
$ns attach-agent $n1 $null
$ns connect $udp $null
proc finish {}
{
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
35
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
}
Schedule Events
$ns at <time> <event>
Call ‘finish’
$ns at 5.0 "finish"
Run the simulation
$ns run
RESULT:
Thus the study of NS was done successfully.
36
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Aim:
To perform simulation of Congestion Control Algorithms (sliding window) using NS.
Algorithm
1. Run NSG 2.1
2. Create two nodes n0 and n1.
3. Create a duplex-link and set the following parameters
a. Queuetype : Droptail
b. Capacity 0.2 Mbps
c. Propagation delay: 200 ms
d. Queue size: 10
4. Create link from n0 to n1.
5. In Agent tab do the following
a. Packet size: 500 bytes
b. Agent type: TCP and draw a line from n0.
c. Agent type: TCP Sink and draw a line from n1.
d. Draw a line from tcp to sink.
6. In Application tab do the following.
a. Application Type: ftp
b. Start time: 0.1
c. Stop time: 3.5
d. Draw line from tcp.
7. In parameters tab do the following
a. Simulation time: 5.0
b. Trace file: sliding.tr
c. Nam file: sliding.nam
d. Click done
8. Click TCL and specify the window side of the sliding window and the transfer of packets and
save the file as sliding.tcl in C:\cygwin\Your folder
Program:
#===================================
# Simulation parameters setup
#===================================
set val(stop) 5.0 ;# time of simulation end
37
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
#===================================
#Create 2 nodes
#===================================
# Links Definition
#===================================
#Createlinks between nodes
38
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp1
39
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
$ns at 0.0 "$ns trace-annotate \"Sliding Window with window size 4 (normal operation)\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \"Send Packet 0,1,2,3\""
$ns at 0.34 "$ns trace-annotate \"Receive Ack 0,1,2,3\""
$ns at 0.56 "$ns trace-annotate \"Send Packet 4,5,6,7\""
$ns at 0.79 "$ns trace-annotate \"Receive Ack 4,5,6,7\""
$ns at 0.99 "$ns trace-annotate \"Send Packet 8,9,10,11\""
$ns at 1.23 "$ns trace-annotate \"Receive Ack 8,9,10,11 \""
$ns at 1.43 "$ns trace-annotate \"Send Packet 12,13,14,15\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack 12,13,14,15\""
$ns at 1.88 "$ns trace-annotate \"Send Packet 16,17,18,19\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack 16,17,18,19\""
$ns at 2.32 "$ns trace-annotate \"Send Packet 20,21,22,23\""
$ns at 2.56 "$ns trace-annotate \"Receive Ack 24,25,26,27\""
$ns at 2.76 "$ns trace-annotate \"Send Packet 28,29,30,31\""
$ns at 3.00 "$ns trace-annotate \"Receive Ack 28\""
$ns at 3.1 "$ns trace-annotate \"FTP stops\""
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam sliding.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run
40
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Steps to execute:
1. Run cygwin and change the directory
Result:
Thus performed simulation of Congestion Control Algorithms (sliding window) using NS.
41
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
42
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
43
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
44
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Output
Result:
Thus studied the performance of TCP/UDP using Simulation Tool (NS2)
45
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Aim:
To perform Simulation of Distance Vector Routing algorithm.
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.
Program (Distance Vector Protocol)
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
$ns namtrace-all $nf
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
46
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
$ns rtproto DV
47
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Output
Result:
Thus performed Simulation of Distance Vector Routing algorithm.
48
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Aim:
To perform Simulation of Link State Routing algorithm.
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.
Program (Link State Protocol)
set ns [new Simulator]
set nr [open link.tr w]
$ns trace-all $nr
set nf [open link.nam w]
$ns namtrace-all $nf
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam link.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
49
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
$ns rtproto LS
50
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Output:
Result:
Thus performed Simulation of Link State Routing algorithm.
51
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
52
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
}
else if($1=="d")
{
dropped++;
}
}
END
{
printf " Packet Sent:%d",sent;
printf "\n Packet Received:%d",received;
printf "\n Number of packets dropped : %.2f", dropped;
printf "\n Packet Delivery Ratio:%.2f",(received/sent)*100;
printf "\n Average throughput : %.2f\n",(received/45);
}
Output:
Abarna@Abarna-PC/ Example/9: $ awk -f 9.awk thro.tr
Packet Sent:59998
Packet Received:55830
Number of packets dropped : 4106.00
Packet Delivery Ratio:93.05
Average throughput : 1240.67
Result :
Thus the program using awk script to evaluate the performance of distance vector and
Link state protocols has been done successfully
53
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Aim :
To write a program in Java to implement the Simulation of Error Correction Code
(CRC)
Algorithm :
At sender side
1. Start the program
2. Read the number of bits to be sent. Let n be the Number of bits in data to be sent from
sender side.
3. Read the number of bits in the divisor. Let k be the Number of bits in the divisor (key
obtained from generator polynomial).
4. The binary data is first increased by adding k-1 zeros in the end of the data
5. Use modulo-2 binary division to divide binary data by the divisor and store remainder
of division.
6. Append the remainder at the end of the data to form the encoded data and send the
same
At receiver side
1. Perform modulo-2 division again and if remainder is 0, then there are no errors.
Modulo 2 division
In each step, a copy of the divisor (or data) is XORed with the k bits of the dividend.
The result of the XOR operation (remainder) is (n-1) bits, which is used for the next
step after 1 extra bit is pulled down to make it n bits long.
When there are no bits left to pull down, we have a result. The (n-1)-bit remainder
which is appended at the sender side.
Program
import java.io.*;
import java.util.*;
class crc
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n;
54
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
// Read input
System.out.println("Enter the size of the data:");
n = input.nextInt();
int data[] = new int[n];
System.out.println("Enter the data, bit by bit:");
for(int i=0 ; i < n ; i++)
{
System.out.println("Enter bit number " + (n-i) + ":");
data[i] = input.nextInt();
}
//Read Divisor
System.out.println("Enter the size of the divisor:");
n = input.nextInt();
int divisor[] = new int[n];
System.out.println("Enter the divisor, bit by bit:");
for(int i=0 ; i < n ; i++)
{
System.out.println("Enter bit number " + (n-i) + ":");
divisor[i] = input.nextInt();
}
//Perform Division
int remainder[] = divide(data, divisor);
for(int i=0 ; i < remainder.length-1 ; i++)
{
System.out.print(remainder[i]);
}
System.out.println("\nThe CRC code generated is:");
55
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
{
System.out.print(remainder[i]);
}
System.out.println();
// Append the remainder
int sent_data[] = new int[data.length + remainder.length - 1];
System.out.println("Enter the data to be sent:");
for(int i=0 ; i < sent_data.length ; i++)
{
System.out.println("Enter bit number " + (sent_data.length-i)+ ":");
sent_data[i] = input.nextInt();
}
receive(sent_data, divisor);
}
static int[] divide(int old_data[], int divisor[])
{
int remainder[] , i;
int data[] = new int[old_data.length + divisor.length];
System.arraycopy(old_data, 0, data, 0, old_data.length);
if(remainder[0] == 1)
{
56
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
57
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
if(remainder[i] != 0)
{
System.out.println("There is an error in received data...");
return;
}
}
System.out.println("Data was received without any error.");
}
}
Output
D:\Abarna>javac crc.java
D:\Abarna>java crc
Enter the size of the data: 7
Enter the data, bit by bit:
Enter bit number 7:
1
Enter bit number 6:
0
Enter bit number 5:
0
Enter bit number 4:
1
Enter bit number 3:
1
Enter bit number 2:
0
Enter bit number 1:
1
Enter the size of the divisor: 4
Enter the divisor, bit by bit:
Enter bit number 4:
1
Enter bit number 3:
0
58
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
59
CS8581_Networks_Lab
4931_Grace College of Engineering, Thoothukudi
Result:
Thus a program in Java implemented the Simulation of Error Correction Code (CRC)
60
CS8581_Networks_Lab