Exp No.
11
                                STUDY OF NS2 SIMULATOR
AIM: To study NS2 simulator
THEORY:
Introduction to Network Simulator-Version 2 (NS2):
NS2 is simply an event-driven 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.
Basic Architecture
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, the 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.
Zlib : This is the required library for NAM.
Xgraph: This is a data plotter with interactive buttons for panning,zooming, printing, and
selecting display options.
NAM ( Network Animator): NAM is a TCL-based animation tool for viewing network
simulation traces and real traces of packet data. To use NAM, first generate a trace file that
contains topological information like nodes, links and packet traces. Once the trace file is
generated, NAM will read it, create a topology, pop up a window, do the layout if necessary,
and then pause at the time of the first packet is in the trace file. NAM provides control over
many aspects of animation through its user interface, and does animation using the following
building blocks: node, link, queue, packet, agent and monitor.
NAM Animator screen
Traced file content
Installing NS2 Simulator
Open the terminal in a linux machine and type the following comments
For Ubuntu 20.04
sudo apt update
sudo apt get install ns2
sudo apt get install nam
Running NS2 Simulation
To execute the file open a terminal and type ns filename(ex: ns hello.tcl)
a) The network consists of TCP source node (n0) and destination node (n1) over an area
size of 500m x 500m. Node (n0) uses Agent/TCP/Reno as the sending TCP agent and FTP
traffic source. Node (n1) is the receiver of FTP transfers, and it uses Agent/TCP sink as its
TCP-agent for the connection establishment. Run the simulation for 150 seconds and show
the TCP window size in two static nodes scenario with any dynamic routing protocol. Run
the script and analyze the output graph for the given scenario.
Simple two node wired network(TCP Connection)
Program:
#Creating a Simulator Object
        set ns [new Simulator]
        $ns color 2 Red
# Setting up files for trace & NAM
         set trace_nam [open out.nam w]
         set trace_all [open all.tr w]
#Tracing files using their commands
        $ns namtrace-all $trace_nam
         $ns trace-all $trace_all
#Closing trace file and starting NAM
proc finish { } {
           global ns trace_nam trace_all
           $ns flush-trace
          close $trace_nam
          close $trace_all
          exec nam out.nam &
          exit 0
}
#Creating NODES
set n0 [$ns node]
set n1 [$ns node]
#Creating LINKS
$ns duplex-link $n0 $n1 200Mb10ms DropTail
$ns queue-limit $n0 $n1 50
#Attaching AGENT TCP to NODE 1
set TCP0 [new Agent/TCP]
$TCP0 set class_ 2
$ns attach-agent $n0 $TCP0
#Attaching AGENT TCP SINK to NODE 2
set SINK0 [new Agent/TCPSink]
$ns attach-agent $n1 $SINK0
#Attaching Application (FTP) to first node(NODE1)
set FTP0 [new Application/FTP]
$FTP0 attach-agent $TCP0
#Connecting two agents(NODE0 and NODE1)
$ns connect $TCP0 $SINK0
#Setting simulation time
        $ns at 5.0 "$FTP0 start"
        $ns at 50.0 "$FTP0 stop"
        $ns at 150.0 “finish”
#Run the simulation
$ns run
Output:
Figure1:
     Sending
TCP Packet
Receiving acknowledgment
b. Simulate the transmission of ping messages over a star network topology consisting of
‘n’ nodes and find the number of packets dropped due to congestion using NS2simulator
Description:
The network for this experiment uses six nodes n0 to n5. In this scenario, node n1 sends
constant bit-rate (CBR) traffic to node n5 using UDP agent. The UDP traffic travels
through n2 and n3 before reaching n5. Both n0 and n4 node sends ping message at certain
time intervals. Ping messages are dropped from the network when the traffic through node
n2 and n3 is full.
Program:
Create a .tcl file(hello.tcl)
# Creating New Simulator
                 set ns [new Simulator]
                 $ns color 1 Blue
                 $ns color 2 Red
            # Setting up the traces
                 set f [open outEx1.tr w]
                 set nf [open outEx1.nam w]
                 $ns namtrace-all $nf
                 $ns trace-all $f
                                  proc finish { } {
                 global ns nf f
                 $ns flush-trace
                 puts "Simulation completed."
                 close $nf
                 close $f
                 exec nam outEx1.nam &
# FIND THE NUMBER OF PING PACKETS DROPED
                    puts "number of ping packets dropped are"
                    exec grep "^d" outEx1.tr | cut -d " " -f 5 | grep -c "ping" &
                    exit 0
                  }
#Create Nodes
                 set n0 [$ns node]
                 puts "n0: [$n0 id]"
                 set n1 [$ns node]
                 puts "n1: [$n1 id]"
                 set n2 [$ns node]
                 puts "n2: [$n2 id]"
                 set n3 [$ns node]
                 puts "n3: [$n3 id]"
                 set n4 [$ns node]
                puts "n4: [$n4 id]"
                set n5 [$ns node]
                puts "n5: [$n5 id]"
#Setup Connections
$ns duplex-link $n0 $n2 .1Mb 5ms DropTail
$ns duplex-link $n1 $n2 .1Mb 5ms DropTail
$ns duplex-link $n2 $n3 .1Mb 5ms DropTail
$ns duplex-link $n3 $n4 .1Mb 5ms DropTail
$ns duplex-link $n3 $n5 .1Mb 5ms DropTail
$ns queue-limit $n2 $n3 2
$ns duplex-link-op $n2 $n3 queuePos .5
                     Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from $from with group trip time $rtt ms"
}
#Setting Ping agent
set p0 [new Agent/Ping]
$p0 set class_ 2
$p0 set fid_ 2
$p0 set packetSize_ 1500
$ns attach-agent $n0 $p0
set udp1 [new Agent/UDP]
$udp1 set class_ 1
$udp1 set fid_ 1
$ns attach-agent $n1 $udp1
set null5 [new Agent/Null]
$ns attach-agent $n5 $null5
set p4 [new Agent/Ping]
$ns attach-agent $n4 $p4
$p4 set class_ 2
$p0 set fid_ 2
$p4 set packetSize_ 1500
$ns connect $p0 $p4
#Setup traffic sources
set cbr1 [new Application/Traffic/CBR]
$cbr1 set rate_ 1Mb
$cbr1 set packetSize_ 1000
$cbr1 attach-agent $udp1
$ns connect $udp1 $null5
$udp1 set fid_ 0
#$ns connect $tcp0 $sink0
#$tcp0 set fid_ 1
#Start up the sources
$ns at 0.05 "$p0 send"
$ns at 0.30 "$cbr1 start"
$ns at 0.30 "$p4 send"
$ns at 0.65 "$p0 send"
$ns at 0.70 "$p4 send"
$ns at 0.75 "$p4 send"
$ns at 1.0 "$cbr1 stop"
$ns at 4.0 "finish"
$ns run
ns hello.tcl (to execute the file)
Output
c) Simulate Link State Protocol in NS2
Description:
The first step is to initialize the network simulator, by creating a network simulator object. After
that, initialize rtproto (routing protocol) to Link State (LS).
Program:
set ns [new Simulator]
$ns rtproto LS
set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
set node5 [$ns node]
set node6 [$ns node]
set node7 [$ns node]
set tf [open out.tr w]
$ns trace-all $tf
set nf [open out.nam w]
$ns namtrace-all $nf
$node1 label "node 1"
$node1 label "node 2"
$node1 label "node 3"
$node1 label "node 4"
$node1 label "node 5"
$node1 label "node 6"
$node1 label "node 7"
$node1 label-color blue
$node2 label-color red
$node3 label-color red
$node4 label-color blue
$node5 label-color blue
$node6 label-color blue
$node7 label-color blue
$ns duplex-link $node1 $node2 1.5Mb 10ms DropTail
$ns duplex-link $node2 $node3 1.5Mb 10ms DropTail
$ns duplex-link $node3 $node4 1.5Mb 10ms DropTail
$ns duplex-link $node4 $node5 1.5Mb 10ms DropTail
$ns duplex-link $node5 $node6 1.5Mb 10ms DropTail
$ns duplex-link $node6 $node7 1.5Mb 10ms DropTail
$ns duplex-link $node7 $node1 1.5Mb 10ms DropTail
$ns duplex-link-op $node1 $node2 orient left-down
$ns duplex-link-op $node2 $node3 orient left-down
$ns duplex-link-op $node3 $node4 orient right-down
$ns duplex-link-op $node4 $node5 orient right
$ns duplex-link-op $node5 $node6 orient right-up
$ns duplex-link-op $node6 $node7 orient left-up
$ns duplex-link-op $node7 $node1 orient left-up
set tcp2 [new Agent/TCP]
$ns attach-agent $node1 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $node4 $sink2
$ns connect $tcp2 $sink2
set traffic_ftp2 [new Application/FTP]
$traffic_ftp2 attach-agent $tcp2
proc finish{} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
$ns at 0.5 "traffic_ftp2 start"
$ns rtmodel-at 1.0 down $node2 $node3
$ns rtmodel-at 2.0 up $node2 $node3
$ns at 3.0 "traffic_ftp2 start"
$ns at 4.0 "traffic_ftp2 stop"
$ns at 5.0 "finish"
$ns run
Output:
In the output obtained there is a normal flow of packets from node 1 to node 4. We now
disable the link between nodes 2 and 3 at time 1 and the simulations changes as shown
below. The disabled link is shown in red and the packet flow this time changes direction
rather than the original route to node 4
Result :
Simulated NS2 .