Skip to content

Instantly share code, notes, and snippets.

@vanjikumaran
Created August 12, 2013 10:22
Show Gist options
  • Save vanjikumaran/6209734 to your computer and use it in GitHub Desktop.
Save vanjikumaran/6209734 to your computer and use it in GitHub Desktop.
# Experimention :- TCP flow vs UDP flow
# Experimenter :- Sivajothy Vanjikumaran |
#Create a simulator object
set ns [new Simulator]
#Colors Define
$ns color 1 brown
$ns color 2 green
#Save throughput for the First tcp flow
set f1 [open tcp-tcp_Exp1.tr w]
#Save throughput for the Second tcp flow
set f2 [open tcp-udp_Exp2.tr w]
#Open the nam trace file
set nf [open tcp_udp_nam.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]
#Links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
$ns duplex-link $n3 $n5 1Mb 10ms DropTail
# Making a bottle neck connection
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
#
#(0)- -(4)
# \ /
# (2)-----(3)
# / \
#(1)- -(5)
#
#layout of the data transferring path
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down
#Queue limit between node n2 & n3 (Bottle neck)
$ns queue-limit $n2 $n3 25
#Creation of TCP agent and attach it to node n0
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
# Max bound on window size
$tcp set window_ 10
# Set flow ID field
$tcp set fid_ 1
#Creation of a UDP agent and attach it to node n1
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
# max bound on window size
$udp set window_ 10
# set flow ID field
$udp set fid_ 2
#Creation of TCP sinks agents
set sink1 [new Agent/TCPSink]
#Creation of UDP Reciver Agent
set sink2 [new Agent/LossMonitor]
#Attach Sinks to nodes
$ns attach-agent $n4 $sink1
$ns attach-agent $n5 $sink2
$ns connect $tcp $sink1
$ns connect $udp $sink2
#Creation of FTP applications + attach to agents
set ftp [new Application/FTP]
$ftp attach-agent $tcp
#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
$cbr set rate_ 1mb
$cbr set random_ false
#Define a 'finish' procedure
proc finish {} {
global ns
$ns flush-trace
puts "running nam..."
exec nam -a tcp_udp_nam.nam &
exec xgraph tcp-tcp_Exp1.tr tcp-udp_Exp2.tr -geometry 800x400+10+10 -x "Time -ms" -y "Throughtput -Kbps" -t "Vanjikumaran's TCP-UDP Experiment" &
exit 0
}
proc record {} {
global sink1 sink2 ns f1 f2
#Set the time the procedure should be called again
set time 0.1
set bw0 [$sink1 set bytes_]
set bw1 [$sink2 set bytes_]
#Get the current time
set now [$ns now]
# throughtput in Kbps
puts $f1 "$now [expr ($bw0 * 8) / ($time * 1024)]"
puts $f2 "$now [expr ($bw1 * 8) / ($time * 1024)]"
$sink1 set bytes_ 0
$sink2 set bytes_ 0
#Call procedure again
$ns at [expr $now + $time] "record"
}
$ns at 0.0 "record"
$ns at 0.2 "$ftp start"
$ns at 0.4 "$cbr start"
$ns at 2.0 "$ftp stop"
$ns at 2.0 "$cbr stop"
$ns at 2.2 "finish"
$ns run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment