To simulate network communication projects using NS2 (Network Simulator 2) that involves modelling communication among nodes in numerous network scenarios, using distinct topologies, and leveraging communication protocols like TCP, UDP, or wireless protocols like 802.11. This instruction will walk you through configuring a simple simulation in NS2 and then move to more complex scenarios.
Steps to Simulate Network Communication Projects in NS2
- Install NS2
We require to have NS2 installed to start functioning on simulations. We can download NS2 from the NS2 website. Also, we follow the installation guidelines particular to the operating system (Linux is recommended for ease of use).
- Basic Concepts in Network Communication Simulation with NS2
NS2 models communication networks such as:
- Nodes: Signifying network devices like computers, routers, or mobile devices.
- Links: Denoting communication channels (wired or wireless) among nodes.
- Traffic Agents: Signifying protocols like TCP or UDP.
- Applications: Denoting end-user applications such as FTP, CBR, and so on, which generate traffic for the simulation.
- Basic Network Simulation Example
Example 1: Simple Wired Network Communication (TCP)
Here’s how to configure a basic simulation in which two nodes are communicate using TCP over a wired network.
# Create NS2 simulator instance
set ns [new Simulator]
# Create a trace file to log the simulation
set tracefile [open out.tr w]
$ns trace-all $tracefile
# Create two nodes
set node1 [$ns node]
set node2 [$ns node]
# Create a duplex link between node1 and node2 (100Mb bandwidth, 10ms delay)
$ns duplex-link $node1 $node2 100Mb 10ms DropTail
# Set up TCP agent on node1 and a TCPSink on node2
set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node1 $tcp
$ns attach-agent $node2 $sink
# Connect TCP agent to the sink
$ns connect $tcp $sink
# Set up FTP traffic over TCP from node1 to node2
set ftp [new Application/FTP]
$ftp attach-agent $tcp
# Schedule FTP traffic to start at time 0.1s and stop at 4.5s
$ns at 0.1 “$ftp start”
$ns at 4.5 “$ftp stop”
# Define a finish procedure to stop the simulation
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exec nam out.nam &
exit 0
}
# Set the simulation to finish at 5 seconds
$ns at 5.0 “finish”
# Run the simulation
$ns run
Explanation:
- Nodes: We make two nodes, node1 and node2.
- Duplex link: A link with a bandwidth of 100Mbps and a delay of 10ms is ascertained among the two nodes.
- TCP connection: A TCP agent is connected to node1, and a TCP sink is connected to node2.
- FTP application: FTP traffic is generated from node1 to node2 using TCP.
- Tracing: All events in the simulation are logged in a trace file (out.tr).
- NAM (Network Animator): A NAM file is made for visualization (out.nam).
- Setting Up Wireless Network Communication
Example 2: Wireless Network Communication (UDP)
For wireless communication, we can model nodes utilizing 802.11 and replicate the network using UDP traffic among two mobile nodes.
# Create NS2 simulator instance
set ns [new Simulator]
# Define wireless channel
set chan [new Channel/WirelessChannel]
# Configure wireless nodes
$ns node-config -adhocRouting AODV -llType LL -macType Mac/802_11 -ifqType Queue/DropTail/PriQueue
-ifqLen 50 -antType Antenna/OmniAntenna -propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy -channelType Channel/WirelessChannel
# Create two wireless nodes
set node1 [$ns node]
set node2 [$ns node]
# Set node positions (X, Y, Z coordinates)
$node1 set X_ 50.0
$node1 set Y_ 50.0
$node1 set Z_ 0.0
$node2 set X_ 150.0
$node2 set Y_ 100.0
$node2 set Z_ 0.0
# Attach UDP agent on node1 and sink (Null agent) on node2
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $node1 $udp
$ns attach-agent $node2 $null
# Connect UDP to Null sink
$ns connect $udp $null
# Set up CBR traffic over UDP from node1 to node2
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 500
$cbr set interval_ 0.05 # 50ms intervals between packets
# Schedule the traffic to start at 0.1s and stop at 4.5s
$ns at 0.1 “$cbr start”
$ns at 4.5 “$cbr stop”
# Trace the simulation
set tracefile [open wireless.tr w]
$ns trace-all $tracefile
# Define finish procedure
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exec nam wireless.nam &
exit 0
}
# Run the simulation for 5 seconds
$ns at 5.0 “finish”
$ns run
Explanation:
- Wireless Channel: A wireless communication channel using 802.11 is configure among the two nodes.
- Node Configuration: Nodes are set up with AODV routing, DropTail queue, and OmniAntenna.
- UDP Communication: A UDP agent is connected to node1, and a Null agent (sink) is connected to node2.
- CBR Traffic: Constant Bit Rate (CBR) traffic is transmitted from node1 to node2.
- Tracing: The simulation makes a trace file (wireless.tr) and a NAM visualization file (wireless.nam).
- More Complex Topologies and Traffic Patterns
5.1. Multi-Hop Communication
We can replicate multi-hop communication by inserting intermediate nodes, which forward packets among source and destination nodes.
# Add a third node (node3) as an intermediate node for multi-hop
set node3 [$ns node]
# Set positions for the intermediate node
$node3 set X_ 100.0
$node3 set Y_ 75.0
$node3 set Z_ 0.0
# Create wireless links between node1-node3 and node3-node2
$ns simplex-link $node1 $node3 1Mb 10ms DropTail
$ns simplex-link $node3 $node2 1Mb 10ms DropTail
5.2. Mobile Nodes and Handoff
We can replicate node mobility using the setdest command in NS2 in which nodes are move dynamically during the simulation.
# Set initial and destination positions for node1 to simulate mobility
$node1 setdest 100.0 150.0 10.0 ;# Move node1 to (100, 150) at 10 m/s
- Analyzing the Simulation
Trace Files
NS2 makes trace files, which include detailed records of all events in the simulation, like packet transmission, reception, drops, and routing decisions. We can parse these trace files to measure parameters like:
- Throughput: The amount of data effectively sent over the network.
- Packet Delivery Ratio: The ratio of effectively received packets to send packets.
- End-to-End Delay: The time it takes for a packet to travel from source to destination.
NAM Visualization
To envision the simulation, NAM (Network Animator) is utilised. Insert the below lines to the script to allow NAM visualization:
# Enable NAM trace file
set namfile [open simulation.nam w]
$ns namtrace-all $namfile
We can run the following command to envision the simulation:
nam simulation.nam
- Advanced Network Communication Scenarios
7.1. Congestion Control
To replicate network congestion by making several traffic flows among nodes. We can differ the data rate and investigate how congestion influences throughput and packet loss.
# Set up another TCP connection between node1 and node3 to simulate congestion
set tcp2 [new Agent/TCP]
set sink2 [new Agent/TCPSink]
$ns attach-agent $node1 $tcp2
$ns attach-agent $node3 $sink2
$ns connect $tcp2 $sink2
7.2. Quality of Service (QoS)
We can model QoS by setting priorities for specific traffic types and setting up queues at nodes (e.g., RED, DropTail).
# Set RED queue at node2 for congestion control
$ns queue-limit $node2 50 ;# RED queue with a limit of 50 packets
- Key Performance Metrics
After running the simulations, we can investigate numerous performance parameters:
- Throughput: Total data received split by the simulation time.
- Packet Delivery Ratio (PDR): Total number of packets received at the destination split by the amount of packets transmitted.
- End-to-End Delay: Average time taken by packets to attain the destination.
- Packet Loss: Total amount of packets dropped because of congestion or errors.
We developed simulation steps to replicate and analyse the Network Communication projects using NS2 simulation platform. Should it be required, we are equipped to distribute deeper insights and relevant details on this topic.
phdprime.com will help you achieve the best results in Network Communication simulations. We provide customized project ideas and topics based on your interests when you collaborate with us. Our writing services are top-notch. You can also get expert advice on communication protocols like TCP, UDP, and wireless protocols for your projects, and we assist with project evaluation as well.