Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


Ns2 Simulator

Educative Answers Team

Ns2 is a software used to show the network flow to a user. It is a simulator that describes the route or the path taken by the packets in a network. The following is a sample problem, with a solution, to help explain the Ns2 simulation.

Ns2 uses a .tcl file to view the simulation of a network.

The objective

  • This network consists of 4 nodes (n0, n1, n2, n3).

  • The duplex links between n0 and n2, and n1 and n2 have 2 Mbps of bandwidth and 10 ms of delay. The duplex link between n2 and n3 has 1.7 Mbps of bandwidth and 20 ms of delay.

  • Each node uses a DropTail queue that has a maximum size of 10.

  • A “TCP” agent is attached to n1, and a connection is established to a TCP “sink” agent attached to n3. A TCP “sink” agent generates and sends ACK packets to the sender (TCP agent) and frees the received packets. A “UDP” agent that is attached to n0 is connected to a “null” agent attached to n3. A “null” agent frees the packets received.

  • An “FTP” and a “CBR” traffic generator are respectively attached to “TCP” and “UDP” agents, and the “CBR” is configured to generate 1 Kbytes packets at the rate of 100 packets per second. FTP will automatically control the traffic according to the throttle mechanism in TCP.

  • The traffic flow of UDP must be colored red, and the traffic flow of TCP must be colored blue. The “CBR” is set to start at 0.1 sec and stop at 4.5 sec; the “FTP​​” is set to start at 0.5 sec and stop at 4.0 sec.


#Creating simulator object
set ns [new Simulator]

#creating two color classes for ns object to distinguish the traffic coming from various sources
$ns color 0 blue
$ns color 1 red

#Creating the nam file
set nf [open out.nam w]
$ns namtrace-all $nf

#Finish Procedure
proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
#Creating Four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Creating a duplex links
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-down

$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns duplex-link-op $n2 $n3 orient right

#Limiting the queue to only 10 packets 
$ns queue-limit $n0 $n2 10
$ns queue-limit $n1 $n2 10
$ns queue-limit $n2 $n3 10

#Creating a TCP agent and connecting it to n1
set tcp1 [new Agent/TCP]

#Specifying tcp traffic to have blue color as defined in the second line of the program
$tcp1 set fid_ 0

$ns attach-agent $n1 $tcp1

#Creating a Sink Agent and attaching it to n3
set sinkTCP3 [new Agent/TCPSink]
$ns attach-agent $n3 $sinkTCP3

#Connecting TCP agent with Sink agent
$ns connect $tcp1 $sinkTCP3

#Specifying the UDP agent
set udp0 [new Agent/UDP]

#Specifying udp traffic to have red color as defined in the second line of program
$udp0 set fid_ 1

#Attaching the UDP agent with n0
$ns attach-agent $n0 $udp0

#Specifying the Null agent
set null0 [new Agent/Null]

#Attaching the NULL agent with n3
$ns attach-agent $n3 $null0

#Connecting both udp0 and null0 agents for transferring data between n0 and n1
$ns connect $udp0 $null0

#Creating FTP agent for traffic and attaching it to tcp1
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp1

#Specifying the CBR agent to generate the traffic over udp0 agent
set cbr0 [new Application/Traffic/CBR]

#Each packet having 1K bytes
$cbr0 set packetSize_ 1000

#Each packet will be generated after 10ms i.e., 100 packets per second
$cbr0 set interval 0.010 

#Attaching cbr0 with udp0
$cbr0 attach-agent $udp0

#Starting the FTP Traffic
$ns at 0.5 "$ftp0 start"
$ns at 4.0 "$ftp0 stop"

#Starting the cbr0 at 0.5 simulation time
$ns at 0.1 "$cbr0 start"

#Stoping the cbr0 at 4.5 simulation time
$ns at 4.5 "$cbr0 stop"

#Calling the finish procedure
$ns at 5.0 "finish"

#Run the simulation
$ns run

Comments in a .tcl file are represented by the ‘#’ symbol.

Drawn out solution


Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring