Monday, 3 October 2011

NS2 Codes for TCP and UDP Data Rate Experiment With 4 Pissible Nodes

#Create a simulator object

set ns [new Simulator]

#Define different colors for data flows (for NAM)

$ns color 1 Blue
$ns color 2 Red

#Open the NAM trace file

set nf [open out.nam w]
$ns namtrace-all $nf

#Open the tr trace file

set nf [open out.tr w]
$ns trace-all $nf


#Define a 'finish' procedure

proc finish {} {
        global ns nf
        $ns flush-trace
        #Close the NAM trace file
        close $nf
        #Execute NAM on the trace file
        exec nam out.nam &
        exit 0
}

#Create four nodes

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


#Create links between the nodes

$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 .25Mb 20ms DropTail


#Set Queue Size of link (n2-n3) to 10

$ns queue-limit $n2 $n3 10


#Setup a TCP connection

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink

$ns connect $tcp $sink
$tcp set fid_ 1


 #Setup a FTP over TCP connection

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


#Setup a UDP connection

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

#Setup a CBR over UDP connection

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000 
#Schedule events for the CBR and FTP agents

$ns at 0.1 "$cbr start"
$ns at .5 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 3.6 "$cbr stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"


#Run the simulation

$ns run
 

No comments:

Post a Comment