How to Start Networking Projects Using NS2

To start a networking project in Network Simulator 2 (NS2) that requires knowing the needs of projects, to configure the environment, and execute the network topology and protocols. NS2 environment is a robust tool for simulation in wired and wireless networks and it is frequently utilised within academic and research settings.

Steps to Start Networking Projects in NS2

  1. Set Up NS2
  1. Install NS2:
    • We should download the new version of NS2 on the system.
    • Install NS2 on a Linux environment like Ubuntu. Also, we will need to install it on Windows with the support of Cygwin or on macOS.
    • Confirm the installation by means of executing example scripts.
  2. Verify Installation:
    • We can execute the below command for installation:

ns

    • We should monitor the NS2 prompt (%).
  1. Understand Project Requirements

Following is networking project’s core modules:

  • Topology: Star, ring, mesh, bus, or hybrid.
  • Protocols: TCP, UDP, routing protocols such as AODV, DSDV, and so on.
  • Traffic Patterns: Constant Bit Rate (CBR), File Transfer Protocol (FTP), or custom traffic.
  1. Define the Network Topology

Create the network topology using Tcl (Tool Command Language) script. NS2 utilises Tcl for set up and scenario scripting.

Example: Simple Network Topology

# Create a simulator instance

set ns [new Simulator]

# Create trace files

set tracefile [open out.tr w]

set namfile [open out.nam w]

$ns trace-all $tracefile

$ns namtrace-all $namfile

# Define nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

# Define links

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

# Define traffic

set udp [new Agent/UDP]

$ns attach-agent $n0 $udp

set null [new Agent/Null]

$ns attach-agent $n2 $null

$ns connect $udp $null

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set interval_ 0.1

$cbr attach-agent $udp

# Start and stop simulation

$ns at 0.1 “$cbr start”

$ns at 5.0 “$cbr stop”

$ns at 6.0 “finish”

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}

# Run simulation

$ns run

  1. Run the Simulation
  1. We will need to store the script like example.tcl.
  2. Execute the script to utilise below code:

ns example.tcl

  1. Go to the network animation within NAM (Network Animator):

nam out.nam

  1. Analyze the Output
  • Trace File (out.tr):
    • It includes in-depth records logs of packet events like send, receive, and drop.
    • Examine it to utilise custom scripts or tools such as AWK or Python.
  • NAM Visualization:
    • Envision network events like packet flow, congestion using NAM for visualization.
  1. Add Advanced Features

Integrate the following aspects to prolong the simulation:

6.1. Routing Protocols

Mimic routing protocols such as AODV, DSDV, or DSR for ad-hoc networks.

$ns node-config -adhocRouting AODV

6.2. Wireless Topology

Describe the wireless nodes and set the mobility.

set n0 [$ns node]

$ns node-config -adhocRouting AODV -llType LL -macType Mac/802_11 -ifqType Queue/DropTail/PriQueue -ifqLen 50 \

-antType Antenna/OmniAntenna -propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy -channelType Channel/WirelessChannel

$ns set X 100

$ns set Y 100

$ns set Z 0

6.3. TCP Traffic

Integrate TCP agents and FTP traffic.

set tcp [new Agent/TCP]

set sink [new Agent/TCPSink]

$ns attach-agent $n0 $tcp

$ns attach-agent $n2 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns at 0.5 “$ftp start”

$ns at 5.0 “$ftp stop”

6.4. Queue Management

Replicate various queue disciplines such as DropTail, RED, or FQ.

$ns queue-limit $n0 $n1 10

$ns queue-limit $n1 $n2 20

  1. Analyze Results
  • Analyse the trace file for performance parameters such as throughput, latency, and packet loss.
  • For data analysis and visualization, we can utilise the external tools such as AWK, Python, or MATLAB.

Example AWK Script for Throughput:

awk ‘{

if ($1 == “r” && $4 == “tcp”) {

sum += $6

}

} END {

print “Throughput:”, sum / 1000, “kbps”

}’ out.tr

  1. Extend the Project
  • Mobility Models: Make use of setdest or directly set the node mobility.
  • Energy Models: Replicate the battery-powered nodes.
  • Advanced Protocols: Execute and experiment the advanced custom protocols.

Example Projects

  1. Ad-Hoc Networks:
    • Execute the networks like AODV, DSDV, or DSR routing protocols.
    • Mimic mobility scenarios.
  2. Wireless Sensor Networks:
    • Design the power-efficient interaction in wireless networks.
  3. Traffic Management:
    • Examine TCP/UDP traffic in jamming.
  4. QoS in Networks:
    • Experiment the QoS strategies for real-time and non-real-time traffic.

NS2 environment permit us to model, replicate and examine the Networking projects through given procedure that contains basic configuration and insert complexity as per project requirements with coding snippets. More innovative approach will also be presented in upcoming guide.