Computer Networks
Lab program 2
Implement transmission of ping messages/trace route over a network topology
consisting of 6 nodes and find the number of packets dropped due to congestion.
#create Simulator object
set ns [new Simulator]
#open trace file
set nt [open lab2.tr w]
$ns trace-all $nt
#open namtrace file
set nf [open lab2.nam w]
$ns namtrace-all $nf
#create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
#label nodes
$n0 label "ping0"
$n1 label "ping1"
$n2 label "ping2"
$n3 label "ping3"
$n4 label "ping4"
$n5 label "router"
#create links, specify the type, nodes, bandwidth, delay and ARQ algorithm for it
$ns duplex-link $n0 $n5 1Mb 10ms DropTail
$ns duplex-link $n1 $n5 1Mb 10ms DropTail
$ns duplex-link $n2 $n5 1Mb 10ms DropTail
$ns duplex-link $n3 $n5 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
#set queue length
$ns queue-limit $n0 $n5 5
$ns queue-limit $n1 $n5 5
$ns queue-limit $n2 $n5 2
$ns queue-limit $n3 $n5 5
$ns queue-limit $n4 $n5 2
$ns color 1 Cyan
$ns color 2 Red
$ns color 3 Blue
$ns color 4 Green
$ns color 5 Yellow
#define ‘recv’ function for class Agent/Ping
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from $from with round-trip time $rtt ms"
}
Agent/Ping → The Ping agent class in NS2.
instproc recv {from rtt} → Defines an instance procedure named recv which takes two
arguments:
from → ID of the sender node
rtt → Round Trip Time of the packet
$self instvar node_ → Retrieves the instance variable node_ (the node on which this agent
resides).
puts ... → Prints the message to the terminal/output trace.
It shows which node received the ping reply, from whom, and the RTT in ms.
Example
If node 0 pings node 1 and gets a reply
node 0 received ping answer from 1 with round-trip time 50 ms
#create ping agent and attach them to node
set p0 [new Agent/Ping]
$ns attach-agent $n0 $p0
$p0 set class_ 1
You created a Ping agent, attached it to node n0, and assigned it a class_ value of 1 so that in the
trace/NAM output, you can identify the ping traffic separately.
set p1 [new Agent/Ping]
$ns attach-agent $n1 $p1
$p1 set class_ 2
set p2 [new Agent/Ping]
$ns attach-agent $n2 $p2
$p2 set class_ 3
set p3 [new Agent/Ping]
$ns attach-agent $n3 $p3
$p3 set class_ 4
set p4 [new Agent/Ping]
$ns attach-agent $n4 $p4
$p4 set class_ 5
#connect 2 agents
$ns connect $p2 $p4
$ns connect $p3 $p4
You’re setting up multiple senders (p2, p3) targeting a single receiver ( p4) in your simulation. This is
how you simulate one-to-many ping communications.
proc sendPingPacket { } {
global ns p2 p3
set intervalTime 0.001
set now [$ns now]
$ns at [expr $now + $intervalTime] "$p2 send"
$ns at [expr $now + $intervalTime] "$p3 send"
$ns at [expr $now + $intervalTime] "sendPingPacket"
}
proc finish { } {
global ns nt nf
$ns flush-trace
close $nt
close $nf
exec nam lab2.nam &
exit 0
}
$ns at 0.1 "sendPingPacket" #“At simulation time = 0.1s, execute the command inside quotes.”
$ns at 2.0 "finish" #At simulation time 2.0 seconds, the simulator will run the finish procedure.
$ns run #the simulator’s event scheduler starts and the simulation actually runs.
Explanation
set intervalTime 0.001
Creates a variable intervalTime with value 0.001 (in seconds).
This is the time gap between two ping packets (i.e., 1 millisecond).
Smaller values (like 0.001) mean packets are generated very frequently.
You can increase this to 0.1 or 1.0 if you want less frequent pinging.
set now [$ns now]
$ns now → returns the current simulation time in seconds.
By default, at the beginning of the simulation, $ns now = 0.0.
As the simulation runs, $ns now updates to reflect the simulator’s clock.
So set now [$ns now] stores the current simulation time in the variable now.
$ns at [expr $now + $intervalTime] "$p2 send"
$ns at <time> <command>
Tells the NS2 simulator: “At simulation time <time>, execute <command>.”
[expr $now + $intervalTime]
Computes the future time.
If $now is 0.0 and $intervalTime is 0.001, then the command will run at simulation time
0.001s.
"$p2 send"
Tells the Ping agent $p2 to send one ping packet at that scheduled time.
$ns at [expr $now + $intervalTime] "sendPingPacket"
Here, at now + intervalTime, NS2 will execute the procedure sendPingPacket.