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
- Set Up NS2
- 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.
- Verify Installation:
- We can execute the below command for installation:
ns
-
- We should monitor the NS2 prompt (%).
- 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.
- 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
- Run the Simulation
- We will need to store the script like example.tcl.
- Execute the script to utilise below code:
ns example.tcl
- Go to the network animation within NAM (Network Animator):
nam out.nam
- 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.
- 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
- 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
- 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
- Ad-Hoc Networks:
- Execute the networks like AODV, DSDV, or DSR routing protocols.
- Mimic mobility scenarios.
- Wireless Sensor Networks:
- Design the power-efficient interaction in wireless networks.
- Traffic Management:
- Examine TCP/UDP traffic in jamming.
- 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.