Basics of NS2 and Otcltcl script

NS2 (Network Simulator version 2) is a discrete event network simulator widely used to simulate and analyze computer network behavior. It is open-source software written in C++ and Otcl (Object-oriented Tool Command Language). Otcl is an extension of Tcl (Tool Command Language) used to create and control network entities and configure network scenarios in NS2.

NS2 has a dual-language architecture: the C++ part provides the underlying simulation engine for scheduling events, maintaining network state, and handling low-level packet processing, while the Otcl part provides the user interface for creating and configuring network entities like nodes and links, and specifying network scenarios.

NS2 Architecture Otcl Layer ? User Interface ? Network Configuration ? Script Control C++ Engine ? Event Scheduling ? Packet Processing ? Protocol Implementation Otcl controls C++ simulation engine

Basic Otcl Script Example

Here's a basic example of an Otcl script that creates a simple network scenario with two nodes and a duplex link:

# Create a Simulator object
set ns [new Simulator]

# Create two nodes
set n0 [$ns node]
set n1 [$ns node]

# Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail

# Start the simulation
$ns run

This script creates a Simulator object to control the simulation, two nodes (n0 and n1), and a duplex link between them with 1 Mbps bandwidth, 10 ms delay, and DropTail queue management. The $ns run command starts the simulation.

Essential NS2 Commands

  • set ns [new Simulator] Creates a new Simulator object to control the simulation

  • set n[i] [$ns node] Creates a new node with unique identifier i

  • $ns duplex-link $n0 $n1 bw delay queue-type Creates bidirectional link between nodes

  • $ns simplex-link $n0 $n1 bw delay queue-type Creates unidirectional link between nodes

  • $ns at time "command" Schedules command execution at specified simulation time

  • $ns run Starts and executes the simulation

Command Examples

Creating Network Topology

set ns [new Simulator]

# Create nodes
set n0 [$ns node]
set n1 [$ns node] 
set n2 [$ns node]

# Create links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns simplex-link $n0 $n2 2Mb 15ms DropTail

Event Scheduling and Simulation Control

# Schedule events
$ns at 1.0 "puts "Simulation started""
$ns at 10.0 "$ns stop"

# Run simulation
$ns run

Key Features

  • Protocol Support Implements TCP, UDP, routing protocols like DSR, AODV, DSDV

  • Queue Management Supports DropTail, RED, FQ queuing algorithms

  • Mobility Models Built-in support for wireless and mobile network simulation

  • Trace Analysis Generates detailed trace files for performance analysis

Conclusion

NS2 combines C++ simulation engine with Otcl scripting interface to provide powerful network simulation capabilities. Understanding basic Otcl commands and NS2 architecture enables effective simulation of various network scenarios and protocol implementations.

Updated on: 2026-03-16T23:36:12+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements