Practical File
Practical File
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.
Command Description
Command Description
$ns duplex-link-op $n0 $n1 orient right Sets orientation of links in NAM
exec nam filename.nam & 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]
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
3
# Define finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam wired.nam &
exit 0
}
Output Simulation:
2. Wireless Simulation
# Create simulator
set ns [new Simulator]
4
set tracefile [open wireless.tr w]
$ns trace-all $tracefile
set namfile [open wireless.nam w]
$ns namtrace-all $namfile
# Set up topography
set topo [new Topography]
$topo load_flatgrid 500 500
5
set null [new Agent/Null]
$ns attach-agent $n1 $null
# Finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam wireless.nam &
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]
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
# Links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n2 $n1 1Mb 10ms DropTail
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
# 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 aloha.nam &
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]
# Number of nodes
set num_nodes 5
# Topography
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
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
}
# 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)
}
}
# 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
}
Output Simulation:
12
Experiment - 5
Objective : Simulation study of Token Bus LAN protocol.
Code Implementation:
# Token Bus Protocol Simulation in NS2 (fixed layout)
# Output files
set tracefile [open tokenbus.tr w]
$ns trace-all $tracefile
# Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns 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)
}
# 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 tokenbus.nam &
exit 0
}
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 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
}
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 wan.tr w]
$ns trace-all $tracefile
set namfile [open wan.nam 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
# 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"
# End simulation
$ns at 7.0 "finish"
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam wan.nam &
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
# Trace files
set tracefile [open wlan.tr w]
$ns trace-all $tracefile
set namfile [open wlan.nam w]
$ns namtrace-all-wireless $namfile 500 500
# Create a channel
set chan [new Channel/WirelessChannel]
# Topography
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
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
}
# 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"
}
# 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 wlan.nam &
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]
# Define nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
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 dv.nam &
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
# Accept a connection
conn, addr = server_socket.accept()
print(f"[+] Connection established with {addr}")
while True:
# Receive message from client
data = conn.recv(1024).decode()
if data.lower() == 'exit':
print("[-] Client disconnected.")
break
print(f"Client says: {data}")
conn.close()
server_socket.close()
2. Client –
import socket
25
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 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(message.encode())
if message.lower() == 'exit':
break
client_socket.close()
Output Simulation:
26