To simulate a Star Topology in NS2, we can follow the below steps:
Steps to Simulate Star Topology Projects in NS2
- Set up NS2 Environment:
Make certain that NS2 is installed and functioning on the machine. NS2 uses TCL scripts to describe network topology and traffic patterns. We will write a script for Star Topology simulation.
- Understanding Star Topology:
- In a Star Topology, all nodes are associated to a central node (often called a hub or switch). Communication among two nodes must run through the central node.
- In NS2, we can replicate it by making various nodes, which are connected to a central node.
- Create a TCL Script for Star Topology:
Below is an instance of a basic Star Topology simulation in NS2:
# Create a new simulator instance
set ns [new Simulator]
# Open a NAM file for visualization
set nf [open out.nam w]
$ns namtrace-all $nf
# Create nodes
set centralNode [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
# Create links between the central node and all other nodes
$ns duplex-link $centralNode $n1 10Mb 10ms DropTail
$ns duplex-link $centralNode $n2 10Mb 10ms DropTail
$ns duplex-link $centralNode $n3 10Mb 10ms DropTail
$ns duplex-link $centralNode $n4 10Mb 10ms DropTail
# Define UDP agents for traffic generation
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set udp2 [new Agent/UDP]
$ns attach-agent $n2 $udp2
set udp3 [new Agent/UDP]
$ns attach-agent $n3 $udp3
# Create Null agents at the central node to receive the traffic
set null1 [new Agent/Null]
$ns attach-agent $centralNode $null1
set null2 [new Agent/Null]
$ns attach-agent $centralNode $null2
set null3 [new Agent/Null]
$ns attach-agent $centralNode $null3
# Connect each node’s UDP agent to the central node’s Null agent
$ns connect $udp1 $null1
$ns connect $udp2 $null2
$ns connect $udp3 $null3
# Create traffic sources (CBR) for each node
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 512
$cbr1 set rate_ 100Kb
$cbr1 attach-agent $udp1
set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 512
$cbr2 set rate_ 100Kb
$cbr2 attach-agent $udp2
set cbr3 [new Application/Traffic/CBR]
$cbr3 set packetSize_ 512
$cbr3 set rate_ 100Kb
$cbr3 attach-agent $udp3
# Start and stop traffic flows at specific times
$ns at 1.0 “$cbr1 start”
$ns at 1.5 “$cbr2 start”
$ns at 2.0 “$cbr3 start”
$ns at 5.0 “$cbr1 stop”
$ns at 5.5 “$cbr2 stop”
$ns at 6.0 “$cbr3 stop”
# Define when the simulation will finish
$ns at 7.0 “finish”
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
# Run the simulation
$ns run
- Explanation of the Code:
- Nodes: A central node (the hub) is associated to four other nodes (n1, n2, n3, n4). It forms the star topology.
- Links: Duplex links are made among the central node and each of the other nodes. These links describe the connection bandwidth (10Mb) and delay (10ms).
- Traffic: Each node generates UDP traffic to the central node utilizing CBR (Constant Bit Rate) traffic. The traffic begins at diverse times to display concurrent communication via the central node.
- Finish: The simulation will end after 7 seconds, and the NAM file will be opened for visualization.
- Run the Simulation:
- We can save the script with a .tcl extension, for instance, star_topology.tcl.
- Open a terminal and traverse to the folder containing the script.
- Execute the simulation with the below command:
ns star_topology.tcl
- The output will be saved in a NAM file (out.nam), which you can visualize using the NAM tool.
When the simulation runs then the Network Animator (NAM) will show the star topology with all nodes is associated to the central node.
- Customization and Enhancements:
- Add More Nodes: We can insert more nodes by making more nodes and duplex links.
- Change Traffic Patterns: We can alter traffic types, like using TCP rather than UDP, or changing packet sizes, rates, or delays.
- Performance Analysis: We can insert measurement tools such as traces to log the performance of the star topology, like throughput and latency.
This technique will guide you through the simulation approach for Star Topology projects with their instances and their enhancement using NS2 tool. We will also offer the extra information on any other topology, if needed. To effectively simulate a Star Topology in NS2, we provide a detailed, step-by-step guide and offer customized research assistance from the team at phdprime.com.