Skip to content

Instantly share code, notes, and snippets.

@digi0ps
Last active March 31, 2018 17:03
Show Gist options
  • Save digi0ps/36bf67b97ec68249350a9580ab109c69 to your computer and use it in GitHub Desktop.
Save digi0ps/36bf67b97ec68249350a9580ab109c69 to your computer and use it in GitHub Desktop.
A TCL tutorial
# variable and printing
set x 10
set y 25
set z [expr $x*$y]
puts $x
puts "x + y = [expr $x+$y]"
# conditional
if { $x > 9 } {
puts "Yes"
} else {
puts "No"
}
# while loop
while { $x <= 100 } {
puts $x
set x [expr $x + 30]
}
# for loop
for {set a 0} {$a < 5} {incr a} {
puts $a
}
# array
set arr(0) 5
set arr(1) 10
puts "$arr(0) $arr(1)"
# associative array
set strarr("name") "sriram"
puts $strarr("name")
# procedures that is functions
proc min {arg1 arg2} {
if { $arg1 > $arg2 } {
return $arg2
}
return $arg1
}
puts "[min 10 20]"
# global modifier ( used inside if or proc)
global x 20
# Files
# File modes: r w a r+ w+
set f [open "a.txt" w]
puts $f "Wassup"
close $
set f [open "a.txt" r]
gets $f readed
puts $readed
close $f
# Simulation Script
# Create a new simulator object
set ns [new Simulator]
# Specify a file to store all simulation results
set tr [open "out.tr" w]
$ns trace-all $tr
# Specify a file to store all animator results
set nam [open "out.nam" w]
$ns namtrace-all $nam
# Create nodes
set $n1 [new node]
set $n2 [new node]
# Create a link
# $ns duplex-link $node1 $node2 Speed Delay QueueType (usually droptail)
$ns duplex-link $n1 $n2 2Mb 4ms DropTail
# Set queue size
$ns duplex-link-op $n1 $n2 queueSize 10
# Now you need to create agents and attach to the nodes
set tcp1 [new Agent/TCP]
set tcpsink [new Agent/TCPSink]
$ns attach-agent $n1 $tcp1
$ns attach-agent $n2 $tcpsink
$ns connect $n1 $n2
# We have to attach a application to agents to send data
set ftp [new Application/FTP]
$ftp attach-agent tcp1
# To start simulation
$ns at 1.0 "$ftp start"
$ns at 2.0 "$ftp stop"
$ns at 2.1 "Finishing simulation"
proc finish() {
global ns tr nam
$ns flush-all
close $tr
close $nam
exec nam out.nam &
exit
}
$ns run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment