Ns 2 tutorial
1 / 10

NS-2 Tutorial - PowerPoint PPT Presentation

  • Uploaded on

NS-2 Tutorial. CS556 Jan 22th, 2004. Figure 1 :Simplified User's View of NS. OTCL. C++ and OTcl: The Duality. Export C++ class to OTcl. Reference: http://nile.wpi.edu/NS/linkage.html. Export C++ Object Control Commands to OTcl.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'NS-2 Tutorial' - babu

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Ns 2 tutorial

NS-2 Tutorial


Jan 22th, 2004

Figure 1 simplified user s view of ns
Figure 1:Simplified User's View of NS

C and otcl the duality
C++ and OTcl: The Duality

Export C++ class to OTcl



Export C++ Object Control Commands to OTcl.

For a complete ns class hierarchy visit http www sop inria fr rodeo personnel antoine clerget ns
For a complete NS class hierarchy, visit http://www-sop.inria.fr/rodeo/personnel/Antoine.Clerget/ns.

Ns 2 tutorial

  • #Schedule events for the CBR and FTP agents

  • $ns at 0.1 "$cbr start"

  • $ns at 1.0 "$ftp start"

  • $ns at 4.0 "$ftp stop"

  • $ns at 4.5 "$cbr stop"

  • #Detach tcp and sink agents (not really necessary)

  • $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

  • #Call the finish procedure after 5 seconds of simulation time

  • $ns at 5.0 "finish"

  • #Print CBR packet size and interval

  • puts "CBR packet size = [$cbr set packet_size_]"

  • puts "CBR interval = [$cbr set interval_]"

  • #Run the simulation

  • $ns run

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

$ns queue-limit $n2 $n3 10

#Give node position (for NAM)

$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

#Monitor the queue for link (n2-n3). (for NAM)

$ns duplex-link-op $n2 $n3 queuePos 0.5

#Setup a TCP connection

set tcp [new Agent/TCP]

$tcp set class_ 2

$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

$cbr set rate_ 1mb

$cbr set random_ false

#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 Trace file

set tf [open out.tr w]

$ns trace-all $tf

#Define a 'finish' procedure

proc finish {} {

global ns nf tf

$ns flush-trace

#Close the NAM trace file

close $nf

#Close the Trace file

close $tf

#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 1.7Mb 20ms DropTail

Ns 2 tutorial

#script: myflowcalcall.awk

# a general script for computing throughput


arguments = 2;

if (ARGC < arguments || ARGC > arguments || flowtype == 0) {

printf("error: wrong number of arguments.\nawk: usage - awk -f flowcalc.awk [-v graphgran=value] [-v fidfrom=value] [-v fidto=value] [-v fid=value] -v flowtype=\"type\" -v outdata_file=\"filename\" indata_file\n--%d",ARGC);



measure_interval = 0.5;

bits = 0;

first_time = graphgran;



if (($1 == "r") &&

((fidfrom == 0 && fidto ==0) || (($8 == fid) && ($3 == fidfrom) && ($4 == fidto))) &&

(flowtype == "all" || flowtype == $5)) {

if (($2 - first_time) > measure_interval) {

first_time = first_time + measure_interval;

rate = (bits/1000000)/first_time;

print filename first_time, rate;


bits = bits + $6 * 8;




measure_interval = 0.5;

first_time = first_time + measure_interval;

rate = (bits/1000000)/first_time;

print filename first_time, rate;


#Script: computeall

#compute throughput so far for a certain flow

awk -f myflowcalcall.awk -v graphgran=0 -v fidfrom=2 -v fidto=3 -v fid=1 -v flowtype="tcp" -v outdata_file="nothing" out.tr >thr1

echo "Compute flow1 throughput all and output to thr1"

awk -f myflowcalcall.awk -v graphgran=0 -v fidfrom=2 -v fidto=3 -v fid=2 -v flowtype="cbr" -v outdata_file="nothing" out.tr >thr2

echo "Compute flow2 throughput all and output to thr2"


#Filename: gscript

# Before loading this file,

# gnuplot>set term windows (x11 for unix)

# Gnuplot script file for plotting data

# in file "force.dat"

# Gnuplot script file for plotting data in files

# This file is called force.p

set title "One CBR Competes with One FTP"

set xlabel "Time in second"

set ylabel "Throughput in Mbps"

set key 3,1.8

# set label "Yield Point" at 0.003,260

# set arrow from 0.0028,250 to 0.003,280

set xr [0:5]

set yr [0:2]

plot "thr1" using 1:2 title 'CBR Flow' with lines, \

"thr2" using 1:2 title 'TCP Flow' with lines

#Filename: saveplot

#Before loading this file

#gnuplot>set out myfig.ps (output file)

# File name: saveplot - saves a plot as #a PostScript file

# to save the current plot as a #postscript file issue the commands:

# gnuplot> set out 'plotfile.ps'

# gnuplot> load 'saveplot'

set size 1.0, 0.4

set terminal postscript portrait \ enhanced mono lw 2 "Helvetica" 14


set terminal x11

set size 1,1

Build ns
Build NS


Linux: first build and instal the source packages of tcl, tk and otcl. Then download a snapshot of ns-2 and build it.

I used to try ns-allinone but failed.

Cygwin: download the allinone package for Cygwin and build it.

Reference: (where most of my materials come from)



Good to look at ~/ns-2-root/tcl/lib/ns-default.tcl, which is the init file for ns.