How to Implement Network Communication in ns2

To implement the network communication using ns2 that has encompasses setting up nodes, describing links among them, set up the agents such as TCP/UDP, and generating traffic patterns. Given below are simple procedure on how we can execute a simple network communication scenario in ns2:

Step-by-Step Implementations:

Step 1: Basic Tcl Script Structure

Initially, we writing a Tcl script that describes the network topology, communication protocols, and traffic patterns.

Example Script: Basic Network Communication in ns2

  1. Initialize the Simulator

Start by, to make an emulator object and state trace files to capture the simulation data.

# Create a simulator object

set ns [new Simulator]

# Open trace file

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Open a NAM (Network Animator) file for visualization

set namfile [open out.nam w]

$ns namtrace-all $namfile

  1. Define the Finish Procedure

This method is known at the end of the simulation to clean up and introduce the NAM visualization tool.

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}

  1. Create Nodes

Explain the nodes in the network. For this instance, we will make three nodes.

# Create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

  1. Create Links between Nodes

Describe the communication links among the nodes including bandwidth, delay, and queue management policies.

# Create duplex links between the nodes

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

$ns duplex-link $n1 $n2 1.5Mb 20ms DropTail

  1. Configure Communication Protocols

Join agents to the nodes to handle communication. In this instance, we will be used TCP for communication among n0 and n2, with node n1 performing as an intermediate router.

# TCP Agent at n0

set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

# TCP Sink at n2

set sink0 [new Agent/TCPSink]

$ns attach-agent $n2 $sink0

# Connect TCP to TCPSink

$ns connect $tcp0 $sink0

  1. Generate Traffic

Append an application to create the traffic. In this example, we will use the FTP over TCP.

# Create an FTP application and attach it to TCP agent

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

$ftp0 start

  1. Schedule Simulation Time

Resolve when the simulation would end. For this instance, the simulation runs for 5 seconds.

# Schedule end of simulation

$ns at 4.5 “finish”

  1. Run the Simulation

Lastly, we run the simulation using the run command.

# Run simulation

$ns run

Full Tcl Script for Network Communication:

# Create a simulator object

set ns [new Simulator]

# Open trace and NAM files

set tracefile [open out.tr w]

$ns trace-all $tracefile

set namfile [open out.nam w]

$ns namtrace-all $namfile

# Define finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}

# Create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

# Create links between nodes

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

$ns duplex-link $n1 $n2 1.5Mb 20ms DropTail

# TCP Agent at node n0

set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

# TCP Sink at node n2

set sink0 [new Agent/TCPSink]

$ns attach-agent $n2 $sink0

# Connect TCP agent to TCPSink

$ns connect $tcp0 $sink0

# Create FTP application and attach to TCP agent

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

$ftp0 start

# Schedule end of simulation

$ns at 4.5 “finish”

# Run simulation

$ns run

Step 2: Run the Simulation

We can save the script as network_communication.tcl then run it using the below command:

ns network_communication.tcl

This command will make a trace file (out.tr) and a NAM file (out.nam). The trace file has encompasses complete information regarding the packet transmission, delays, and losses. The NAM file can be visualized using the NAM tool.

Step 3: Visualize the Simulation

We can visualize the network communication using the NAM tool, then we run the below command:

nam out.nam

The NAM tool will open, presenting the network topology and the communication process.

Step 4: Analyse Results

We can examine the output trace file (out.tr) to get performance parameters such as throughput, latency, packet loss, etc., using analysis tools such as AWK, Perl, or Python scripts.

Customizing the Simulation

  • Adding more nodes: We can append more nodes and links to the topology by expanding the code.
  • Using UDP: We can substitute the TCP with UDP by varying the agent kind (Agent/UDP) and using a CBR (Constant Bit Rate) application for traffic generation.
  • Changing protocols: The simulation tool ns2 aids numerous routing and transport protocols such as AODV, DSR, etc., which we can simply incorporate into the script.

We applied a sequential procedure to Network communication, which was then implemented and evaluated using the simulation tool ns2. If you want further details regarding this, we will offered. If you’re looking for top-notch assistance and guidance on Network Communication in ns2, hit up ns2project.com for the best outcomes.