To simulate a Peer-to-Peer (P2P) Topology using NS2 that encompasses the nodes communicating directly with each other, without depending on a central server or hub. Each node within a peer-to-peer network performs as both a client and a server that permitting the data to be interchanged directly among the nodes. This kind of topology is frequently utilized in file-sharing systems, distributed networks, and decentralized applications.
In the network simulator NS2, we can replicate a Peer-to-Peer Topology by permitting several nodes to communicate directly with each other with the help of duplex links.
We will instruct you on how to simulate a Peer-to-Peer Topology in NS2:
Steps to Simulate Peer to Peer Topology Projects in NS2
- Set up NS2 Environment:
Make sure that NS2 is installed and functioning on the machine. The simulation will be conducted by writing a TCL script to describe the peer-to-peer topology and replicate the communication among the nodes.
- Understanding Peer-to-Peer Topology:
- In a Peer-to-Peer Topology, all nodes can be communicated with other nodes directly, without a central server. Every single node can introduce communication and perform as both a sender and a receiver.
- This topology permits for decentralized communication in which the nodes are responsible for managing their own data exchanges.
- Create a TCL Script for Peer-to-Peer Topology:
Following is a TCL script to replicate a Peer-to-Peer Topology with 4 nodes in NS2:
# Create a new simulator instance
set ns [new Simulator]
# Open a NAM trace file for visualization
set nf [open out.nam w]
$ns namtrace-all $nf
# Create 4 nodes for the Peer-to-Peer network
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create duplex links between the nodes (fully connected peer-to-peer network)
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n0 $n2 10Mb 10ms DropTail
$ns duplex-link $n0 $n3 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms DropTail
# Attach UDP agents to each node
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
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
# Attach Null agents (traffic sinks) to each node
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
set null1 [new Agent/Null]
$ns attach-agent $n2 $null1
set null2 [new Agent/Null]
$ns attach-agent $n3 $null2
set null3 [new Agent/Null]
$ns attach-agent $n0 $null3
# Connect UDP agents to Null agents (peer-to-peer communication)
$ns connect $udp0 $null0
$ns connect $udp1 $null1
$ns connect $udp2 $null2
$ns connect $udp3 $null3
# Create CBR traffic sources and attach them to the UDP agents
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 512
$cbr0 set rate_ 100Kb
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set packetSize_ 512
$cbr1 set rate_ 100Kb
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp2
$cbr2 set packetSize_ 512
$cbr2 set rate_ 100Kb
set cbr3 [new Application/Traffic/CBR]
$cbr3 attach-agent $udp3
$cbr3 set packetSize_ 512
$cbr3 set rate_ 100Kb
# Schedule the CBR traffic to start and stop
$ns at 1.0 “$cbr0 start”
$ns at 1.5 “$cbr1 start”
$ns at 2.0 “$cbr2 start”
$ns at 2.5 “$cbr3 start”
$ns at 5.0 “$cbr0 stop”
$ns at 5.5 “$cbr1 stop”
$ns at 6.0 “$cbr2 stop”
$ns at 6.5 “$cbr3 stop”
# End the simulation at 7 seconds
$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: Four nodes (n0, n1, n2, n3) are made that signifying the peers within the peer-to-peer network.
- Links: Duplex links are made among each pair of nodes, which generating a fully connected peer-to-peer network in which every nodes can communicate directly with each other.
- UDP Traffic: UDP agents are connected to each node to replicate traffic generation. Null agents are associated as traffic sinks to get data from the respective UDP agents.
- CBR Traffic: Constant Bit Rate (CBR) traffic generators are made and connected to the UDP agents that replicating the communication amongst the nodes. Every single node transmits traffic to another node within the network.
- Traffic Scheduling: The CBR traffic begins at distinct times for each node (at 1.0, 1.5, 2.0, and 2.5 seconds) and ends at diverse times (5.0, 5.5, 6.0, and 6.5 seconds).
- Simulation End: The simulation stops at 7 seconds, and for visualization, the outcomes are saved in a .nam file.
- Run the Simulation:
- We need to save the script as peer_to_peer_topology.tcl.
- Open a terminal and navigate to the folder in which script is saved.
- We can execute the simulation using:
ns peer_to_peer_topology.tcl
- The simulation will make a out.nam file, which can be opened utilizing the Network Animator (NAM) for visualization.
- Visualization in NAM:
- We can open the out.nam file in NAM to envision the peer-to-peer topology. We will observe the nodes are fully connected to each other, and traffic flowing among them according to the CBR traffic schedule.
- Customization and Enhancements:
- Increase the Number of Nodes: We can insert additional nodes and links to replicate a larger peer-to-peer network. Simply prolong the script by including more nodes and connecting them with duplex links.
- TCP Traffic: Substitute the UDP agents with TCP agents for reliable communication:
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n1 $sink0
$ns connect $tcp0 $sink0
- Dynamic Peer Behavior: We can replicate dynamic peer behavior in which nodes are joining and exit the network for the period of the simulation by inserting or eliminating the links dynamically.
- Performance Metrics: Append the trace files to assess performance parameters such as packet loss, delay, and throughput:
set tracefile [open trace.tr w]
$ns trace-all $tracefile
- Performance Analysis:
To investigate the performance of the peer-to-peer topology, we can:
- Calculate throughput to monitor how much data is effectively sent among the peers.
- Assess latency (delay) to envision the duration for data to travel between nodes.
- Investigate the packet loss to estimate network reliability, particularly when utilizing UDP traffic.
In this set up, you can obtain the knowledge regarding how to set up the NS2 environment, how to customize and enhance the Peer to Peer Topology projects and how to analyse its performance metrics in NS2 tool. We are ready to give further detailed insights about this topic in another manual. You can simulate peer-to-peer topology projects using NS2 at phdprime.com, which will provide excellent simulation results and great topic ideas in this field.