How to Implement Network Packets Transmission in NS2

To implement network packet transmission in NS2 has series of steps to follow that has to generate the simulation scenarios in which the nodes interacted by sending packets using diverse protocols like TCP, UDP. We can modify the packet transmission by setting up the diverse network metrics such as link bandwidth, latency, packet size, and traffic patterns. The below is an implementation procedure to implement the packet transmission between nodes in NS2.

Steps to Implement Network Packet Transmission in NS2

  1. Install NS2

Make sure that NS2 is installed on the machine.

  1. Create a Basic TCL Script for Packet Transmission

The simple configuration has involves generating nodes, connecting them with links, describing traffic agents, and generating data packets.

Example: Simple Packet Transmission Using TCP

# Define a simulator object

set ns [new Simulator]

# Define trace and nam files for output

set tracefile [open out.tr w]

set namfile [open out.nam w]

$ns trace-all $tracefile

$ns namtrace-all $namfile

# Define a ‘finish’ procedure to end the simulation and visualize in NAM

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}

# Create two nodes

set n0 [$ns node]

set n1 [$ns node]

# Create a duplex link between the nodes with a bandwidth of 2Mb and delay of 10ms

$ns duplex-link $n0 $n1 2Mb 10ms DropTail

# Define a TCP agent at node n0 (source)

set tcp [new Agent/TCP]

$ns attach-agent $n0 $tcp

# Define a TCP sink at node n1 (destination)

set sink [new Agent/TCPSink]

$ns attach-agent $n1 $sink

# Connect the TCP agent to the sink

$ns connect $tcp $sink

# Create an FTP application to generate traffic over TCP

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp start 1.0  ;# Start FTP traffic at 1 second

$ftp stop 4.0   ;# Stop FTP traffic at 4 seconds

# Schedule the end of the simulation

$ns at 5.0 “finish”

# Run the simulation

$ns run

  1. Explanation of the Script
  • Nodes and Links: Two nodes (n0 and n1) are generated and associated by a duplex link with a bandwidth of 2 Mbps and a delay of 10 ms.
  • Traffic Source (TCP): A TCP agent is attached to node n0 (the source), and a TCP sink is attached to node n1 (the destination).
  • Application (FTP): FTP traffic is created over TCP, initiating at 1 second and ending at 4 seconds.
  • Trace and NAM Files: Trace files (out.tr) are created to record packet transmission events, and a NAM file (out.nam) is generated for visualization.
  1. Run the Simulation

Save the script as packet_transmission.tcl and execute it in NS2 using the following command:

ns packet_transmission.tcl

This will create a trace file (out.tr) and a NAM file (out.nam).

  1. Visualize the Packet Transmission in NAM

We can use the NAM file to envision the network and see packet transmission among the nodes:

nam out.nam

The NAM envisions will demonstrate packets being communicated among n0 and n1.

  1. Analyse the Trace File

After executing the simulation, we need to evaluate the trace file (out.tr) to track packet events (send, receive, drop).

The format of the trace file usually looks like this:

r 2.000000000 _0_ AGT  — 1 tcp 40 [0 0 0 0] [0] 1 0

s 2.010000000 _0_ AGT  — 1 tcp 40 [0 0 0 0] [0] 1 0

  • r: Packet received
  • s: Packet sent
  • AGT: Agent level (application traffic like FTP, CBR)
  • tcp: Protocol used (TCP in this case)
  • Time: Timestamp of the event

We need to use AWK, Perl, or Python to process this trace file and extract parameters such as throughput, delay, or packet loss.

  1. Example: Packet Transmission Using UDP

To execute packet transmission using UDP rather than TCP, adapt the script as follows:

# Define a simulator object

set ns [new Simulator]

# Define trace and nam files for output

set tracefile [open out.tr w]

set namfile [open out.nam w]

$ns trace-all $tracefile

$ns namtrace-all $namfile

# Create two nodes

set n0 [$ns node]

set n1 [$ns node]

# Create a duplex link between the nodes with a bandwidth of 2Mb and delay of 10ms

$ns duplex-link $n0 $n1 2Mb 10ms DropTail

# Define a UDP agent at node n0 (source)

set udp [new Agent/UDP]

$ns attach-agent $n0 $udp

# Define a Null agent at node n1 (destination for UDP)

set null [new Agent/Null]

$ns attach-agent $n1 $null

# Connect the UDP agent to the Null agent

$ns connect $udp $null

# Create CBR traffic over UDP

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetSize_ 500

$cbr set rate_ 1Mb  ;# Set CBR rate to 1 Mbps

$cbr start 1.0      ;# Start CBR at 1 second

$cbr stop 4.0       ;# Stop CBR at 4 seconds

# Schedule the end of the simulation

$ns at 5.0 “finish”

# Run the simulation

$ns run

Explanation of the UDP Script:

  • UDP Agent: A UDP agent is devoted to node n0, and a Null agent is attached to node n1 to obtain packets.
  • CBR Traffic: Constant Bit Rate (CBR) traffic is created over UDP, with a packet size of 500 bytes and a rate of 1 Mbps.
  • Traffic Pattern: CBR traffic initiates at 1 second and stops at 4 seconds.
  1. Analysing Packet Transmission

We can assess numerous aspects of packet transmission, such as:

  • Throughput: The total number of bytes received per second.
  • Delay: The time taken for packets to travel from the source to the destination.
  • Packet Loss: The number of packets lost during transmission.

To compute these parameters, we can extract data from the trace file using AWK, Perl, or Python scripts.

  1. Sample AWK Script to Calculate Throughput

The below is an instance AWK script to estimate throughput from the trace file:

awk ‘{

if ($1 == “r” && $4 == “AGT” && $9 == “tcp”) {

total_bytes += $8

count++

}

} END {

if (count > 0) {

throughput = total_bytes / (5 – 1)  ;# Simulation time from 1s to 5s

print “Throughput: “, throughput, “bytes/sec”

}

}’ out.tr

This script estimates the throughput by totalling the size of all received packets ($8) and dividing by the mimic the time (from 1 second to 5 seconds).

In this module, we had clearly understood the implementation procedures, sample snippets were given to enforce the network packet transmission with the help of ns2 tool. We also deliver further significant information regarding the network packet transmission will be provided. We work on protocols like TCP, UDP related to your projects get implementation guidance from us.