BCA / B.Tech 9 min read

Basic Syntax of NS2 & NAM

Basic Syntax of NS2 & NAM in Hindi | Basic Syntax of NS2 (Network Simulator 2) and NAM (Network Animator) in Hindi:


  • NS2 (Network Simulator 2) and NAM (Network Animator) are very important tools for network simulation and animation. NS2 allows users to simulate various network protocols and topologies,
  • while NAM provides a graphical visualization of these simulations.
  • The basic syntax knowledge of NS2 and NAM is important in network simulation and animation. NS2 is based on the Tcl programming language, while NAM provides a graphical interface. Users can simulate various network protocols and activities using both these tools,
  • so that they can get a better understanding of the behavior of the network. The combination of NS2 and NAM makes the simulation more effective and clear, which is extremely helpful in both research and teaching.

In this answer, we will discuss in detail the basic syntax of NS2 and NAM and their use.

Basic Syntax of NS2:

The syntax of NS2 is based on the Tcl (Tool Command Language) programming language. While writing a script in NS2, it is necessary to pay attention to some important points:

1. Structure of a simple script

# Creation of simulation
set ns [new Simulator]

# Creation of nodes
set node1 [$ns node]
set node2 [$ns node]

# Creation of link
$ns connect $node1 $node2

# Setting events for simulation
$ns at 0.0 "$node1 sendPacket"
$ns at 5.0 "$node2 receivePacket"

# Starting the simulation
$ns run

2. Description of different elements:

  • Simulator: A new simulator is created through set ns [new Simulator]. It manages animation and various network events.
  • Nodes: Nodes are created using set node1 [$ns node] and set node2 [$ns node]. In NS2, nodes are the main components of the network.
  • Connections: A link is established between nodes using $ns connect $node1 $node2.
  • Events: Events are set using $ns at 0.0 "$node1 sendPacket", which includes various activities such as sending or receiving a packet.
  • Run Simulation: $ns run is used to start the simulation.

3. Use of packets and protocols:

# Packet creation
set packet [new Agent/UDP]

# Connecting the packet to nodes
$ns attach-agent $node1 $packet
$ns connect $packet $node2
Basic Syntax of NAM
NAM (Network Animator) provides a graphical interface with NS2. NAM is used to see a visual representation of network activities during a simulation.

Syntax of NAM in Hindi | Syntax for NAM:

The syntax for NAM is used in an NS2 script. An example is given below:

# Generating a trace file for NAM
set tracefile [open out.nam w]
$ns namtrace-all $tracefile

# Running NAM after the simulation
$ns nam $tracefile

Use of trace Files in Hindi | Use of a trace file:

  • To use a trace file in NAM, first it is opened and all events are traced.
  • The name of the trace file usually has a .nam extension.

Combination of NS2 and NAM

By using NS2 and NAM together, the user can see the network activities in real-time during the simulation. Within the NS2 script, proper tracing and management of events for NAM is done, which gives the user a detailed view.

Example script

# A simple script for NS2 and NAM
set ns [new Simulator]

# Creation of nodes
set node1 [$ns node]
set node2 [$ns node]

# Creation of link
$ns connect $node1 $node2

# Trace file
set tracefile [open out.nam w]
$ns namtrace-all $tracefile

# Setting events
$ns at 0.0 "$node1 sendPacket"
$ns at 5.0 "$node2 receivePacket"

# Starting the simulation
$ns run

In this Chapter

Basic Syntax of NS2 & NAM
Auto-Configuration and Anycast
Modified EUI-64 | What is Modified EUI-64?
Architecture of Mobile Computing
Wireless LAN in Hindi | What is Wireless LAN?
Wireless User Devices
MAC Protocol | What is MAC Protocol?
IEEE 802.11 | What is IEEE 802.11
Mobile IP | What is Mobile IP
Wireless TCP/IP
Unicast & Multicast Communication
Bluetooth | What is Bluetooth?
NS2 & NAM | What are NS2 and NAM?
Purpose & installation of NS2 & NAM
Background of NS2 & NAM
Architecture of NS2 & NAM
Interface of OTcl & C++
Trace Files & Formats
Protocol Support of NS2
Simulation Object of NS2 & NAM
Node Creation of NS2 & NAM
Running NS2 & NAM
Finish Procedure NS2 & NAM
Invoking external commands within NS2
Nodes & Agents of NS2 & NAM
NS2 Commands
Creating Links in Wired Network
Setting Link Parameters
Sending Traffics Through NS2 Links
Routing Protocol Support of NS2
Scenarios in Wired Networks
Additional Parameters
Setting node positions
God object & topography
Protocol Support
Scenarios in Wireless Networks
What is the Internet? Difference Between Intranet and Extranet
Types of Internet
OSI Model and its Layers
Computer Network (CN) All Important Questions and Answers in English (MDSU)
BCA | Computer Network | 2025 Paper | MDSU Exam Paper
Importance of the Layer Model in Computer Networks
Network Classification
Network Topology
Network Switching & Components
Ethernet
Token Ring
Basic Networking Concepts & Cabling
What is a Computer in English?
Bridges in Computer Networks
Routers in Computer Networks
Gateways in Computer Networks
Public & Private Networks
FDMA (Frequency Division Multiple Access)
BCA | CN(Computer Network) Paper | 2023 MDSU Exam Paper
What is World Wide Web (WWW)
TDMA, SDMA, CDMA
Personal Communication System | What is PCS
IPv4 | What is IPv4?
Subnetting IPv4 Address
Casting in IPv4
Private IPv4 Addressing
IPv4 Address Scheme
IPv6 Addressing Scheme
Types of IPv6 Addressing