Experiment-1
Aim: Introduction To Network Simulation
(NS-2) Software.
THEORY
Network simulator 2, or Ns-2, is an open-source, discrete event-
driven network simulation program that is frequently used in
academic and research contexts to examine computer network
behaviour. It offers a versatile and potent framework for modelling
different network topologies and protocols. ns-2, which was
created as a component of the virtual internetwork testbed (VINT)
project, has proven crucial in assessing network applications and
protocols.
Key Features Of NS-2
1. Simulation of Various Network Protocol
- Supports both wired and wireless networking protocols.
- Simulates routing protocols (e.g., AODV, DSR) and transport protocols
(e.g., TCP, UDP).
2. Event-Driven Architecture
- Utilizes discrete-event simulation, where events like packet
transmission and reception are scheduled and processed in sequence.
3. Open Source:
- Available for free under the GNU General Public License (GPL).
- Extensible, enabling researchers to modify existing protocols or create
new ones.
4. Comprehensive Libraries
- Provides a broad range of pre-configured models for various OSI
layers, including physical, MAC, routing, and transport layers.
5. Support for Wired and Wireless Networks
- Models both wired connections (e.g., Ethernet) and wireless
environments (e.g., ad hoc, sensor, and mobile networks).
6. Trace Generation and Analysis
- Generates detailed trace files for post-simulation analysis.
- Compatible with visualization tools like NAM (Network Animator) and
trace analysis utilities.
7. Scripting and Extensibility
- Network scenarios are defined using TCL (Tool Command Language)
scripts.
- The simulation engine is implemented in C++, providing enhanced
performance and flexibility.
Architecture Of NS -2
NS-2’s architecture is comprised of three key components:
1. TCL Script Interpreter
- Enables users to define network topology, protocols, and simulation
parameters.
- Acts as the primary interface for creating and managing network
scenarios.
2. Simulation Engine
- Written in C++, it performs the core functions, such as event
scheduling, packet processing, and managing interactions between
network layers.
3. Network Animator (NAM)
- A visualization tool that graphically represents packet movement
and network activities during the simulation.
Applications Of NS-2
1. Protocol Development and Testing
- Allows for simulating new routing, transport, and MAC protocols
before deploying them in real-world scenarios.
2. Performance Evaluation
- Assesses the performance of existing protocols across various
network conditions and topologies.
3. Education
- Used as an educational tool to teach networking concepts,
algorithms, and protocols.
4. Research
- Widely used in academic and industrial research to study and
analyze network behavior.
Advantages Of NS-2
1. Comprehensive Protocol Support
- Supports a wide variety of protocols, enabling detailed simulation of
different network behaviors.
2. Extensibility
- Customizable and modifiable to meet specific research
requirements, allowing users to add or modify features as needed.
3. Visualization Support
- The graphical representation of simulations enhances
understanding and aids in debugging.
4. Community and Documentation
- Backed by a large community with extensive tutorials, research
papers, and active forums providing support.
Disadvantages Of NS-2
1. Complexity
- Has a steep learning curve due to the need to use both TCL for
simulation scripting and C++ for core modifications.
2. Performance Issues
- May struggle to scale efficiently when running very large
simulations.
3. Outdated Support
- Development has slowed, with a shift towards newer tools like ns-3
which may result in limited updates and community focus on ns-2.
Setting Up NS-2
1. System Requirements
- ns-2 is compatible with Linux-based systems.
- Can be installed on Windows through virtualization or using
compatibility layers like Cygwin.
2. Installation Steps (Linux)
- Download the source code from the official website or repository.
- Compile and install the software using `make` commands.
- Verify the installation by running sample TCL scripts.
3. Running a Simulation
- Create a TCL script to define the network topology, nodes,
protocols, and events.
- Run the script with the `ns` command.
- Analyze the generated trace files or use NAM for visualization of the
simulation results.
Experiment-2
Aim: Write A TCL Script To Create Two Nodes
And Link Nodes.
Between
Code:
#Creation Of Simulator Object
set ns [new simulator]
#Creation Of Output Files
set tracefile [open exp1.tr w]
$ns trace-all $tracefile
set namfile [open exp1.nam w]
$ns namtrace-all $namfile
#Creation Of
Nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "source node"
$n1 label "router"
$n2 label "destination node"
#Creation Of Links Between Nodes With Droptail Queue
$ns duplex-link $n0 $n1 100kb 2ms droptail
$ns duplex-link $n1 $n2 2mb 5ms droptail
#Creation Of Agents: TCP(Transmission Control
Protocol)
#TCP Agent
#Creation Of TCP Agent At Node N0 And Sink Agent At
Node N2
set tcp [new agent/tcp]
set sink [new agent/tcpsink]
#Attaching Of TCP Agent To Node N0(Source Node) And
Sink To Node
N2(Destination Node)
$ns attach-agent $n0 $tcp
$ns attach-agent $n2 $sink
#Virual Connections Between Two Agents:Tcp And Sink
$ns connect $tcp $sink
#Creation Of Application FTP (File Transfer Protocol,Ex-
Documents File
Between Nodes) After Creation Of Agents
#FTP application
set ftp [new application/ftp]
#Application FTP Is Attached To Agent TCP
$ftp attach-agent $tcp
#After Creation Of Application We Need To Start The
Traffic #Initialization Of Packetsize
$ftp set packetsize_ 200
#Define The Interval Of Packets
$ftp set interval_ 0.001
#Color Setup
$ns color 1 "red"
$tcp set class_ 1
#Start Traffic# At 1s I Want To Start Teraffic Of FTP
$ns at 0.2 "$ftp start"
# At 10s I Want To Finish Teraffic
$ns at 5.0 "finish"
#Defining The Finish Procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam
exp1.nam & exit 0
}
$ns run
Output :
Experiment-3
Aim Write A TCL Script To Transmit Data
:
Between Nodes.
Code:
#creation of simulator
object set ns [new
simulator]
#creation of output files
set tracefile [open exp2.tr w]
$ns trace-all $tracefile
set namfile [open exp2.nam w]
$ns namtrace-all $namfile
#creation of
nodes set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node]
$n0 label "source node"
$n1 label "router"
$n2 label "destination node"
#creation of links between nodes with droptail
queue
$ns duplex-link $n0 $n1 100kb 2ms droptail
$ns duplex-link $n1 $n2 2mb 5ms droptail
#creation of agents:udp(user datagram
protocol) #udpagent
#creation of udp agent at node n0 and null agent at
node n2
set udp [new agent/udp]
set null [new agent/null] #attaching of udp agent to node
n0(source node) and null to node
n2(destination node)
$ns attach-agent $n0 $udp
$ns attach-agent $n2 $null
#virual connections between two agents:udp and
null
$ns connect $udp $null
#creation of application cbr (constant bit rate)
after creation of agents
#ftp application
set cbr [new application/traffic/cbr]
#application cbr is attached to agent udp
$cbr attach-agent $udp
#after creation of application we need to start the
traffic #initialization of packetsize
$cbr set packetsize_ 300
#define the interval of packets
$cbr set interval_ 0.001
#color setup
$ns color 1 "blue"
$cbr set class_ 1
#start traffic
# at 1s i want to start teraffic of cbr
$ns at 0.2 "$cbr start"
# at 10s i want to finish teraffic
$ns at 5.0 “finish" #defining the finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam exp2.nam &
exit 0
}
$ns run
Output:
Experiment-4
Aim Write A TCL Script To Evaluate The
:
Performance Of Lan Topologies.
Various
Code:
#Creation Of Simulator Object
set ns [new simulator]
#Creation Of Output Files
set tracefile [open exp3.tr w]
$ns trace-all $tracefile
set namfile [open exp3.nam w]
$ns namtrace-all $namfile
#Creation Of
Nodes set n0 [$ns
node] set n1 [$ns
node] set n2 [$ns
node] set n3 [$ns
node]
$n0 label "tcp source node"
$n1 label "udp source node"
$n2 label "router"
$n3 label "destination"
#Creation Of Links Between Nodes With Droptail Queue
$ns duplex-link $n0 $n2 100kb 2ms droptail
$ns duplex-link $n1 $n2 2mb 2ms droptail
$ns duplex-link $n2 $n3 5mb 2ms droptail
#Creation Of Agents TCP(Transmission Control Protocol)
#Tcp Agent#Creation Of TCP Agent At Node N0 And Sink
Agent At Node N3
set tcp [new agent/tcp]
set sink [new agent/tcpsink]
#Attaching Of TCP Agent To Node N0(Source Node) And
Sink To Node
N3(Destination Node)
$ns attach-agent $n0 $tcp
$ns attach-agent $n3 $sink
#Virtual Connections Between Two Agents:TCP And Sink
$ns connect $tcp $sink
#Creation Of Application FTP (File Transfer Protocol,Ex-
Documents File
Between Nodes) After Creation Of Agents
#FTP Application
set ftp [new application/ftp]
#Application FTP Is Attached To Agent Tcp
$ftp attach-agent $tcp
#Udp Agent
#Creation Of UDP Agent At Node N1 And Null Agent At
Node N3
set udp [new agent/udp]
set null [new agent/null]
#Attaching Of UDP Agent To Node No And Null To Node N3
$ns attach-agent $n1 $udp
$ns attach-agent $n3 $null
#Virtual Connections Between Two Agents
$ns connect $udp $null
$ns connect $udp $null
#Creation Of Application CBR(Constant Bit Rate) After
Creation Of Agents
#FTP Application
set cbr [new application/traffic/cbr]
#Application CBR Is Attached To Agent UDP
$cbr attach-agent $udp
#Packet Size And Time Interval
$ftp set packetsize_ 200
$ftp set interval_ 0.001
$cbr set packetsize_ 300
$cbr set interval_ 0.001
#Color Setup
$ns color 1 "green"
$ns color 2 "red"
$tcp set class_ 1
$cbr set class_ 2
#After Creation Of Application We Need To Start The
Traffic #Start Traffic
# At 1s I Want To Start Teraffic # At 2s I Want To Start
Teraffic
$ns at 0.1 "$cbr start"
$ns at 0.11 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.0 "$cbr stop"
# At 10s I Want To Finish Teraffic
$ns at 10.0 "finish"
#Defining The Finish Procedure
proc finish {} {
global ns tracefile namfile
$ns flush-
trace close
$tracefile
close
$namfile
exec nam
exp3.nam &
exit 0
}
$ns run
Output:
Experiment-5
Aim Write A TCL Script ToEvaluate The
:
Performance Of Droptail And Red Queue
Manageme Schemes.
nt
Code:
#Creation Of Simulator
Object
set ns [new simulator]
#Creation Of Output Files
set tracefile [open exp4.tr w]
$ns trace-all $tracefile
set namfile [open exp4.nam w]
$ns namtrace-all $namfile
#Creation Of
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]
#Creation Of Links Between Nodes With Droptail
Queue
$ns duplex-link $n0 $n1 5mb 2ms droptail
$ns duplex-link $n1 $n2 10mb 5ms droptail
$ns duplex-link $n1 $n4 3mb 10ms droptail
$ns duplex-link $n4 $n3 100mb 2ms droptail
$ns duplex-link $n4 $n5 4mb 10ms droptail
#Creation Of Agents:Udp,Tcp
#Udp Agent#Creation Of Udp Agent At Node No And
Null Agent At Node N3
set udp [new agent/udp]
set null [new agent/null]
#Attaching Of Udp Agent To Node No And Null To
Node N3
$ns attach-agent $n0 $udp
$ns attach-agent $n3 $null
#Virtual Connections Between Two Agents
$ns connect $udp $null
#TCPAgent
#Creation Of TCP Agent At Node N2 And Sink Agent
At Node N5
set tcp [new agent/tcp]
set sink [new agent/tcpsink]
#Attaching Of TCP Agent To Node N2 And Sink To
Node N5
$ns attach-agent $n2 $tcp
$ns attach-agent $n5 $sink
#Virtual Connections Between Two Agents:TCP And
Sink
$ns connect $tcp $sink
#Creation Of Application Cbr(Constant Bit Rate,Ex-
Video,Mp3) And Ftp (File
Transfer Protocol,Ex-Documents File) After Creation
Of Agents
#CBR Application
set cbr [new application/traffic/cbr]
#Application CBR Is Attached To Agent Udp
$cbr attach-agent $udp
#Ftp Application
set ftp [New Application/FTP]
#Application FTP is attached to agent TCP
$ftp attach-agent $tcp
#After Creation Of Application We Need To Start The
Traffic #Start Traffic
# At 1s I Want To Start Traffic # At 2s I Want To
Start Traffic
$ns at 1.0 "$cbr start"
$ns at 2.0 "$ftp start"
# At 10s I Want To Finish Traffic
$ns at 10.0 "finish"
#Defining The Finish Procedure
proc finish {} {
global ns tracefile namfile
$ns flush-
trace close
$tracefile
close $namfile
exec nam
exp4.nam & exit 0
}
$ns run
Output:
Experiment-6
Aim Write a TCL Script To Create Scenario
: And
Study The Performance Of Star Topology
Through Simulation.
Code:
set ns [new
simulator] set nf
[open ex1.nam w]
#open the name
trace file set nf [open
ex1.nam w]
$ns namtrace-all $nf
#define a ‘finish’ procedure
proc finish {} {
global ns nf
$ns flush-trace
#close the trace
file close $nf
#executenam on the trace file
exec nam ex1.nam &exit 0
}
#create six
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]
#change the shape of center node in a star topology
$n0 shape square
#create links between the nodes
$ns duplex-link $n0 $n1 1mb 10ms droptail
$ns duplex-link $n0 $n2 1mb 10ms droptail
$ns duplex-link $n0 $n3 1mb 10ms droptail
$ns duplex-link $n0 $n4 1mb 10ms droptail
$ns duplex-link $n0 $n5 1mb 10ms droptail
#create a tcp agent and attach it to node n0
set tcp0 [new agent/tcp]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
#create a tcp sink agent (a traffic sink) for tcp and attach
it to node n3
set sink0 [new agent/tcpsink]
$ns attach-agent $n3 $sink0
#connect the traffic sources with the traffic sink
$ns connect $tcp0 $sink0
# create a cbr traffic source and attach it to tcp0
set cbr0 [new application/traffic/cbr]
$cbr0 set packetsize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0
#schedule events for the cbr agents
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#call the finish procedure after 5 seconds of simulation
time
$ns at 1.0 "finish"
#run the simulation
$ns run
Output:
Experiment-7
Aim Write A TCL Script To Simulate And To
:
Study About Go Back N Protocol And
Selective Repeat Using NS-2.
Program For Go Back N:
#send packets one by
one set ns [new
simulator] set n0 [$ns
node]
set n1 [$ns
node] set n2
[$ns node] set
n3 [$ns node]
set n4 [$ns
node] set n5
[$ns node]
$n0 color "red"
$n1 color "red"
$n2 color "green"
$n3 color "green"
$n4 color "black"
$n5 color "black"
$n0 shape circle ;
$n1 shape circle ;
$n2 shape circle ;
$n3 shape circle ;
$n4 shape circle ;
$n5 shape circle ;
$ns at 0.0 "$n0 label sys1"
$ns at 0.0 "$n1 label sys2"
$ns at 0.0 "$n2 label sys3"
$ns at 0.0 "$n3 label sys4"
$ns at 0.0 "$n4 label sys5"
$ns at 0.0 "$n5 label
sys6" set nf [open
srepeat.nam w]
$ns namtrace-all $nf
set f [open srepeat.tr
w]
$ns trace-all $f
$ns duplex-link $n0 $n2 1mb 10ms droptail
$ns duplex-link-op $n0 $n2 orient right-down
$ns queue-limit $n0 $n2 5
$ns duplex-link $n1 $n2 1mb 10ms droptail$ns duplex-link-op $n1
$n2 orient right-up
$ns duplex-link $n2 $n3 1mb 10ms droptail
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link $n3 $n4 1mb 10ms droptail
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link $n3 $n5 1mb 10ms droptail
$ns duplex-link-op $n3 $n5 orient right-down
agent/tcp set_nam_tracevar_true set
tcp [new agent/tcp]
$tcp set fid 1
$ns attach-agent $n1 $tcp
set sink [new
agent/tcpsink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
set ftp [new application/ftp]
$ftp attach-agent $tcp
$ns at 0.05 "$ftp start"
$ns at 0.06 "$tcp set windowlnit 8"
$ns at 0.06 "$tcp set maxcwnd 8"
$ns at 0.25 "$ns queue-limit $n3 $n4 0"
$ns at 0.26 "$ns queue-limit $n3 $n4 10"
$ns at 0.30 "$tcp set windowlnit 1"
$ns at 0.30 "$tcp set maxcwnd 1"
$ns at 0.30 "$ns queue-limit $n3 $n4 10"
$ns at 0.47 "$ns detach-agent $n1 $tcp;$ns detach-agent $n4 $sink"
$ns at 1.75 "finish"
$ns at 0.0 "$ns trace-annotate \"select and repeat\""
$ns at 0.05 "$ns trace-annotate \"ftp starts at 0.01\""
$ns at 0.06 "$ns trace-annotate \"send 8packets from sys1 to
sys4\""
$ns at 0.26 "$ns trace-annotate \"error occurs in 4th packet \""
$ns at 0.30 "$ns trace-annotate \"retransmit packet_4 from sys1 to
sys4\""
$ns at 1.5 "$ns trace-annotate \"ftp
stops\"" proc finish {} {
global ns nf
$ns flush-trace
close $nf
puts "filtering..."
#exec tclsh../bin/namfilter.tcl
srepeat.nam #puts "running nam..."
exec nam srepeat.nam &
exit 0
}
$ns run
Output:
Program For Selective Repeat:
#send packets one by
one set ns [new
simulator] set n0 [$ns
node]
set n1 [$ns
node] set n2
[$ns node] set
n3 [$ns node]
set n4 [$ns
node] set n5
[$ns node]
$n0 color "purple"
$n1 color "purple"
$n2 color "violet"
$n3 color "violet"
$n4 color "chocolate"
$n5 color "chocolate"
$n0 shape box ;
$n1 shape box ;
$n2 shape box ;
$n3 shape box ;
$n4 shape box ;
$n5 shape box ;
$ns at 0.0 "$n0 label sys0"
$ns at 0.0 "$n1 label sys1"
$ns at 0.0 "$n2 label sys2"
$ns at 0.0 "$n3 label sys3"
$ns at 0.0 "$n4 label sys4"
$ns at 0.0 "$n5 label
sys5" set nf [open
goback.nam w]
$ns namtrace-all $nf
set f [open goback.tr
w]
$ns trace-all $f
$ns duplex-link $n0 $n2 1mb 20ms droptail
$ns duplex-link-op $n0 $n2 orient right-down
$ns queue-limit $n0 $n2 5
$ns duplex-link $n1 $n2 1mb 20ms droptail
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link $n2 $n3 1mb 20ms droptail
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link $n3 $n4 1mb 20ms droptail
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link $n3 $n5 1mb 20ms droptail
$ns duplex-link-op $n3 $n5 orient right-down
agent/tcp set_nam_tracevar_true
set tcp [new agent/tcp]
$tcp set fid 1$ns attach-agent $n1 $tcp
set sink [new agent/tcpsink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
set ftp [new application/ftp]
$ftp attach-agent $tcp
$ns at 0.05 "$ftp start"
$ns at 0.06 "$tcp set windowlnit 6"
$ns at 0.06 "$tcp set maxcwnd 6"
$ns at 0.25 "$ns queue-limit $n3 $n4 0"
$ns at 0.26 "$ns queue-limit $n3 $n4 10"
$ns at 0.305 "$tcp set windowlnit 4"
$ns at 0.305 "$tcp set maxcwnd 4"
$ns at 0.368 "$ns detach-agent $n1 $tcp ; $ns detach-agent $n4
$sink"
$ns at 1.5 "finish"
$ns at 0.0 "$ns trace-annotate \"goback n end\""
$ns at 0.05 "$ns trace-annotate \"ftp starts at 0.01\""
$ns at 0.06 "$ns trace-annotate \"send 6packets from sys1 to
sys4\""
$ns at 0.26 "$ns trace-annotate \"error occurs for 4th packet so not
sent ack for the packet\""
$ns at 0.30 "$ns trace-annotate \"retransmit packet_4 to 6\""
$ns at 1.0 "$ns trace-annotate \"ftp
stops\"" proc finish {} {
global ns nf
$ns flush-trace
close $nf
puts "filtering..."
#exec tclsh../bin/namfilter.tcl goback.nam
#puts "running nam..."
exec nam goback.nam
& exit 0
}
$ns run
Output:
G B PANT DSEU OKHLA-I CAMPUS
DATA COMMUNICATION
AND NETWORK
Lab File
Submitted By: Aazmeer
Rayyaan
Roll No. 41422002
5th Semester
Department Of Electronics And
Communication Engineering