Ns2 Projects with source code

Ns2 Projects with source code – NS2 abbreviated as Network Simulator 2 is a fundamentals of network simulation tool. In short, it’s a event network simulator tools which is highly used in research and development.Ns2 is  developed at UC berkely. Ns2 Projects contains rich library of network and protocol object.

Ns2 framework: 

In which we also going to discuss about what is the languages to be used in ns2 projects as well as nam, graph details.

 Languages used in ns2:

  • TCL:
      • Ns is a Tcl interpreter to run Tcl Scripts.
  • OTCL:
      • OTCL is used to control the simulation as well as to schedule discrete events.
      • Working procedure:
        • Tells traffic sources when to start/stop transmitting packets through the event scheduler
        • Initiates an event scheduler.
        • Sets up the network topology using the network objects.
  • C++:
      • C++ is used for defining the internals of NS2.
      • By using C++ the following program is written and compiled.
        • Basic network component objects.
        • Event scheduler.

Supporting Tools Utilized in Ns2 Projects : There also two supporting tools also used in ns2 projects  for providing output.

  • NAM
  • XGraph


  • It’s a network animator
  • Nam is a Tcl/TK based animation tool for viewing network simulation traces and real world packet traces.
  • The Network Animator (nam) is a completely separate program that is distributed with the NS simulator.
  • It supports topology layout, packet level animation, and various data inspection tools. 
Steps used for creating output file in nam:
  • Create an output file.
  • Before running the simulation activate the NAM trace feature in NS.
  • At the end of the simulation close the output file.
  • Sample Ns2 Projects with source code of nam is as follows,.
# (1) Create the trace file

 set namfile [open out.nam w]

 # (2) Tell NS to write NAM network events to this trace file

 $ns namtrace-all $namfile

 # (3) Close output file at the end of the simulation run

 proc finish {} {

 global ns namfile

 $ns flush-trace                 ;# flush trace files

 close $namfile                  ;# close trace file

 exec nam out.nam &        ;# OPTIONAL: run NAM from inside the NS simulation 

 exit 0



XGraph is a plotting program that is used to create graphic representations of simulation results. It is important because it allows some basic animation of data sets. The animation only pages through data sets in the order in which they loaded. It is quite crude, but useful if all the data sets  in one file in the time order, and are put out at uniform intervals

XGraph includes the following:
  • Animation and also derivatives.
  • Interactive plotting and also graphing.
Sample Ns2 Projects with source code for generating XGraph:

exec xgraph example.tr –geometry 700×500 

Protocols used in Ns2 Projects with source code

The following protocols also to the combination of proactive, reactive, hybrid and clustering protocols,

  • AODV
  • DSR
  • DSDV
  • GPSR
  • W-MAC
  • S-MAC
  • T-MAC
  • Z-MAC
  • TEEN
  • R-MAC
  • Zigbee
  • Buetooth
  • Bluestar
  • Star
  • TDMA
  • CDMA
  • SDMA
  • IZRP
  • ZRP
  • OLSR
  • HEED
  • SOP
  • DMRP
  • TORA
  • and also SAFARI.

Attacks handled in ns2:

  • DDOS.
  • DOS.
  • Black hole attack.
  • Warm hole attack.
  • Jellyfish..
  • Grey hole attack.
  • Sybil attack.
  • Misbehaving nodes.
  • Selfish nodes.
  • Phishing attack.
  • False node misbehavior.
  • Man in the middle attack.
  • IP spoofing.
  • Ping of death.
  • Smurf attack.
  • ICMP attack.
  • Distributed attack.
  • Insider attack.
  • Close-in attack.
  • Trojan horse.
  • Viruses.
  • IP fragmentation attack.
  • SQL injection attack.
  • Port scanning.
  • Email spoofing.
  • Network sniffing.
  • DNS sniffing.
  • Social phishing.
  • Eavesdropping.
  • Application-layer attack.