Ns 2 Prefinal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

# create a sim object set ns [new Simulator]

# creating a trace file set tracefile [open a2.tr w]


$ns trace-all $tracefile

# create nam file set nam [open a2.nam w]


$ns namtrace-all $nam

# Number of nodes (excluding the switch) set num_nodes 30 set num_switch 10

# create nodes for {set i 0} {$i < $num_nodes} {incr i} { set node($i) [$ns node]
}
set n0 [$ns node]

# Create switch node with an internal buffer size of 10 packets


#for {set i 0} {$i < $num_switch} {incr i} {
# set s($i) [$ns node]
#}

set switch [$ns node] $switch set queueLimit_ 10 ;

# create links for {set i 0} {$i < $num_nodes} {incr i} {


$ns duplex-link $node($i) $switch 100Mb 10ms DropTail }
#for {set i 0} {$i < $num_switch} {incr i} {
# $ns duplex-link $switch $s($i) 100Mb 1ms DropTail
#}
#for {set i 0} {$i < $num_switch} {incr i} {
# $ns duplex-link $s($i) $n0 60kb 10ms DropTail
#}

for {set i 0} {$i < $num_switch} {incr i} {


set random_node [expr {int(rand()*$num_switch)}] ;
$ns duplex-link $switch $node($random_node) 60Kb 10ms DropTail
$ns duplex-link $node($random_node) $n0 100Mb 10ms DropTail
}

# Creating agents and applications for {set i 0} {$i < $num_nodes} {incr i} {
set udp($i) [new Agent/UDP] $ns attach-agent $node($i) $udp($i)
set cbr($i) [new Application/Traffic/Exponential] $cbr($i) set packetSize_ 750 ;# Mean packet size in
bytes
$cbr($i) set burst_time_ 1.0 ;# Mean interarrival time in seconds
$cbr($i) attach-agent $udp($i)
}
set null [ new Agent/Null] $ns attach-agent $n0 $null

for {set i 0} {$i < $num_nodes} {incr i} {


$ns connect $udp($i) $null
$ns at 1.0 "$cbr($i) start"
}
# Schedule finish procedure
$ns at 10.0 "finish"

# Finish procedure proc finish {} {


global ns tracefile nam $ns flush-trace exec nam
a2.nam & close $tracefile close $nam exit 0
}
puts "Simulation is starting..."
$nsghrun

You might also like