NS2 Tcl Script - Detailed Explanation
Complete Tcl Script
# Create simulator
set ns [new Simulator]
# Open trace file and NAM file
set ntrace [open prog1.tr w]
$ns trace-all $ntrace
set namFile [open prog1.nam w]
$ns namtrace-all $namFile
# Finish procedure
proc finish {} {
global ns ntrace namFile
$ns flush-trace
close $ntrace
close $namFile
exec nam prog1.nam &
exec echo "The number of packet drops is " &
exec grep -c "^d" prog1.tr &
exit 0
}
# Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
# Label the nodes
$n0 label "TCP Source"
$n1 label "Router"
$n2 label "TCP Sink"
# Create links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 500Kb 10ms DropTail
# Queue limit
$ns queue-limit $n1 $n2 10
# TCP agent and sink
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink
$ns connect $tcp $sink
# Application (FTP over TCP)
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
# Flow coloring
$ns color 1 Blue
$tcp set fid_ 1
# Scheduling events
$ns at 0.10 "$ftp start"
$ns at 4.50 "$ftp stop"
$ns at 5.00 "finish"
# Run
$ns run
Line-by-Line Explanation
set ns [new Simulator]
Creates a new simulator object, which controls all aspects of the simulation.
set ntrace [open prog1.tr w]
Opens a trace file (prog1.tr) in write mode to record packet events.
$ns trace-all $ntrace
Instructs NS2 to log all events into the trace file.
set namFile [open prog1.nam w]
Opens a file (prog1.nam) in write mode for network animation (NAM).
$ns namtrace-all $namFile
Enables logging of simulation events into the NAM file.
proc finish {}
Defines a procedure that is called when the simulation ends.
global ns ntrace namFile
Allows procedure access to the simulator and file variables.
$ns flush-trace
Ensures all remaining events are written into trace files.
close $ntrace / close $namFile
Closes the trace and NAM files properly.
exec nam prog1.nam &
Automatically starts the NAM animation tool after simulation ends.
exec echo ... / exec grep -c "^d" prog1.tr &
Displays the number of packets dropped (lines starting with 'd').
exit 0
Ends the program execution.
set n0 [$ns node] ...
Creates three nodes: TCP source (n0), router (n1), and TCP sink (n2).
$n0 label ...
Labels the nodes for easier visualization in NAM.
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
Creates a duplex link between n0 and n1 (1Mb bandwidth, 10ms delay, DropTail queue).
$ns duplex-link $n1 $n2 500Kb 10ms DropTail
Creates a bottleneck link between n1 and n2 (500Kb bandwidth, 10ms delay).
$ns queue-limit $n1 $n2 10
Limits the queue size at the n1→n2 link to 10 packets (excess packets are dropped).
set tcp [new Agent/TCP]
Creates a TCP agent that will generate traffic from the source node.
$tcp set class_ 2
Sets the TCP flow class (used for coloring in NAM).
$ns attach-agent $n0 $tcp
Attaches the TCP agent to node n0.
set sink [new Agent/TCPSink]
Creates a TCP sink agent (receiver).
$ns attach-agent $n2 $sink
Attaches the TCP sink to node n2.
$ns connect $tcp $sink
Connects the TCP source agent to the TCP sink.
set ftp [new Application/FTP]
Creates an FTP application that will use the TCP connection.
$ftp attach-agent $tcp
Attaches FTP to the TCP agent.
$ftp set type_ FTP
Configures the application to behave like FTP.
$ns color 1 Blue / $tcp set fid_ 1
Assigns a flow ID (1) to the TCP connection and colors it blue in NAM.
$ns at 0.10 "$ftp start"
Schedules FTP traffic to start at 0.10 seconds.
$ns at 4.50 "$ftp stop"
Schedules FTP traffic to stop at 4.50 seconds.
$ns at 5.00 "finish"
Calls the finish procedure at 5.00 seconds.
$ns run
Starts running the simulation.
Network Topology