Monday, 15 December 2014

how to simulate aloha routing protocol using NS2

set ns [new Simulator]
$ns color 1 Blue
$ns color 2 Red

set file1 [open aloha.tr w]
set winfile [open WinFile w]
$ns trace-all $file1

set file2 [open aloha.nam w]
$ns namtrace-all $file2

proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec nam aloha.nam &
exit 0
}

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

$n1 color red
$n1 shape box

$ns duplex-link $n0 $n1 2Mb 3ms DropTail
$ns duplex-link $n0 $n2 2Mb 3ms DropTail
$ns simplex-link $n1 $n3 0.3Mb 10ms DropTail
$ns simplex-link $n3 $n1 0.3Mb 10ms DropTail
$ns simplex-link $n2 $n4 0.3Mb 10ms DropTail
$ns simplex-link $n4 $n2 0.3Mb 10ms DropTail
$ns simplex-link $n3 $n5 0.3Mb 10ms DropTail
$ns simplex-link $n4 $n5 0.3Mb 10ms DropTail

set lan [$ns newLan "$n3 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/sat/Unslotted Aloha Channel]

set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552

set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP



set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2


set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 124.0 "$ftp stop"
$ns at 124.5 "$cbr stop"
$ns at 125.0 "finish"
$ns run






No comments:

Post a Comment