0% found this document useful (0 votes)
7 views34 pages

Computer network lab manual

This document outlines practical exercises for installing and configuring NS2 and QualNet, creating network simulations, and studying basic network commands. It includes detailed steps for setting up both simulators on different platforms, as well as coding examples for wired, wireless, and hybrid network simulations in NS2. Additionally, it covers the implementation of new protocols and the simulation study of the pure ALOHA protocol.

Uploaded by

xyz70y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views34 pages

Computer network lab manual

This document outlines practical exercises for installing and configuring NS2 and QualNet, creating network simulations, and studying basic network commands. It includes detailed steps for setting up both simulators on different platforms, as well as coding examples for wired, wireless, and hybrid network simulations in NS2. Additionally, it covers the implementation of new protocols and the simulation study of the pure ALOHA protocol.

Uploaded by

xyz70y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Practical-1

Aim: Installation and Configuration of NS2 and QualNet.

Objective:

To understand and perform the installation and basic configuration of:

• NS2 (Network Simulator 2)


• QualNet (Quality Networking Simulator)

Software Requirements:

Software Version Platform


NS2 2.35 or latest Ubuntu/Linux (Recommended)
QualNet 7.4 or latest Windows/Linux

PART A: Installation and Configuration of NS2

Steps to Install NS2 on Ubuntu:

1. Open Terminal
2. Install Required Dependencies:
❖ bash
❖ CopyEdit
❖ sudo apt-get update
sudo apt-get install build-essential autoconf automake libxmu-dev

3. Download NS2 All-in-One Package:


❖ bash
❖ CopyEdit
❖ wget https://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35.tar.gz

4. Extract the Package:


❖ bash
❖ CopyEdit
❖ tar -xvzf ns-allinone-2.35.tar.gz
cd ns-allinone-2.35

5. Install NS2:
❖ bash
❖ CopyEdit
❖ ./install
6. Set Environment Variables:
Add these lines to your .bashrc:
❖ bash
❖ CopyEdit
❖ export PATH=$PATH:/home/your_username/ns-allinone-2.35/bin:/home/your_username/ns-
allinone-2.35/tcl8.5.10/unix:/home/your_username/ns-allinone-2.35/tk8.5.10/unix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/your_username/ns-allinone-
2.35/otcl-1.14:/home/your_username/ns-allinone-2.35/lib

Then run:

❖ bash
❖ CopyEdit
❖ source ~/.bashrc

7. Verify Installation:
❖ bash
❖ CopyEdit
❖ ns

It should open the % NS2 command prompt.

PART B: Installation and Configuration of QualNet

Note: QualNet is a commercial simulator. Make sure you have a valid license or trial version.

Steps to Install QualNet on Windows:

1. Download QualNet:
a. From official site: https://web.scalable-networks.com
2. Install the Software:
a. Run the installer and follow the instructions.
b. Choose the required components (e.g., Developer Mode, GUI, etc.)
3. License Setup:
a. Place the license file in the required directory.
b. Configure the license in the QualNet_License_Manager.
4. Verify Installation:
a. Open QualNet GUI
b. Run sample simulation scenarios to verify successful configuration
Practical-2

Aim: Creating a network: nodes, links and queues, Creating connections, traffic and computing
routers Insertion of errors and analysis of trace file.
# Create simulator object

set ns [new Simulator]

# Create trace files

set tracefile [open out.tr w]

$ns trace-all $tracefile

set namfile [open out.nam w]

$ns namtrace-all $namfile

# Create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

# Create duplex links

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

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

# Setup queue limits

$ns queue-limit $n1 $n2 50

# Setup TCP agent on node n0

set tcp [new Agent/TCP]

$ns attach-agent $n0 $tcp

$tcp set fid_ 1 ;# flow ID for NAM

# Setup TCPSink agent on node n2

set sink [new Agent/TCPSink]

$ns attach-agent $n2 $sink

# Connect TCP to sink

$ns connect $tcp $sink


# Add tracing for agents

$ns add-agent-trace $tcp "tcp"

$ns add-agent-trace $sink "sink"

# Setup FTP traffic over TCP

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns at 0.5 "$ftp start"

# Define error model and attach to n1 -> n2

set err [new ErrorModel]

$err set rate_ 0.01

$err unit pkt

$err set drop-target [$ns set nullAgent_]

$ns lossmodel $err $n1 $n2

# Finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

# Schedule finish

$ns at 5.0 "finish"

# Run simulation

$ns run
Output:
Practical-3

Aim: Study of Basic Network Commands and Network Configuration Commands.

Objective:

To understand and use basic network and configuration commands used for diagnosing and managing network
settings in Linux and Windows operating systems.

Software Requirements:

Tool/OS Version
Windows 10/11
Linux Ubuntu 20.04 or later
Terminal/Command Prompt -

PART A: Basic Network Commands

Command Platform Description


Test network connectivity
ping Windows/Linux
between hosts
tracert (Windows) / traceroute Traces the route packets
(Linux) take to a host
ipconfig Windows Displays IP configuration
Displays network interfaces and
ifconfig / ip a Linux
IP addresses
Queries DNS for domain
nslookup Windows/Linux
name/IP mapping
Displays active connections and
netstat Windows/Linux
listening ports
Displays and modifies ARP
arp Windows/Linux
cache
Displays or sets the system
hostname Windows/Linux
hostname

PART B: Network Configuration Commands

On Linux:

Command Purpose
Enable or disable network
sudo ifconfig eth0 up/down
interface
Get IP address from DHCP
sudo dhclient
server
nmcli or nmtui Manage network connections
ip route View routing table
sudo ip link set eth0 address XX:XX:XX:XX:XX:XX Set MAC address
On Windows:

Command Purpose
netsh interface ip set address Manually configure IP address
netsh interface show interface Show all interfaces
netsh wlan show profile Display saved Wi-Fi profiles
netsh winsock reset Reset TCP/IP stack
route print Show routing table

Procedure:

1. Open Command Prompt (Windows) or Terminal (Linux).


2. Run each command listed above one-by-one.
3. Observe the output and understand its significance.
4. Record IP address, DNS, Gateway, and interface details.
5. Try to disable and re-enable the interface and observe connectivity.

Sample Execution (Linux):

bash
CopyEdit
ping www.google.com
ifconfig
traceroute www.google.com
nslookup www.example.com
ip route

Sample Execution (Windows):

cmd
CopyEdit
ping www.google.com
ipconfig
tracert www.google.com
nslookup www.example.com
netstat -an
Observations:
Command Output Summary Use
ping google.com Successful response from IP Checks connectivity
ipconfig Shows IPv4, gateway View network settings
netstat -an Shows listening ports Check open connections
Result:

Successfully studied and executed basic network and configuration commands. Gained practical experience in
monitoring and managing network settings.
Practical-4

Aim: Simple project on NS2 – wired, wireless and combination of wired and wireless.

#Wired:
set ns [new Simulator]
set nf [open wired.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
$tcp set fid_ 1
set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 0.5 "$ftp start"
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam wired.nam &
exit 0
}
$ns at 5.0 "finish"
$ns run
Output:

#Wireless:
# Create simulator
set ns [new Simulator]
# Create a wireless channel
set chan_ [new Channel/WirelessChannel]
# Create trace files
set tracefile [open wireless_trace.tr w]
$ns trace-all $tracefile
set namfile [open wireless.nam w]
$ns namtrace-all-wireless $namfile 500 500
# Define topology
set topo [new Topography]
$topo load_flatgrid 500 500
# Create General Operations Director
create-god 2
# Configure wireless nodes
$ns node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channel $chan_ \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
# Set static node positions (no mobility)
$n0 set X_ 100; $n0 set Y_ 100; $n0 set Z_ 0
$n1 set X_ 200; $n1 set Y_ 100; $n1 set Z_ 0
$ns initial_node_pos $n0 20
$ns initial_node_pos $n1 20
# Create UDP agent and attach to n0
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
# Create null agent and attach to n1
set null [new Agent/Null]
$ns attach-agent $n1 $null
# Connect the agents
$ns connect $udp $null
# Add CBR traffic source
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 500
$cbr set interval_ 0.01
$cbr set maxpkts_ 1000
$cbr attach-agent $udp
# Start the traffic
$ns at 1.0 "$cbr start"
# Finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam wireless.nam &
exit 0
}
# Schedule finish
$ns at 5.0 "finish"
# Run simulation
$ns run

Output:

#Hybrid: combination of wired and wireless


# Create a simulator object
set ns [new Simulator]
# Define trace files for NAM and simulation trace
set tracefile [open out.tr w]
$ns trace-all $tracefile
set nf [open hybrid.nam w]
$ns namtrace-all-wireless $nf 500 500
# Define topology and create a flat grid for wireless nodes
set topo [new Topography]
$topo load_flatgrid 500 500
# Create God for mobile nodes (set number of nodes to 4)
create-god 4
# Wireless configuration (using common settings for mobile nodes)
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
# Create nodes (4 wireless nodes)
set n0 [$ns node] ;# wireless node
set n1 [$ns node] ;# wireless node
set n2 [$ns node] ;# wireless node
set n3 [$ns node] ;# wireless node
# Set initial positions for wireless nodes (ensure nodes are within range)
$n0 set X_ 100 ; $n0 set Y_ 100 ; $n0 set Z_ 0
$n1 set X_ 200 ; $n1 set Y_ 100 ; $n1 set Z_ 0
$n2 set X_ 300 ; $n2 set Y_ 100 ; $n2 set Z_ 0
$n3 set X_ 400 ; $n3 set Y_ 100 ; $n3 set Z_ 0
$ns initial_node_pos $n0 20
$ns initial_node_pos $n1 20
$ns initial_node_pos $n2 20
$ns initial_node_pos $n3 20
# Add mobility for nodes if needed
# Set the mobility model, in case you want nodes to move
# Example: Move n0 from position (100,100) to (400,100) in 10 seconds
$ns at 5.0 "$n0 set X_ 400; $n0 set Y_ 100"
$ns at 6.0 "$n1 set X_ 400; $n1 set Y_ 100"
$ns at 7.0 "$n2 set X_ 400; $n2 set Y_ 100"
# Define a wired link (example: between nodes n2 and n3)
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
# Setup UDP agents
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
# Connect the UDP agent to Null agent
$ns connect $udp $null
# Define constant bit rate (CBR) traffic
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 500
$cbr set interval_ 0.01
$cbr attach-agent $udp
$ns at 1.0 "$cbr start"
# Finish procedure to close trace and run NAM visualization
proc finish {} {global ns tracefile nf
$ns flush-trace
close $tracefile
close $nf
exec nam hybrid.nam &
exit 0
}
# Finish after 5 seconds
$ns at 5.0 "finish"
# Run the simulation
$ns run
Output:
Practical-5
Aim: Implementation of new protocols in NS2.
# Create a simulator object
set ns [new Simulator]
# Open NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
# Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
# Create two nodes
set n0 [$ns node]
set n1 [$ns node]
# Create a link between nodes (bandwidth, delay, queue type)
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
# Create a UDP agent and attach it to node n0
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
# Create a Null agent (sink) and attach to node n1
set null [new Agent/Null]
$ns attach-agent $n1 $null
# Connect the two agents
$ns connect $udp $null
# Create a CBR (Constant Bit Rate) traffic source and attach it to UDP
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 500
$cbr set interval_ 0.5
$cbr attach-agent $udp
# Start and stop the traffic
$ns at 1.0 "$cbr start"
$ns at 4.0 "$cbr stop"
# End simulation at 5.0 seconds
$ns at 5.0 "finish"
# Run the simulation
$ns run
Output:
Practical-6
Aim: Simulation study of pure ALOHA protocol.
# Create a simulator object
set ns [new Simulator]
# Define simulation area
set topo [new Topography]
$topo load_flatgrid 400 400
# Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
# Create nodes
set nodeCount 25
for {set i 0} {$i < $nodeCount} {incr i} {
set n($i) [$ns node]
# Randomly place nodes in 400x400 area
$n($i) set X_ [expr {int(rand()*400)}]
$n($i) set Y_ [expr {int(rand()*400)}]
}
# Create a central receiver node (for star topology)
set receiver [$ns node]
$receiver set X_ 200
$receiver set Y_ 200
# Connect each node to the receiver
for {set i 0} {$i < $nodeCount} {incr i} {
$ns duplex-link $n($i) $receiver 1Mb 10ms DropTail
}
# Setup UDP agents and CBR traffic for each node
for {set i 0} {$i < $nodeCount} {incr i} {
set udp($i) [new Agent/UDP]
$ns attach-agent $n($i) $udp($i)
set null($i) [new Agent/Null]
$ns attach-agent $receiver $null($i)
$ns connect $udp($i) $null($i)
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) attach-agent $udp($i)
$cbr($i) set packet_size_ 512
# Randomize rate between 100kb and 299kb
set rate [expr {int(rand()*200)+100}]
$cbr($i) set rate_ "${rate}kb"
$cbr($i) set random_ true
# Randomize start time between 0.1s and 2.1s
set startTime [expr {0.1 + rand()*2.0}]
$ns at $startTime "$cbr($i) start"
$ns at 4.5 "$cbr($i) stop"
}
# Finish procedure
proc finish {} { global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}$ns at 5.0 "finish"
$ns run
Output:
Practical-7
Aim: Simulation study of slotted ALOHA protocol.
# Create a simulator object
set ns [new Simulator]
# Define simulation area
set topo [new Topography]
$topo load_flatgrid 400 400
# Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
# Create nodes
set nodeCount 25
for {set i 0} {$i < $nodeCount} {incr i} {
set n($i) [$ns node]
# Randomly place nodes in 400x400 area
$n($i) set X_ [expr {int(rand()*400)}]
$n($i) set Y_ [expr {int(rand()*400)}]
}
# Create a central receiver node (for star topology)
set receiver [$ns node]
$receiver set X_ 200
$receiver set Y_ 200
# Connect each node to the receiver
for {set i 0} {$i < $nodeCount} {incr i} {
$ns duplex-link $n($i) $receiver 1Mb 10ms DropTail
}
# Setup UDP agents and CBR traffic for each node
for {set i 0} {$i < $nodeCount} {incr i} {
set udp($i) [new Agent/UDP]
$ns attach-agent $n($i) $udp($i)
set null($i) [new Agent/Null]
$ns attach-agent $receiver $null($i)
$ns connect $udp($i) $null($i)
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) attach-agent $udp($i)
$cbr($i) set packet_size_ 512
# Randomize rate between 100kb and 299kb
set rate [expr {int(rand()*200)+100}]
$cbr($i) set rate_ "${rate}kb"
$cbr($i) set random_ true
# Randomize start time between 0.1s and 2.1s
set startTime [expr {0.1 + rand()*2.0}]
$ns at $startTime "$cbr($i) start"
$ns at 4.5 "$cbr($i) stop"
}
# Finish procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}$ns at 5.0 "finish"
$ns run
Output:
Practical-8
Aim: Simulation study of Token Bus LAN protocol.
# Create a simulator instance
set ns [new Simulator]
# Create NAM trace file
set nf [open tokenbus3.nam w]
$ns namtrace-all $nf
# Define finish procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam tokenbus3.nam &
exit 0
}
# Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
# Create links to form a logical ring
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n0 1Mb 10ms DropTail
# Create UDP and Null agents
for {set i 0} {$i < 3} {incr i} { set udp($i) [new Agent/UDP]
set null($i) [new Agent/Null]
set node [set n$i]
$ns attach-agent $node $udp($i)
$ns attach-agent $node $null($i)
}
# Connect each UDP agent to the next Null agent (logical ring)
$ns connect $udp(0) $null(1)
$ns connect $udp(1) $null(2)
$ns connect $udp(2) $null(0)
# Create CBR traffic for each node
for {set i 0} {$i < 3} {incr i} {
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 500
$cbr($i) set interval_ 0.5
$cbr($i) attach-agent $udp($i)
}
# Simulate token passing (only one CBR active at a time)
$ns at 1.0 "$cbr(0) start"
$ns at 2.0 "$cbr(0) stop"
$ns at 2.1 "$cbr(1) start"
$ns at 3.1 "$cbr(1) stop"
$ns at 3.2 "$cbr(2) start"
$ns at 4.2 "$cbr(2) stop"
# Finish simulation
$ns at 5.0 "finish
# Run the simulation
$ns run
Output:
Practical-9
Aim: Simulation study of Token Ring LAN protocol.
# Create a simulator instance
set ns [new Simulator]
# Open NAM trace file
set nf [open tokenring6.nam w]
$ns namtrace-all $nf
# Define the finish procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam tokenring6.nam &
exit 0
}
# Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
# Form a logical ring using duplex links
$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 $n3 $n4 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
$ns duplex-link $n5 $n0 1Mb 10ms DropTail
# Create UDP and Null agents, attach to nodes
for {set i 0} {$i < 6} {incr i} {
set udp($i) [new Agent/UDP]
set null($i) [new Agent/Null]
set node [set n$i]
$ns attach-agent $node $udp($i)
$ns attach-agent $node $null($i)
}
# Connect each UDP to the next Null in the ring
$ns connect $udp(0) $null(1)
$ns connect $udp(1) $null(2)
$ns connect $udp(2) $null(3)
$ns connect $udp(3) $null(4)
$ns connect $udp(4) $null(5)
$ns connect $udp(5) $null(0)
# Define CBR traffic for each UDP agent
for {set i 0} {$i < 6} {incr i} {
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 500
$cbr($i) set interval_ 0.5
$cbr($i) attach-agent $udp($i)
}
# Simulate token passing: only one node sends at a time
set start_time 1.0
set duration 1.0
for {set i 0} {$i < 6} {incr i} {
set stop_time [expr {$start_time + $duration}]
$ns at $start_time "$cbr($i) start"
$ns at $stop_time "$cbr($i) stop"
set start_time [expr {$stop_time + 0.1}]
}
# End the simulation
$ns at [expr {$start_time + 1.0}] "finish"
# Run the simulation
$ns run
Output:
Practical-10
Aim: Simulation study of WAN protocol like Frame Relay, X. 25.
# Create a simulator object
set ns [new Simulator]
# Open NAM trace file
set nf [open wan_relay.nam w]
$ns namtrace-all $nf
# Define finish procedure
proc finish {} { global ns nf
$ns flush-trace
close $nf
exec nam wan_relay.nam &
exit 0
}# Create nodes
set DTE1 [$ns node]
set DTE2 [$ns node]
set DTE3 [$ns node]
set Switch [$ns node]
# Create links (PVCs): DTEs <-> Frame Relay Switch (DCE)
$ns duplex-link $DTE1 $Switch 512Kb 20ms DropTail
$ns duplex-link $DTE2 $Switch 512Kb 20ms DropTail
$ns duplex-link $DTE3 $Switch 512Kb 20ms DropTail
# Create UDP agents and Null agents
set udp1 [new Agent/UDP]
set null1 [new Agent/Null]
$ns attach-agent $DTE1 $udp1
$ns attach-agent $DTE2 $null1
$ns connect $udp1 $null1
set udp2 [new Agent/UDP]
set null2 [new Agent/Null]
$ns attach-agent $DTE2 $udp2
$ns attach-agent $DTE3 $null2
$ns connect $udp2 $null2
# Application Layer (simulate data transfer like Frame Relay PVC)
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 1000
$cbr1 set interval_ 0.5
$cbr1 attach-agent $udp1
set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 800
$cbr2 set interval_ 0.6
$cbr2 attach-agent $udp2
# Start traffic (simulate packet switching)
$ns at 1.0 "$cbr1 start"
$ns at 2.0 "$cbr2 start"
$ns at 4.0 "$cbr1 stop"
$ns at 5.0 "$cbr2 stop"
# Finish simulation
$ns at 6.0 "finish"
# Run the simulation
$ns run
Output:
Practical-11
Aim: Study of 802. 11 wireless LAN protocols.
# Create a simulator instance
set ns [new Simulator]
# Define trace file for NAM
set tracefile [open wireless_out.tr w]
$ns trace-all $tracefile
# Define NAM output for wireless visualization
set namfile [open wireless_out.nam w]
$ns namtrace-all-wireless $namfile 500 500
# Define simulation parameters
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(llLL
set val(ant) Antenna/OmniAntenna
set val(x) 500
set val(y) 500
set val(nn)3
set val(stop)10.0
# Create topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Create a GOD object
create-god $val(nn)
# Configure nodes before creating them
$ns node-config -adhocRouting DSDV \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen 50 \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON
# Create 3 wireless nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
# Initial positions for NAM
$n0 set X_ 100
$n0 set Y_ 200
$n0 set Z_ 0.0
$n1 set X_ 200
$n1 set Y_ 250
$n1 set Z_ 0.0
$n2 set X_ 300
$n2 set Y_ 200
$n2 set Z_ 0.0
# Setup communication: node 0 → node 2 via node 1
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
set null [new Agent/Null]
$ns attach-agent $n2 $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 1000
$cbr set interval_ 0.5
$cbr attach-agent $udp
# Optional: add node movement
$ns at 1.0 "$n1 setdest 250 220 5.0"
$ns at 2.0 "$n2 setdest 200 200 10.0"
# Start/Stop application
$ns at 2.0 "$cbr start"
$ns at 8.0 "$cbr stop"
# Finish procedure
proc finish {} {global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam wireless_out.nam &
exit 0
}# Schedule finish
$ns at $val(stop) "finish"
# Run simulation
$ns run
Output:
Practical-12
Aim: Implement the Distance Vector Routing protocol for finding the shortest path.
# Define the number of nodes
set N 4
# Define the initial distance matrix (INF = 999)
set INF 999
array set dist {}
# Initialize distance between nodes
# Graph:
# 0 --1--> 1
# 1 --3--> 2
# 0 --4--> 2
# 2 --2--> 3
# Initialize all distances to INF
for {set i 0} {$i < $N} {incr i} {
for {set j 0} {$j < $N} {incr j} {
if {$i == $j} { set dist($i,$j) 0
} else {set dist($i,$j) $INF}}
}
# Define direct connections
set dist(0,1) 1
set dist(1,0) 1
set dist(1,2) 3
set dist(2,1) 3
set dist(0,2) 4
set dist(2,0) 4
set dist(2,3) 2
set dist(3,2) 2
# Print initial distances
puts "Initial Distance Table:"
for {set i 0} {$i < $N} {incr i} {
for {set j 0} {$j < $N} {incr j} {
puts -nonewline "[format %4s $dist($i,$j)]"}
puts ""
}
# Distance Vector Algorithm (Bellman-Ford)
for {set k 0} {$k < $N} {incr k} {
for {set i 0} {$i < $N} {incr i} {
for {set j 0} {$j < $N} {incr j} {
if {$dist($i,$j) > [expr {$dist($i,$k) + $dist($k,$j)}]} {
set dist($i,$j) [expr {$dist($i,$k) + $dist($k,$j)}]
}}}
}
# Print final distances
puts "\nDistance Table after Distance Vector Routing:"
for {set i 0} {$i < $N} {incr i} {
for {set j 0} {$j < $N} {incr j} {
if {$dist($i,$j) == $INF} {
puts -nonewline " INF"
} else { puts -nonewline "[format %4s $dist($i,$j)]"}
}
puts ""
}
Output:
Practical-13
Aim: Write a program to connect server with client and passes information from one system to
another and vice versa that by creating / establishing connection.
#Server.java

import java.io.*;

import java.net.*;

public class Server{

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(5000);

System.out.println("Server started. Waiting for client...");

Socket socket = serverSocket.accept();

System.out.println("Client connected.");

BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter output = new PrintWriter(socket.getOutputStream(), true);

BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

String clientMsg, serverMsg;

while (true) {

clientMsg = input.readLine();

if (clientMsg.equalsIgnoreCase("bye")) {

System.out.println("Client disconnected.");

break;

System.out.println("Client: " + clientMsg);

System.out.print("Server: ");

serverMsg = keyboard.readLine();

output.println(serverMsg);

socket.close();
serverSocket.close();

Output:

#Client.java

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) throws IOException {

Socket socket = new Socket("localhost", 5000);

System.out.println("Connected to server.");

BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter output = new PrintWriter(socket.getOutputStream(), true);

BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

String clientMsg, serverMsg;

while (true) {

System.out.print("Client: ");

clientMsg = keyboard.readLine();

output.println(clientMsg);

if (clientMsg.equalsIgnoreCase("bye")) {

break;

serverMsg = input.readLine();
System.out.println("Server: " + serverMsg);

socket.close();

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy