Skip to main content

PyTorch Code for Simple Neural Networks for MNIST Dataset

Network Simulator 2 - Simple Example - NS2 Tutorial 2

Introduction to Network Simulator 2

In this post

  • Introduction to Network Simulator 2 and its architecture
  • Installation of Network Simulator 2 in various Operating Systems
  • How to simulate a given network with available libraries of NS2

The network simulator 2 (ns) addresses large number of protocols, applications, agents, queues, etc. These are called as “simulated objects” either in the C++ or OTCL. This book will let the users to code the ns Tcl scripts for wired and wireless networks. This book is intended for students, researchers who need better understanding of networks.

NS is a discrete event simulator developed towards the research and development in networking. NS supports various supports on wired, wireless networks, protocols, unicast/multicast networks, satellite networks. The recent versions of ns2 support latest wireless networks.

NS evolves in 1989 as REAL network simulator. Later in the year 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UCB, and USC/ISI.

While ns is favorable to researchers right through the inception it is still not a finished or a polished product. Still the bugs are being discovered and corrected by the researchers.  Sometimes the simulations may be invalidated by the bugs. Users are responsible for verifying themselves to correct these.

The main purpose of this book is to provide the readers the basic knowledge and architecture on ns2. This book provides with Tcl scripts for various networks that were available as modules within ns2. The best way to start learning NS2 is through the Ns Manual [1] provided by the However, this book will help you to simulate these networks that were available in ns2 software.

Installation of NS2

NS2 uses two languages OTCL and C++, in which OTCL provides the front end and C++ runs in the backend. For more details, please refer Chapter 3.


·       Interpreted language also called as interpreted hierarchy

·       OTcl is Object Oriented Tcl

·       Preferable for beginners to simply test the protocols, wired, wireless networks, etc

·       Interpretation is fast and execution is slow (so prefer to run in the front end)


·       Compiled hierarchy

·       Preferable when dealing with packet, agent, application, protocol, routing, etc.

·       Compilation is slow, but execution is faster (so prefer to run in the backend)

TclCL is

·       Provides some classes for interfacing between C++ and OTcl

·       Consists of classes with methods Tcl and members to does the mapping between the other two languages.

The fig1.1 will describe the same.



Fig 1.1 – Architecture of NS2

As shown in the fig1.1, the input is the Tcl script and the output is to plot the graph or animate the network.

·       Develop the .tcl file.

·       Execute the file using ns filename.tcl

·       There are two output files

·       A file with nam as extension to show the animation

·       To execute the nam file, nam filename.nam

·       A file with .tr or .xg or any other as specified to plot the graph and to identify the performance characteristics

·       To see the graph, either to use xgraph or tracegraph or gnuplot

·       The trace file can also be processed using awk script by analyzing the text information from the file and predict the performance metrics of the networks

Directory Structure of NS2

This book deals with the latest ns package with version 2.35. The NS is a free tool that can be downloaded from [3]. It can be installed on various operating systems like Linux, Unix, Mac Systems, Windows, etc.  This book unless otherwise specified, uses Linux as the Operating System for using NS2.

NS comes with two different forms,

·       All in one package

·       The current version is ns-2.35

·       The various packages comes along with the allinone package

·       nam-1.15

·       ns-2.35

·       otcl-1.14

·       tcl-8.5.10

·       tk-8.5.10

·       tclcl-1.20

·       xgraph-12.2

·       All these packages comes in a single bundle and it contains a install script that compiles all the packages using the make utility and generate the binaries for ns, nam, xgraph, etc.

·       Component wise package suite

·       In this all the packages have to be compiled separately

NS2 has the directory structure with ns-allinone-2.35 is the top level and there are subsequent levels. The fig 1.2 shows the directory structure of ns-2.35


NS2 Directory Structure
NS2 Directory Structure

The ns-2.35 folder contains all the simulated modules in the compiled form and there are so many other folders within the ns-2.35. If anyone is working on the networks, then they can retain themselves within this ns-2.35 directory, because that contains almost everything related to the network.

Some other folders of use are

·       Ns-2.35 /

·       aodv – that contains the code of the routing algorithm AODV

·       dsdv – contains the code for the routing DSDV

·       mac – contains all the mac layer codes , Ex. 802.11, etc

·       queue – codes that contain all the queuing nodes

·       mobile – information about the mobile parameters of the node.

·       tcl – all the simulation modules in the interpreted hierarchy

·       lib – all the TCL simulation objects

·       ex – contains the tcl scripts for various networks

Installation of NS2 on a UNIX like platform

To install the ns2 software on a UNIX like platform, the install script has to be executed using the make utility


Shell> ./install

To install all the required software and to create the binaries.

Shell> ./validate

This is to validate the all the required functionalities that were installed during the ./install process.

Please refer, Appendix A for detailed installation instructions for most of the Operating systems.

Starting with NS2

Starting with ns, the user need to know lot many things. Listed are some important ideas and techniques before proceeding to program ns.

·       set tcp1 [new Agent/TCP]

·       In the above statement, [new Agent/TCP] creates a shadow object from the TcpClass Agent class.

·       When this statement is dealt with, there are many parameters to be noted.

·       Where is the actual agent (in which source file the agent was written).

·       TcpClass was written in C++ and the code is located at ~ns-2.35/tcp/

·       When new Agent/TCP is called in OTCL, the TcpClass runs in the backend and thus creates a shadow object which returns the TcpAgent.

·       There are many parameters executes when the agent is created. TcpAgent also has many parameters initialized and modified during the simulation of network. However, not all the parameters are relevant for the research when simulating the network.

·       All those parameters default values are located in the ~ns-2.35/tcl/lib/ns-default.tcl file. These values can be overwritten within the tcl script when the user is writing.

·       For example, Agent/TCP set numdupacks_ 3 (this is the line taken from the ns-default.tcl file and specifies that the number of duplicate acknowledgment is 3 and if the user wanted to change it to 5, the code has to be included in the tcl file like this

·       $tcp0 set numdupacks_ 5 (as tcp0 is the handler for the Agent/TCP)

·       There may be more number of C++ classes running in the background when the Tcl file is interpreting and the researchers are advised to go through the source code if they are programming a given network.

The following table tells the Agent name and their corresponding file location.










Table 1.1 – Otcl - C++ Class names

Let us go through a simple example of a wired network that exchange packets between the nodes.

Two wired Nodes
Two wired Nodes

Listing 1.1 – TCP with FTP Traffic between two nodes

#create a new simulator object ns

set ns [new Simulator]

# creation of two nodes n0 and n1

set n0 [$ns node]

set n1 [$ns node]

#Open the files for tracing in write mode, if the file already exits, it will be overwritten, else a #new file will be created

set tracefile [open w]

$ns trace-all $tracefile

#Open the files for network animation

set namfile [open out.nam w]

$ns namtrace-all $namfile

#Create a duplex link between nodes n0 and n1

$ns duplex-link $n0 $n1 2MB 10ms DropTail

#Creates TCP Agent and attach it to node 0

set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

#creates TCP Sink Agent and attach that agent to node 1

set tcpsink0 [new Agent/TCPSink]

$ns attach-agent $n1 $tcpsink0

# Set the Traffic for FTP and attach it to TCP Agent

set ftp [new Application/FTP]

$ftp attach-agent $tcp0

#connect the source and the sink

$ns connect $tcp0 $tcpsink0

#Start the traffic at 1.0seconds.

$ns at 1.0 "$ftp start"

#call the finish procedure at 4.0 sec

$ns at 4.0 "finish"

#Procedure finish{}

proc finish {} {

            global ns tracefile namfile

            exec nam out.nam &;  #execute the animation within the procedure

            exit 0


#Run the simulation

$ns run


This chapter gives you the short glimpse of what is ns2 and how it is installed under various operating system. This chapter deals with the directory structure of ns2 with various other packages that are of important to Network simulator for its operation. Also this chapter shows the two languages used in ns2 OTcl and C++ and demonstrates a wired Tcl script with Network animation screenshot.


Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libclang-dev llvm-

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/ Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/ ~xgraph-12.2/ ~otcl-1.14/ Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes made Step 4: Open a new termi

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb bionic main universe $ sudo apt update Since, it&#