‘ns‘ is a discrete event simulator targeted at networking research. Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks. ns-2 is a packet-level simulator and essentially a centric discrete event scheduler to schedule the events such as packet and timer expiration. Centric event scheduler cannot accurately emulate “events handled at the same time” in real world, that is, events are handled one by one. However, this is not a serious problem in most network simulations, because the events here are often transitory. Beyond the event scheduler, ns-2 implements a variety of network components and protocols.

Setting it up
Setting up ‘ns’ in your Ubuntu system is really easy with the PPA given below :
sudo add-apt-repository ppa:wouterh/ppa
sudo apt-get update
sudo apt-get install ns2 nam xgraph
and we are almost there. Test Working of this simulator :-
nam
ns2 Installation
[img src=http://www.linoob.com/wp-content/flagallery/ns2-installation/thumbs/thumbs_02update-repository.png]6870
[img src=http://www.linoob.com/wp-content/flagallery/ns2-installation/thumbs/thumbs_03install-ns2.png]7060
[img src=http://www.linoob.com/wp-content/flagallery/ns2-installation/thumbs/thumbs_04nam-editor.png]6640
If a window showing ‘The Network Animator’ shows up, then we are good to go! For further details on the package, please see this link.
Time for Experiments
Just fire up anyone of the following and start working on them.
- ns
- nam
- tclcl
- otcl
For e.g. – First, we typed ‘ns’ and were presented with a prompt where all the programming oft he network is to be done which is about to be simulated. After that we type ‘nam’ in the terminal and the window will appear showing NAM (The Network Animator). This tool was primarily built for animated packet trace data. This trace is typically derived as the output of any simulator say ‘ns’ or from real network measurements e.g. using ‘tcpdump’.
ns2 Directory structure –>> The C++ classes of ns-2 network components or protocols are implemented in the subdirectory “ns-2”, and the TCL library (corresponding to configurations of these C++ instances) in the subdirectory of “tcl”.

‘nam’ –>> Type ‘nam’ in the terminal. This will open a window and click ‘new’. This will bring up the ‘nam’ UI where you are supposed to design everything. The design instructions are as follows :-
- Create nodes using ‘Add Nodes’ tool.
- Connect them using ‘Add Link’ tool.
- Attach an ‘Agent’ to a node.
- Connect agents by using Link tool.
- Set options by double-clicking or right-clicking on each object.
- Run the scenario using File menu command, run ‘ns’.
Take a look at the ‘nam’ editor and its components :

Working with ‘ns’ –>> Type ‘ns’ in the terminal to get started. Now follow the instructions to set up a TCL connection. Save this file in any text editor as ‘test1.tcl’ being this the first tcl script of this tutorial.
#Create a simulator object
set ns [new Simulator]#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf#Define a ‘finish’ procedure
proc finish {} {
global ns nf
$ns flush-trace#Close the trace file
close $nf#Execute nam on the trace file
exec nam out.nam &
exit 0
}# Insert your own code for topology creation and agent definitions, etc. here
#Call the finish procedure after 5 seconds simulation time$ns at 5.0 “finish”
#Run the simulation
$ns run
done. You can run this script by typing in ‘ns test1.tcl’ but apparently we haven’t defined any objects (nodes, links, etc.) or events, so we are going to get an error message like ‘nam: empty trace file out.nam’. We are going to define the objects now :
In this section we are going to define a very simple topology with two nodes that are connected by a link. The following two lines define the two nodes. (Note: You have to insert the code in this section before the line ‘$ns run’, or even better, before the line ‘$ns at 5.0 “finish”‘).
set n0 [$ns node]
set n1 [$ns node]
A new node object is created with the command ‘$ns node’. The above code creates two nodes and assigns them to the handles ‘n0′ and ‘n1′. The next line connects the two nodes.
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue. Now you can save your file and start the script with ‘ns example1.tcl’. nam will be started automatically. Below you can see the output of the work we have done so far. Easy isnt it, but it will be understandable to you only when you are having knowledge of computer-networks, protocols and particularly the OSI model.
Lets test another one - This time we will be straight forward to the concept, this time we are going to ‘send data’ between two nodes. In ns, data is always being sent from one ‘agent’ to another. So the next step is to create an agent object that sends data from node n0, and another agent object that receives the data on node n1. Considering the above example, add agent ‘UDP’ and set other preferences as listed below :
# Insert your own code for topology creation and agent definitions, etc. here. Call the finish procedure after 5 seconds simulation time. Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0#Now the two agents have to be connected with each other.
$ns connect $udp0 $null0
$ns at 0.5 “$cbr0 start”
$ns at 4.5 “$cbr0 stop”
Add these lines carefully to the file before the line ‘$ns at 5.0 “finish” ‘ and save it. Run it again as test1.tcl and after a few seconds you will see a flow of data from one node to another. It looks really fascinating to see the first simulation working
Here, take a look :-
ns Testing
[img src=http://www.linoob.com/wp-content/flagallery/ns-testing/thumbs/thumbs_02first-simulation.png]16780
[img src=http://www.linoob.com/wp-content/flagallery/ns-testing/thumbs/thumbs_03-advanced-code.png]16050
[img src=http://www.linoob.com/wp-content/flagallery/ns-testing/thumbs/thumbs_04-output.png]15380
Thats it for now. You should test more things with nam & TCL scripts like packer monitoring etc. The ns2 documentation will make things more clear to you. The following links may also prove useful in understanding the concepts – ns-tut , wireless simulation , ns-by-example.
For any queries regarding Mint/Ubuntu can be asked here anytime.
Njoy Network Simulation !!!