JustPaste.it

1. Simulate a three nodes point – to – point network with duplex links between 
them.Set the queue size and vary the bandwidth and find the number of packets 
dropped.
# create a new simulator
set ns [new Simulator]
# open trace and NAM trace file in write mode
set tf [open out.tr w]
$ns trace-all $tf
set nf [open out.nam w]
$ns namtrace-all $nf
##### Decide a topology #########
#
# [udp][cbr]
# [0]------
# | [null]
# [2]------[3]
# |
# [1]------
# [udp][cbr]
#
#################################
# create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# create duplex links between nodes
$ns duplex-link $n0 $n2 10Mb 300ms DropTail
$ns duplex-link $n1 $n2 10Mb 300ms DropTail
$ns duplex-link $n2 $n3 1Mb 300ms DropTail
# set up queue size
$ns queue-limit $n0 $n2 10
$ns queue-limit $n1 $n2 10
$ns queue-limit $n2 $n3 10
# setup udp connection for transport layer
set udp0 [new Agent/UDP]
set udp1 [new Agent/UDP]
set null3 [new Agent/Null]
$ns attach-agent $n0 $udp0
$ns attach-agent $n1 $udp1
$ns attach-agent $n3 $null3
# setup cbr(constant bit rate) over udp for application layer
set cbr0 [new Application/Traffic/CBR]
set cbr1 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr1 attach-agent $udp1
# connect source to destination
$ns connect $udp0 $null3
$ns connect $udp1 $null3
# set bandwidth (vary values for different output)
$cbr0 set packetSize_ 500Mb
$cbr1 set packetSize_ 500Mb
$cbr0 set interval_ 0.005
$cbr1 set interval_ 0.005
# define a finish procedure
proc finish {} {
global ns nf tf
$ns flush-trace
exec nam out.nam &
close $tf 
close $nf
set count 0
set tf [open out.tr r]
while {[gets $tf line] != -1} {
# d is event in the trace file which denotes dropped packets 
if { [string match "d*" $line] } {
set count [expr $count + 1]
}
}
puts "Number of packets dropped: $count"
exit 0
}
# schedule events
$ns at 0.01 "$cbr0 start"
$ns at 0.01 "$cbr1 start"
$ns at 5.0 "finish"
$ns run
############# output #############
# Number of packets dropped: 700
2. Simulate the different types of Internet traffic such as FTP and TELNET over
a network and analyze the throughput.
# create a new simulator
set ns [new Simulator]
# open trace and NAM trace file in write mode
set tf [open out.tr w]
$ns trace-all $tf
set nf [open out.nam w]
$ns namtrace-all $nf
######## Decide a topology #######
#
# [ftp]
# [tcp]
# [0]------ 
# | [sink0]
# [2]------[3]
# | [sink1]
# [1]------
# [tcp]
# [telnet]
#
##################################
# create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# create duplex links between nodes
$ns duplex-link $n0 $n2 2Mb 1ms DropTail
$ns duplex-link $n1 $n2 2Mb 1ms DropTail
$ns duplex-link $n2 $n3 2Mb 1ms DropTail
# set n0 and n1 as tcp source
set tcp0 [new Agent/TCP]
set tcp1 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n1 $tcp1
# set n3 as tcp destination for n0 and n1
set TCPS0 [new Agent/TCPSink]
set TCPS1 [new Agent/TCPSink]
$ns attach-agent $n3 $TCPS0
$ns attach-agent $n3 $TCPS1
# set ftp over tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
#set telnet over tcp1
set tel1 [new Application/Telnet]
$tel1 attach-agent $tcp1
$tel1 set packetSize_ 500Mb
$tel1 set interval_ 0.001
# connect source to destination
$ns connect $tcp0 $TCPS0
$ns connect $tcp1 $TCPS1
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam out.nam &
close $tf
close $nf
# because time difference between start and finish is 2
set time 2
set fCount 0
set tCount 0
set tf [open out.tr r]
while {[gets $tf line] != -1} {
if { [string match "*tcp*0.0*3.0*" $line] } {
set fCount [expr $fCount + 1]
}
if { [string match "*tcp*1.0*3.1*" $line] } {
set tCount [expr $tCount + 1]
}
}
puts "Throughput of FTP: [expr $fCount/$time]"
puts "Throughput of TELNET: [expr $tCount/$time]"
exit 0
}
# schedule events
$ns at 0.01 "$ftp0 start"
$ns at 0.01 "$tel1 start"
$ns at 2.01 "finish"
$ns run
############### output #################
# No of FTP packets: 767
# No of TELNET packets: 750
 3. Simulate an Ethernet LAN using n nodes (6-10), change error rate and data 
rate and compare the throughput.
# Declare a new Simulator
set ns [new Simulator]
# Open nam and trace file in write mode
set tf [open out.tr w]
set nf [open out.nam w]
$ns trace-all $tf
$ns namtrace-all $nf
# Take value of error rate and data rate from std input
puts "Enter error rate (<1) : " 
gets stdin erate
puts "Enter data rate (in Mbps) : "
gets stdin drate
############## Select a topology #####################
# [udp1] duplex-link
#. [n0] [n1] [n2] [n3]-------------
# | | | | |
# | | | | |
# -------------------------------lan7 |
# |
# -------------------------------lan8 |
# | | | |
# | | | |
# [n4] [n5] [n6]----------------
# [null5]
#####################################################
# 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]
set n6 [$ns node]
# set label and color (OPTIONAL)
$n1 label "udp/source"
$n5 label "udp/null"
$n0 color "blue"
$n1 color "blue"
$n2 color "blue"
$n3 color "blue"
$n4 color "red"
$n5 color "red"
$n6 color "red"
# Create two lans
$ns make-lan "$n0 $n1 $n2 $n3" 10Mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n4 $n5 $n6" 10Mb 10ms LL Queue/DropTail Mac/802_3
# Setup Links
$ns duplex-link $n3 $n6 10Mb 10ms DropTail
# Declare the transport layer protocols
set udp1 [new Agent/UDP]
set null5 [new Agent/Null]
$ns attach-agent $n1 $udp1
$ns attach-agent $n5 $null5
# Declare the application layer protocol
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
# Connect the source and destination
$ns connect $udp1 $null5
# Create error model
set err [new ErrorModel]
$ns lossmodel $err $n3 $n6
$err set rate_ $erate
# Define the data rate
$cbr1 set packetSize_ $drate.Mb
$cbr1 set interval_ 0.001
# Define procedure
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam out.nam &
close $nf
close $tf
set count 0
set tr [open out.tr r]
while {[gets $tr line] != -1} {
# 8 denotes LAN at destination side and 5 denotes destination node
if {[string match "* 8 5 *" $line]} {
set count [expr $count+1]
}
}
set thr [expr $count/5]
puts "Throughput : $thr"
exit 0
}
$ns at 0.1 "$cbr1 start"
$ns at 5.1 "finish"
$ns run
4. Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and 
determine the collision across different nodes.
# Declare a new Simulator
set ns [new Simulator]
# Open the trace and nam file in write mode
set tf [open out.tr w]
set nf [open out.nam w]
$ns trace-all $tf
$ns namtrace-all $nf
# Decide the topology: [tcp(0->2)], [udp(2->1)], [tcp(1->3)]
#
# [tcp0] [tcp1][null1]
# [n0] [n1]
# | |
# | |
# ------------------------------ lan4
# | |
# | |
# [n3] [n2]
# [sink3] [udp2][sink2]
# Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create lan and setup the link
$ns make-lan -trace on "$n0 $n1 $n2 $n3" 100Mb 10ms LL Queue/DropTail Mac/802_3
# Declare the required transport layer Protocols
set tcp0 [new Agent/TCP]
set tcp1 [new Agent/TCP]
set udp2 [new Agent/UDP]
set null1 [new Agent/Null]
set sink2 [new Agent/TCPSink]
set sink3 [new Agent/TCPSink]
# Attach these Protocols to their respective nodes
$ns attach-agent $n0 $tcp0
$ns attach-agent $n1 $tcp1
$ns attach-agent $n2 $udp2
$ns attach-agent $n1 $null1
$ns attach-agent $n2 $sink2
$ns attach-agent $n3 $sink3
# Declare Application layer protocols and attach them with their transport layer protocols
set ftp0 [new Application/FTP]
set ftp1 [new Application/FTP]
set cbr2 [new Application/Traffic/CBR]
$ftp0 attach-agent $tcp0
$ftp1 attach-agent $tcp1
$cbr2 attach-agent $udp2
# connect source to destination
$ns connect $tcp0 $sink2
$ns connect $udp2 $null1
$ns connect $tcp1 $sink3
# set the interval 
$ftp0 set interval_ 0.001
$ftp1 set interval_ 0.001
$cbr2 set interval_ 0.01
# define finish procedure
proc finish {} {
global ns nf tf
$ns flush-trace
exec nam out.nam &
close $tf
close $nf
set count 0
set tr [open out.tr r]
while {[gets $tr line] !=-1 } {
if { [string match "c*" $line] } {
set count [expr $count + 1]
}
}
puts "No of packets collided: $count"
exit 0
}
# schedule the events
$ns at 0.1 "$cbr2 start"
$ns at 0.1 "$ftp0 start"
$ns at 0.1 "$ftp1 start"
$ns at 5.0 "finish"
$ns run
############ output ############
# No of packets collided: 242
5. Simulate the transmission of ping messages over a network topology 
consisting of 6 nodes and find the number of packets dropped due to congestion.
# Declare new Simulator
set ns [new Simulator]
# Open trace and nam file in write mode
set tf [open out.tr w]
set nf [open out.nam w]
$ns trace-all $tf
$ns namtrace-all $nf
# Decide the topology
#
# [s0][ping] [ping] [ping]
# [n0] [n1] [n3]
# '. | .'
# '. | .'
# '. | .'
# '. | .'
# '. | .'
# [n2]
# .' | '.
# .' | '.
# .' | '.
# .' | '.
# .' | '.
# .' | '.
# [n4] [n5] [n6]
# [ping][d0] [s1][ping] [ping][d1]
# Create the 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]
set n6 [$ns node]
# set up links
$ns duplex-link $n0 $n2 100Mb 300ms DropTail
$ns duplex-link $n5 $n2 100Mb 300ms DropTail
$ns duplex-link $n1 $n2 100Mb 300ms DropTail
$ns duplex-link $n3 $n2 100Mb 300ms DropTail
$ns duplex-link $n2 $n4 100Mb 300ms DropTail
$ns duplex-link $n2 $n6 100Mb 300ms DropTail
# Declare the agents/protocols
set ping0 [new Agent/Ping]
set ping4 [new Agent/Ping]
set ping5 [new Agent/Ping]
set ping6 [new Agent/Ping]
# Attach the ping with the respective nodes
$ns attach-agent $n0 $ping0
$ns attach-agent $n4 $ping4
$ns attach-agent $n5 $ping5
$ns attach-agent $n6 $ping6
# Connect the ping from source to destination
$ns connect $ping0 $ping4
$ns connect $ping5 $ping6
# Write proc for ping agent
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "The node [$node_ id] recieved $from with round trip time $rtt"
}
# Write the proc function
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam out.nam &
close $nf
close $tf
set count 0
set tr [open out.tr r]
while {[gets $tr line]!=-1} {
if {[string match "d*" $line]} {
set count [expr $count + 1]
}
}
puts "No. of packet dropped : $count"
exit 0
}
# shut down link between n2 and n6 for 1 sec(2 - 1 = 1) to show packet dropping
$ns rtmodel-at 1 down $n2 $n6
$ns rtmodel-at 2 up $n2 $n6
# schedule events
for {set i 0.1} {$i<2} {set i [expr $i+0.1]} {
 $ns at $i "$ping0 send"
 $ns at $i "$ping5 send"
}
$ns at 5.0 "finish"
$ns run
6. A Simple ESS with transmitting nodes in Wireless LAN
7. A simple ad-hoc network with transmitting nodes
# Declare new Simulator
set ns [new Simulator]
# Open the trace file in write mode
set tf [open out.tr w]
$ns trace-all $tf
# Set name-trace for wireless network
set nf [open out.nam w]
$ns namtrace-all-wireless $nf 500 500
# Set new topography
set topo [new Topography]
$topo load_flatgrid 500 500
# Configure for a wireless node.
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF 
# Create a god object
create-god 3
################### Decide the topology ###############
# 500
# |
# 400 [sink2]
# | [n2]
# | .'
# | .'
# | .'
# | .'
# | .'
# | .'
# 100 [n1]
# | .' [sink1]
# | .' [tcp1]
# 10 [n0] [ftp1]
# | [tcp0]
# | [ftp0]
# |
# |____10______100_______________400_________500
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
# Locate the nodes on load_flatgrid
$n0 set X_ 10
$n0 set Y_ 10
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0
$n2 set X_ 400
$n2 set Y_ 400
$n2 set Z_ 0
# initial state
$ns at 0.0 "$n0 setdest 10 10 15"
$ns at 0.0 "$n1 setdest 100 100 15"
$ns at 0.0 "$n2 setdest 400 400 15"
# move n1 near to node n2 at 50s and come back near to node n0 at 100s
$ns at 50 "$n1 setdest 300 300 15"
$ns at 100 "$n1 setdest 100 100 15"
# Declare and attach transport layer protocol
set tcp0 [new Agent/TCP]
set tcp1 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n1 $tcp1
set sink1 [new Agent/TCPSink]
set sink2 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns attach-agent $n2 $sink2
# Declare and attach appliction layer protocol
set ftp0 [new Application/FTP]
set ftp1 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp1 attach-agent $tcp1
# connect source to destination
$ns connect $tcp0 $sink1
$ns connect $tcp1 $sink2
proc finish { } {
 global ns nf tf
 $ns flush-trace
 exec nam out.nam &
 close $tf
 set ctr1 0
 set ctr2 0
 set tf [open out.tr r]
 
 while {[gets $tf line] != -1} {
 # r->received, _1_ -> destination node
 if {[string match "r*_1_*AGT*" $line]} {
 set ctr1 [expr $ctr1 + 1]
 }
 if {[string match "r*_2_*AGT*" $line]} {
 set ctr2 [expr $ctr2 + 1]
 }
 }
 puts "\nThroughput from n0 to n1: $ctr1"
 puts "Throughput from n1 to n2: $ctr2"
 exit 0
}
# schedule events
# start ftp traffic
$ns at 1 "$ftp0 start"
$ns at 1 "$ftp1 start"
$ns at 150 "finish"
$ns run