LAB EXPERIMENT-5
LAB EXPERIMENT-5
1. What is NS2
NS2 stands for Network Simulator Version 2. It is an open-source event-driven simulator designed specifically for
research in computer communication networks.
2. Features of NS2
Discrete Event Simulation: NS2 uses discrete event simulation to model the behavior of network protocols and
applications. It simulates events such as packet transmissions, receptions, and protocol state changes at discrete
points in time.
Modular Architecture: NS2 is designed with a modular architecture, allowing users to extend and customize its
functionality by adding new protocols, algorithms, and network models. This makes it highly flexible and adaptable
to different research needs.
Support for Various Protocols: NS2 includes support for a wide range of network protocols, including TCP, UDP,
IP, routing protocols (such as OSPF, BGP, and RIP), transport layer protocols (such as TCP and UDP variants), and
application layer protocols (such as FTP, HTTP, and SMTP).
Wireless Network Support: NS2 provides support for simulating wireless networks, including mobile ad hoc
networks (MANETs), wireless sensor networks (WSNs), and wireless LANs (WLANs). It includes models for radio
propagation, mobility, and energy consumption in wireless devices.
Visualization Tools: NS2 comes with built-in visualization tools that allow users to visualize and analyze simulation
results. These tools provide graphical representations of network topologies, packet traces, and performance metrics,
helping users to understand the behavior of their simulated networks.
Scripting Support: NS2 is primarily scripted using the Tool Command Language (TCL), which allows users to define
network topologies, configure simulation parameters, and specify traffic patterns. Additionally, users can write C++
code to extend NS2's functionality or implement custom protocols.
Community Support: NS2 has a large and active user community, with many resources available online, including
documentation, tutorials, and user forums. This community support makes it easier for users to get started with
NS2, troubleshoot problems, and collaborate with other researchers.
Open Source: NS2 is open-source software, distributed under the GNU General Public License (GPL). This means
that users have access to the source code and can modify it according to their needs. It also encourages collaboration
and contributions from the research community.
NS2 (Network Simulator 2) is primarily written in two languages: C++ and OTcl (Object-oriented Tcl).
C++: The core of NS2, including the simulation engine and many built-in models and protocols, is
implemented in C++. C++ provides efficiency and low-level control, making it well-suited for tasks such as
packet processing and network simulation.
OTcl: OTcl is an extension of the Tcl (Tool Command Language) scripting language that adds object-oriented
features. NS2 uses OTcl for configuration and scripting purposes. Users can define network topologies,
configure simulation parameters, and specify protocols and applications using OTcl scripts.
Installing NS2:
Step 1: Open the terminal and execute the following command
Step 2: Go to google and type as follows, download the file from the google drive link.
Step 3: Go to the folder where you have the downloaded file and open terminal and type the following
command.
The installation is successful, and you can now start doing your programs.
Create a simple network configuration as follows and simulate the following network
This network consists of 4 nodes (n0, n1, n2, n3) as shown in above figure.
The duplex links between n0 and n2, and n1 and n2 have 2 Mbps of bandwidth and 10 ms of delay.
The duplex link between n2 and n3 has 1.7 Mbps of bandwidth and 20 ms of delay.
Each node uses a DropTail queue, of which the maximum size is 10. A "tcp" agent is attached to n0,
and a connection is established to a tcp "sink" agent attached to n3.
As default, the maximum size of a packet that a "tcp" agent can generate is 1KByte.
A tcp "sink" agent generates and sends ACK packets to the sender (tcp agent) and frees the received
packets.
A "udp" agent that is attached to n1 is connected to a "null" agent attached to n3. A "null" agent just
frees the packets received.
A "ftp" and a "cbr" traffic generator are attached to "tcp" and "udp" agents respectively, and the "cbr"
is configured to generate 1 KByte packets at the rate of 1 Mbps. The "cbr" is set to start at 0.1 sec and
stop at 4.5 sec, and "ftp" is set to start at 1.0 sec and stop at 4.0 sec.
Program Code:
Save it as ns-simple.tcl as shown below:
Code:
#Create a simulator object
set ns [new Simulator]
Explanation:
set ns [new Simulator]: This line creates a new ns (Network Simulator) object named ns, which will be used
to define and simulate the network topology.
$ns color 1 Blue and $ns color 2 Red: These lines define different colors for data flows in the Network
Animator (NAM) visualization tool. Blue is assigned to flow 1, and red is assigned to flow 2.
set nf [open out.nam w] and $ns namtrace-all $nf: These lines open a trace file named "out.nam" and
configure the ns object to trace all events in the simulation to this file using the Network Animator (NAM).
proc finish {} { ... }: This block of code defines a Tcl procedure named finish. It flushes the trace, closes the
trace file, executes NAM on the trace file, and exits the simulation.
set n0 [$ns node], set n1 [$ns node], etc.: These lines create four nodes in the network simulation and store
references to them in variables n0, n1, n2, and n3.
$ns duplex-link $n0 $n2 2Mb 10ms DropTail, etc.: These lines create duplex links between the nodes with
specified bandwidths, delays, and queuing disciplines (DropTail).
$ns queue-limit $n2 $n3 10: This line sets the queue size of the link between nodes n2 and n3 to 10 packets.
$ns duplex-link-op $n0 $n2 orient right-down, etc.: These lines position the nodes and links in the NAM
visualization.
$ns duplex-link-op $n2 $n3 queuePos 0.5: This line positions the queue monitor for the link between nodes
n2 and n3.
set tcp [new Agent/TCP], set sink [new Agent/TCPSink], etc.: These lines create TCP and UDP agents for
data transmission and reception.
$ns attach-agent $n0 $tcp, etc.: These lines attach the TCP and UDP agents to the corresponding nodes.
$ns connect $tcp $sink: This line establishes a connection between the TCP agent and the TCP sink agent.
$tcp set fid_ 1, $udp set fid_ 2: These lines set flow identifiers for TCP and UDP agents.
set ftp [new Application/FTP], set cbr [new Application/Traffic/CBR]: These lines create FTP and CBR
(Constant Bit Rate) traffic applications.
$ftp attach-agent $tcp, $cbr attach-agent $udp: These lines attach the FTP and CBR applications to the
corresponding agents.
$cbr set packet_size_ 1000, $cbr set rate_ 1mb, etc.: These lines configure the parameters for the CBR
traffic, including packet size, rate, and whether the traffic is generated randomly.
$ns at 0.1 "$cbr start", etc.: These lines schedule events for starting and stopping the CBR and FTP traffic.
$ns at 5.0 "finish": This line schedules the finish procedure to be executed after 5 seconds of simulation time.
puts "CBR packet size = [$cbr set packet_size_]", etc.: These lines print out the CBR packet size and
interval.