How to Implement Network Structure in NS2

To implement a network structure in ns2 has needs to configure the network topology, setting up node positions, describing link properties, and allows communication among nodes using numerous routing protocols or application-level traffic. A network structure in NS2 can denotes different kinds of networks like wired, wireless, ad-hoc, or sensor networks, relaying on simulation goals. The given below is guide to implementing a basic network structure in NS2:

Step-by-Step Implementation:

  1. Define the Network Topology

The topology demonstrates the arrangement of nodes and links in the network. Nodes are generated using the node command, and links among the nodes are established using duplex-link for wired networks or wireless channels for wireless networks.

Example of Creating a Basic Wired Topology:

# Create the simulator instance

set ns [new Simulator]

# Create nodes

set node1 [$ns node]

set node2 [$ns node]

set node3 [$ns node]

# Create duplex links between nodes (for wired networks)

$ns duplex-link $node1 $node2 1Mb 10ms DropTail  # 1Mb bandwidth, 10ms delay

$ns duplex-link $node2 $node3 1Mb 10ms DropTail

# Attach a loss model (optional)

$ns queue-limit $node1 $node2 50  # Set queue limit to 50 packets

  • duplex-link: Generates a bidirectional (full-duplex) link among two nodes with specified bandwidth and delay.
  • DropTail: Specifies the queue management scheme like DropTail, RED.
  • queue-limit: Describes the packet queue limit for the link.
  1. Configure Wireless Networks

For wireless networks, we describe the communication medium (wireless channel), set the physical layer parameters, and place the nodes in a 2D space.

Example of Setting Up a Wireless Network:

# Create the simulator instance

set ns [new Simulator]

# Define the wireless channel

set channel [new Channel/WirelessChannel]

# Configure the wireless parameters

set prop [new Propagation/TwoRayGround]  # Set propagation model

set topo [new Topography]  # Create the topology object

$topo load_flatgrid 1000 1000  # Define the size of the simulation area

# Configure wireless node parameters

set phy [new Phy/WirelessPhy]

set mac [new Mac/802_11]  # Use IEEE 802.11 MAC layer

set ll [new LL]  # Logical Link layer

set ifq [new Queue/DropTail/PriQueue]  # Interface queue

set antenna [new Antenna/OmniAntenna]  # Omni-directional antenna

# Create wireless nodes

for {set i 0} {$i < 10} {incr i} {

set node($i) [$ns node]

$node($i) random-motion 0  # Disable random node movement

}

# Set node positions

$node(0) set X_ 100

$node(0) set Y_ 100

$node(0) set Z_ 0

$node(1) set X_ 200

$node(1) set Y_ 200

$node(1) set Z_ 0

# Set other node positions similarly

  • WirelessChannel: Describes the interaction medium for wireless nodes.
  • Propagation Model: Specifies how signals propagate like TwoRayGround, FreeSpace.
  • Phy/WirelessPhy: Describes physical layer properties like transmission range and power.
  • Mac/802_11: Configures the MAC layer using the IEEE 802.11 standard for wireless networks.
  • OmniAntenna: Configures an omni-directional antenna for all nodes.
  • random-motion 0: Disables random motion if we want a static network.
  1. Set Node Mobility (for Mobile Networks)

In mobile ad-hoc networks (MANETs), nodes move enthusiastically within a given area. We can set node mobility by describing movement patterns over time.

Example of Setting Node Mobility:

# Enable mobility model

for {set i 0} {$i < 10} {incr i} {

$node($i) setdest 300 400 10  # Set destination coordinates (x, y) and speed

}

# Alternatively, use random waypoint mobility model

for {set i 0} {$i < 10} {incr i} {

$ns at 0.0 “$node($i) setdest [expr rand() * 1000] [expr rand() * 1000] [expr rand() * 10]”

}

  • setdest: Describes the destination coordinates and speed for each node.
  • Random Waypoint: This model moves nodes randomly within the simulation area.
  1. Attach Agents and Applications

After describing the topology, we attach agents (like UDP or TCP) to the nodes to manage traffic generation. Applications like CBR (Constant Bit Rate) or FTP creates traffic.

Example of Adding Traffic to the Network:

# Create TCP agents

set tcp0 [new Agent/TCP]

set tcp1 [new Agent/TCP]

$ns attach-agent $node1 $tcp0

$ns attach-agent $node2 $tcp1

$ns connect $tcp0 $tcp1

# Create FTP application over TCP

set ftp [new Application/FTP]

$ftp attach-agent $tcp0

# Start the FTP transfer at 1.0 seconds

$ns at 1.0 “$ftp start”

# Run the simulation

$ns run

  • Agent/TCP: Attaches a TCP agent to the node.
  • Agent/UDP: Use for UDP-based communication.
  • Application/FTP: Generates an FTP application that runs across TCP.
  • CBR (Constant Bit Rate): Creates UDP-based constant-rate traffic.
  1. Configure Routing Protocols

For communication between nodes, we need to describe the routing protocol. NS2 supports numerous built-in routing protocols like DSDV, AODV, DSR, and others. We can also execute custom routing protocols.

Example of Configuring Routing Protocol (AODV):

# Enable AODV routing protocol

$ns node-config -adhocRouting AODV

We can use other routing protocols such as DSDV, DSR, or execute own custom routing algorithm.

  1. Configure Link Failures (Optional)

To mimic network dynamics, like link failures, we can establish link failures and recoveries during the simulation.

Example of Simulating Link Failure:

# Simulate link failure between node1 and node2 at time 5.0 seconds

$ns rtmodel-at 5.0 down $node1 $node2

# Recover the link at time 10.0 seconds

$ns rtmodel-at 10.0 up $node1 $node2

  1. Run the Simulation and Collect Results

Finally, execute the simulation and collect the outcomes. NS2 creates trace files that record all events during the simulation. These trace files can be evaluated to extract parameters such as throughput, delay, packet loss, etc.

Running the Simulation:

# Run the simulation

$ns run

  1. Analyze the Output (Trace Files)

NS2 creates a trace file during the simulation that delivers the detailed logs of every packet sent, received, dropped, or forwarded. We can assess these trace files to compute numerous metrics.

Example of a Simple Trace File Analysis (in AWK):

# Calculate the packet delivery ratio (PDR)

awk ‘{

if($1 == “r” && $4 == “AGT”) received++;

if($1 == “s” && $4 == “AGT”) sent++;

} END { print “PDR = ” received/sent * 100 “%” }’ tracefile.tr

We can use AWK, Python, or other tools to process trace files and calculate parameters such as:

  • Packet Delivery Ratio (PDR)
  • Throughput
  • Average Latency
  • Energy Consumption (for wireless networks)

At the end, we thorough the manual and provide the valuable insights regarding how to implement the network structure in ns2 tool. Further details regarding the implementation of the network structure in diverse simulations will be provided.

Send us the specifics of your network structure research, and we’ll give you the best ns2tool simulation results.   You can contact us for the best thesis topics and ideas, and we’ll provide you with helpful advice. We provide you with the best simulation results by relaying on simulation goals through our work on various types of networks, including wired, wireless, ad hoc, and sensor networks.