INDEX
[Link]. Program Page No. Sign
To understand and study Study of basic network command and
1 2
network configuration commands.
2 Simple project on NS2 – wired and wireless. 3-6
3 Simulation study of pure ALOHA protocol. 7-9
4 Simulation study of slotted ALOHA protocol 10-12
5 Simulation study of Token Bus LAN protocol. 13-15
6 Simulation study of Token Ring LAN protocol. 16-17
7 Simulation study of WAN protocol like Frame Relay, X. 25 18-19
8 Study of 802. 11 wireless LAN protocols. 20-22
Implement the Distance Vector Routing protocol for finding the
9 23-24
shortest path.
Write a program to connect server with client and passes
10 information from one system to another and vice versa that by 25-26
creating / establishing connection.
1
Experiment - 1
Objective : To understand and study Study of basic network command and network configuration commands.
Theory :
NS2 (Network Simulator 2) is a discrete event simulator primarily used for simulating network protocols, such
as TCP, UDP, routing, and queues in wired and wireless networks.
NAM (Network Animator) is a graphical tool that visualizes the packet movement and node behavior in a
network simulation created in NS2.
Common Network Configuration Commands in NS2:
Command Description
set ns [new Simulator] Creates a new simulator object
set n0 [$ns node] Creates a network node
Creates a bidirectional link between nodes with
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
bandwidth and delay
$ns attach-agent $n0 $udp0 Attaches an agent (e.g., UDP) to a node
$ns connect $udp0 $null1 Connects sender agent to receiver agent
$ns rtproto DV Sets the routing protocol to Distance Vector
$ns at 1.0 "$app start" Schedules an event at a particular time
proc finish {} User-defined procedure to finish simulation
$ns run Starts the simulation
Basic NAM Configuration Commands:
Command Description
$ns namtrace-all $file Creates NAM trace file
$ns duplex-link-op $n0 $n1 orient right Sets orientation of links in NAM
exec nam [Link] & Launches NAM with the output trace
2
Experiment - 2
Objective : Simple project on NS2 – wired and wireless
Code Implementation:
1. Wired Simulation -
# Create a simulator
set ns [new Simulator]
# Open trace files
set tracefile [open [Link] w]
$ns trace-all $tracefile
set namfile [open [Link] w]
$ns namtrace-all $namfile
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
# Create a duplex link between nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link-op $n0 $n1 orient right
# Create UDP agent and attach to node n0
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
# Create Null agent and attach to node n1
set null [new Agent/Null]
$ns attach-agent $n1 $null
# Connect the agents
$ns connect $udp $null
# Attach a CBR application to the UDP agent
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 500
$cbr set interval_ 0.5
$cbr attach-agent $udp
# Start and stop traffic
$ns at 0.5 "$cbr start"
$ns at 4.5 "$cbr stop"
3
# Define finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
exit 0
}
# Schedule simulation end
$ns at 5.0 "finish"
# Run the simulation
$ns run
Output Simulation:
2. Wireless Simulation
# Create simulator
set ns [new Simulator]
# Define trace and nam files
4
set tracefile [open [Link] w]
$ns trace-all $tracefile
set namfile [open [Link] w]
$ns namtrace-all $namfile
# Set up topography
set topo [new Topography]
$topo load_flatgrid 500 500
# Create God object
create-god 2
# 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 \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON
# Create wireless nodes
set n0 [$ns node]
set n1 [$ns node]
# Set node positions
$n0 set X_ 100
$n0 set Y_ 100
$n0 set Z_ 0
$n1 set X_ 200
$n1 set Y_ 100
$n1 set Z_ 0
# Fix node positions
$ns at 0.0 "$n0 setdest 100 100 0"
$ns at 0.0 "$n1 setdest 200 100 0"
# Setup UDP agent and CBR traffic
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
5
set null [new Agent/Null]
$ns attach-agent $n1 $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 300
$cbr set interval_ 0.3
$cbr attach-agent $udp
$ns at 1.0 "$cbr start"
$ns at 4.5 "$cbr stop"
# Finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
exit 0
}
$ns at 5.0 "finish"
$ns run
Output Simulation:
6
Experiment - 3
Objective : Simulation study of pure ALOHA protocol.
Code Implementation:
# ALOHA Simulation in NS2 using TCL
# Define simulator
set ns [new Simulator]
# Open trace and NAM files
set tracefile [open [Link] w]
$ns trace-all $tracefile
set namfile [open [Link] w]
$ns namtrace-all $namfile
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
# Positioning for NAM visualization
$n0 set X_ 50
$n0 set Y_ 100
$n1 set X_ 150
$n1 set Y_ 100
$n2 set X_ 250
$n2 set Y_ 100
# Links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n2 $n1 1Mb 10ms DropTail
# Set up UDP agents to simulate ALOHA senders
set udp0 [new Agent/UDP]
set udp1 [new Agent/UDP]
$ns attach-agent $n0 $udp0
$ns attach-agent $n2 $udp1
# Null sink at receiver
set null [new Agent/Null]
$ns attach-agent $n1 $null
$ns connect $udp0 $null
$ns connect $udp1 $null
7
# Create traffic generators (CBR) with ALOHA-like behavior (random start times)
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 1.0
$cbr0 attach-agent $udp0
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 1.0
$cbr1 attach-agent $udp1
# ALOHA: schedule with random times to simulate lack of synchronization
set rng0 [new RandomVariable/Uniform]
$rng0 set min_ 0.5
$rng0 set max_ 1.5
set start0 [$rng0 value]
set rng1 [new RandomVariable/Uniform]
$rng1 set min_ 0.5
$rng1 set max_ 1.5
set start1 [$rng1 value]
# Start CBR traffic with randomized delays
$ns at $start0 "$cbr0 start"
$ns at $start1 "$cbr1 start"
# Stop simulation
$ns at 5.0 "$cbr0 stop"
$ns at 5.0 "$cbr1 stop"
$ns at 5.1 "finish"
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
exit 0
}
# Run simulation
$ns run
8
Output Simulation:
9
Experiment - 4
Objective : Simulation study of slotted ALOHA protocol.
Code Implementation:
# Slotted ALOHA Simulation in NS2.35
set ns [new Simulator]
# Create trace and nam output files
set tracefile [open slotted_aloha.tr w]
$ns trace-all $tracefile
set namfile [open slotted_aloha.nam w]
$ns namtrace-all-wireless $namfile 500 500
# Define simulation area
set val(x) 500
set val(y) 500
# Number of nodes
set num_nodes 5
# Create the wireless channel
set chan [new Channel/WirelessChannel]
# Topography
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Configure wireless nodes
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON
# Create God for routing info
create-god $num_nodes
10
# Create nodes and set initial positions
for {set i 0} {$i < $num_nodes} {incr i} {
set node($i) [$ns node]
$node($i) set X_ [expr 100 + 80 * $i]
$node($i) set Y_ 250
$node($i) set Z_ 0
$ns initial_node_pos $node($i) 30
}
# Create a UDP agent and traffic generator for each node
for {set i 0} {$i < $num_nodes} {incr i} {
# UDP Agent
set udp($i) [new Agent/UDP]
$ns attach-agent $node($i) $udp($i)
# Null sink at node 0 (receiver)
if {$i != 0} {
set null($i) [new Agent/Null]
$ns attach-agent $node(0) $null($i)
$ns connect $udp($i) $null($i)
# CBR traffic
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 512
$cbr($i) set interval_ 0.05
$cbr($i) set random_ false
$cbr($i) attach-agent $udp($i)
}
}
# Define slot time (in seconds)
set slot_time 0.1
# Schedule slotted transmissions
set sim_start 1.0
for {set t $sim_start} {$t <= 9.0} {set t [expr $t + $slot_time]} {
# Randomize which node gets to transmit this slot
set sender [expr int(rand() * ($num_nodes - 1)) + 1]
$ns at $t "$cbr($sender) start"
$ns at [expr $t + 0.02] "$cbr($sender) stop"
}
# End simulation
$ns at 10.0 "finish"
proc finish {} {
global ns tracefile namfile
11
$ns flush-trace
close $tracefile
close $namfile
exec nam slotted_aloha.nam &
exit 0
}
# Run the simulation
$ns run
Output Simulation:
12
Experiment - 5
Objective : Simulation study of Token Bus LAN protocol.
Code Implementation:
# Token Bus Protocol Simulation in NS2 (fixed layout)
# Create the simulator
set ns [new Simulator]
# Output files
set tracefile [open [Link] w]
$ns trace-all $tracefile
set namfile [open [Link] w]
$ns namtrace-all $namfile
# Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create a hub node to simulate bus medium
set hub [$ns node]
# Set positions for clear NAM layout
$n0 set X_ 50
$n1 set X_ 150
$n2 set X_ 250
$n3 set X_ 350
$n0 set Y_ 200
$n1 set Y_ 200
$n2 set Y_ 200
$n3 set Y_ 200
$hub set X_ 200
$hub set Y_ 100
# Connect all nodes to the common hub (bus)
foreach node "$n0 $n1 $n2 $n3" {
$ns duplex-link $node $hub 1Mb 10ms DropTail
}
# Create UDP agents for each node
13
foreach i {0 1 2 3} {
set udp($i) [new Agent/UDP]
set null($i) [new Agent/Null]
set node [eval set n$i]
$ns attach-agent $node $udp($i)
$ns attach-agent $hub $null($i)
$ns connect $udp($i) $null($i)
}
# Create CBR traffic applications (1 per node)
foreach i {0 1 2 3} {
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 500
$cbr($i) set interval_ 0.5
$cbr($i) attach-agent $udp($i)
}
# Token-passing logic: each node transmits for 1 second
set time 0.5
for {set i 0} {$i < 4} {incr i} {
$ns at $time "$cbr($i) start"
set stoptime [expr $time + 1.0]
$ns at $stoptime "$cbr($i) stop"
set time $stoptime
}
# End simulation
$ns at [expr $time + 0.5] "finish"
# Finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
exit 0
}
# Run the simulation
$ns run
14
Output Simulation:
15
Experiment - 6
Objective : Simulation study of Token Ring LAN protocol.
Code Implementation:
# Token Ring Protocol Simulation in NS2.35
set ns [new Simulator]
# Create trace and NAM files
set tracefile [open token_ring.tr w]
$ns trace-all $tracefile
set namfile [open token_ring.nam w]
$ns namtrace-all $namfile
# Define number of nodes
set num_nodes 5
# Create nodes
for {set i 0} {$i < $num_nodes} {incr i} {
set n($i) [$ns node]
}
# Create a ring topology (each node connects to the next, last connects to first)
for {set i 0} {$i < $num_nodes} {incr i} {
set next [expr ($i + 1) % $num_nodes]
$ns duplex-link $n($i) $n($next) 1Mb 10ms DropTail
$ns duplex-link-op $n($i) $n($next) orient right
}
# Create UDP agents and attach to nodes
for {set i 0} {$i < $num_nodes} {incr i} {
set udp($i) [new Agent/UDP]
$ns attach-agent $n($i) $udp($i)
set null($i) [new Agent/Null]
set recv_index [expr ($i + 1) % $num_nodes]
$ns attach-agent $n($recv_index) $null($i)
$ns connect $udp($i) $null($i)
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 500
$cbr($i) set interval_ 0.1
$cbr($i) set random_ false
$cbr($i) attach-agent $udp($i)
}
16
# Define token passing logic
set token_time 0.5
set start_time 1.0
for {set i 0} {$i < $num_nodes} {incr i} {
set t [expr $start_time + $i * $token_time]
$ns at $t "$cbr($i) start"
$ns at [expr $t + 0.3] "$cbr($i) stop"
}
# End simulation
$ns at [expr $start_time + $num_nodes * $token_time + 1] "finish"
# Finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam token_ring.nam &
exit 0
}
# Run the simulation
$ns run
Output Simulation:
17
Experiment - 7
Objective : Simulation study of WAN protocol like Frame Relay, X. 25.
Code Implementation:
# Frame Relay/X.25 WAN Emulation in NS2
set ns [new Simulator]
# Output files
set tracefile [open [Link] w]
$ns trace-all $tracefile
set namfile [open [Link] w]
$ns namtrace-all $namfile
# Create nodes
set siteA [$ns node]
set siteB [$ns node]
set siteC [$ns node]
set fr_switch [$ns node] ;# Acts like a frame relay switch
# Create links (simulate WAN links with delays)
$ns duplex-link $siteA $fr_switch 512Kb 50ms DropTail
$ns duplex-link $siteB $fr_switch 512Kb 50ms DropTail
$ns duplex-link $siteC $fr_switch 512Kb 50ms DropTail
# Position nodes in NAM
$ns duplex-link-op $siteA $fr_switch orient right-up
$ns duplex-link-op $siteB $fr_switch orient right
$ns duplex-link-op $siteC $fr_switch orient right-down
# Create UDP agents (simulate data over VC)
set udpA [new Agent/UDP]
$ns attach-agent $siteA $udpA
set nullB [new Agent/Null]
$ns attach-agent $siteB $nullB
# Create a virtual circuit from Site A -> Site B
$ns connect $udpA $nullB
# Application traffic on VC
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 300
$cbr set interval_ 0.1
$cbr attach-agent $udpA
# Start/stop traffic
18
$ns at 1.0 "$cbr start"
$ns at 5.0 "$cbr stop"
# Another VC: Site C → Site A
set udpC [new Agent/UDP]
$ns attach-agent $siteC $udpC
set nullA [new Agent/Null]
$ns attach-agent $siteA $nullA
$ns connect $udpC $nullA
set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 300
$cbr2 set interval_ 0.15
$cbr2 attach-agent $udpC
$ns at 2.0 "$cbr2 start"
$ns at 6.0 "$cbr2 stop"
# End simulation
$ns at 7.0 "finish"
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
exit 0
} Output Simulation:
$ns run
19
Experiment - 8
Objective : Study of 802. 11 wireless LAN protocols.
Code Implementation:
# NS2 simulation of IEEE 802.11 Wireless LAN with colored traffic flows
set ns [new Simulator]
# Trace files
set tracefile [open [Link] w]
$ns trace-all $tracefile
set namfile [open [Link] w]
$ns namtrace-all-wireless $namfile 500 500
# Define topology boundary
set val(x) 500
set val(y) 500
# Define number of nodes
set num_nodes 5
# Create a channel
set chan [new Channel/WirelessChannel]
# Topography
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Wireless node configuration
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON
# Create GOD (required for wireless simulations)
20
create-god $num_nodes
# Create nodes
for {set i 0} {$i < $num_nodes} {incr i} {
set node($i) [$ns node]
$node($i) random-motion 0 ;# disable random motion
}
# Define initial positions and labels
$node(0) set X_ 100; $node(0) set Y_ 100
$node(1) set X_ 400; $node(1) set Y_ 100
$node(2) set X_ 250; $node(2) set Y_ 250
$node(3) set X_ 100; $node(3) set Y_ 400
$node(4) set X_ 400; $node(4) set Y_ 400
# NAM configuration (only label the nodes, color is not supported for wireless nodes)
for {set i 0} {$i < $num_nodes} {incr i} {
$ns at 0.0 "$node($i) label Node-$i"
}
# Define TCP flow setup
proc setup_tcp_flow {src dst flow_color} {
global ns node
set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node($src) $tcp
$ns attach-agent $node($dst) $sink
$ns connect $tcp $sink
$tcp set fid_ $flow_color
set ftp [new Application/FTP]
$ftp attach-agent $tcp
return $ftp
}
# Setup three flows
set ftp1 [setup_tcp_flow 0 2 1] ;# red
set ftp2 [setup_tcp_flow 1 3 2] ;# green
set ftp3 [setup_tcp_flow 4 2 3] ;# blue
# Start traffic
$ns at 1.0 "$ftp1 start"
$ns at 2.0 "$ftp2 start"
$ns at 3.0 "$ftp3 start"
# Optional mobility
21
$ns at 0.5 "$node(0) setdest 200 200 10"
$ns at 1.0 "$node(1) setdest 300 300 10"
$ns at 1.5 "$node(4) setdest 200 350 15"
# Finish
$ns at 10.0 "finish"
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
exit 0
}
# Run simulation
$ns run
Output Simulation:
22
Experiment - 9
Objective : Implement the Distance Vector Routing protocol for finding the shortest path.
Code Implementation:
# Distance Vector Routing Simulation in NS2
# Create simulator
set ns [new Simulator]
# Open trace files
set tracefile [open [Link] w]
$ns trace-all $tracefile
set namfile [open [Link] w]
$ns namtrace-all $namfile
# Define nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Positioning for NAM
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 30ms DropTail ;# Higher cost path
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n2 $n3 orient down
$ns duplex-link-op $n0 $n2 orient down-right
# Set routing protocol to Distance Vector
$ns rtproto DV
# Attach UDP agents
set udp0 [new Agent/UDP]
set null3 [new Agent/Null]
$ns attach-agent $n0 $udp0
$ns attach-agent $n3 $null3
$ns connect $udp0 $null3
# CBR traffic over UDP
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 1000
23
$cbr0 set interval_ 0.5
$cbr0 attach-agent $udp0
# Start simulation
$ns at 1.0 "$cbr0 start"
$ns at 5.0 "$cbr0 stop"
# Finish
$ns at 6.0 "finish"
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam [Link] &
exit 0
}
$ns run
Output Simulation:
24
Experiment - 10
Objective : Write a program to connect server with client and passes information from one system to another
and vice versa that by creating / establishing connection.
Code Implementation:
1. Server –
import socket
# Create a TCP/IP socket
server_socket = [Link](socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific address and port
server_host = '[Link]' # Listens on all interfaces
server_port = 12345
server_socket.bind((server_host, server_port))
# Listen for incoming connections
server_socket.listen(1)
print(f"[+] Server listening on port {server_port}...")
# Accept a connection
conn, addr = server_socket.accept()
print(f"[+] Connection established with {addr}")
while True:
# Receive message from client
data = [Link](1024).decode()
if [Link]() == 'exit':
print("[-] Client disconnected.")
break
print(f"Client says: {data}")
# Send reply to client
reply = input("Server reply: ")
[Link]([Link]())
[Link]()
server_socket.close()
2. Client –
import socket
# Create a TCP/IP socket
25
client_socket = [Link](socket.AF_INET, socket.SOCK_STREAM)
# Enter the server's IP address here
server_ip = input("[Link]")
server_port = 12345
# Connect to server
client_socket.connect((server_ip, server_port))
print("[+] Connected to the server.")
while True:
# Send message to server
message = input("You: ")
client_socket.send([Link]())
if [Link]() == 'exit':
break
# Receive server's reply
data = client_socket.recv(1024).decode()
print(f"Server says: {data}")
client_socket.close()
Output Simulation:
26