0% found this document useful (0 votes)
20 views27 pages

CN External

Uploaded by

Greeshmareddy5
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)
20 views27 pages

CN External

Uploaded by

Greeshmareddy5
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/ 27

wireless network of at least 10 nodes with TCP between node n0 to n3 and UDP between

n1 to n3. give all the nodes labels and colours for the above wirless communication

# Define simulation parameters


set val(chan) Channel/WirelessChannel ;# Channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ll) LL ;# Link layer type
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(ifqlen) 50 ;# Max packet queue length
set val(nn) 10 ;# Number of nodes
set val(rp) DSDV ;# Routing protocol
set val(x) 500 ;# X dimension of topology
set val(y) 500 ;# Y dimension of topology
set val(stop) 100 ;# Simulation stop time

# Create Simulator
set ns [new Simulator]

# Define trace and NAM files


set tracefd [open tcp_udp.tr w]
set namtrace [open tcp_udp.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)

# Setup topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

# Configure the nodes


$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Create nodes
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
}

# Set initial positions for nodes


$node_(0) set X_ 50.0; $node_(0) set Y_ 450.0; $node_(0) set Z_ 0.0
$node_(1) set X_ 100.0; $node_(1) set Y_ 400.0; $node_(1) set Z_ 0.0
$node_(2) set X_ 200.0; $node_(2) set Y_ 350.0; $node_(2) set Z_ 0.0
$node_(3) set X_ 300.0; $node_(3) set Y_ 300.0; $node_(3) set Z_ 0.0
$node_(4) set X_ 400.0; $node_(4) set Y_ 250.0; $node_(4) set Z_ 0.0
$node_(5) set X_ 450.0; $node_(5) set Y_ 200.0; $node_(5) set Z_ 0.0
$node_(6) set X_ 50.0; $node_(6) set Y_ 150.0; $node_(6) set Z_ 0.0
$node_(7) set X_ 100.0; $node_(7) set Y_ 100.0; $node_(7) set Z_ 0.0
$node_(8) set X_ 200.0; $node_(8) set Y_ 50.0; $node_(8) set Z_ 0.0
$node_(9) set X_ 300.0; $node_(9) set Y_ 50.0; $node_(9) set Z_ 0.0

# Assign colors and labels


for {set i 0} {$i < $val(nn)} {incr i} {
$node_($i) color Blue
$ns at 0.5 "$node_($i) label Node$i"
}

# TCP communication: Node 0 to Node 3


set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(3) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.0 "$ftp start"

# UDP communication: Node 1 to Node 3


set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $node_(1) $udp
$ns attach-agent $node_(3) $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 512
$cbr set interval_ 0.1
$cbr attach-agent $udp
$ns at 2.0 "$cbr start"

# End simulation
$ns at $val(stop) "stop"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam tcp_udp.nam &
exit 0
}

# Run simulation
$ns run

Creation of min 10nodes 4 Tx and 4 Rx for wireless communication

# Simulation Parameters
set val(chan) Channel/WirelessChannel ;# Channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ll) LL ;# Link layer type
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(ifqlen) 50 ;# Max packet queue length
set val(nn) 10 ;# Number of nodes
set val(rp) DSDV ;# Routing protocol
set val(x) 600 ;# X dimension of topology
set val(y) 600 ;# Y dimension of topology
set val(stop) 50 ;# Simulation stop time

# Create Simulator
set ns [new Simulator]

# Define trace and NAM files


set tracefd [open wireless_comm.tr w]
set namtrace [open wireless_comm.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)

# Setup topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

# Configure the nodes


$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

# Create 10 nodes
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) color Blue
$ns at 0.5 "$node_($i) label Node$i"

# Assign random positions


set x [expr rand() * $val(x)]
set y [expr rand() * $val(y)]
$node_($i) set X_ $x
$node_($i) set Y_ $y
$node_($i) set Z_ 0.0
}

# Define Transmitters (Tx) and Receivers (Rx)


set tx_nodes {0 2 4 6} ;# Nodes 0, 2, 4, 6 are transmitters
set rx_nodes {1 3 5 7} ;# Nodes 1, 3, 5, 7 are receivers

# Create wireless communication flows


set fid 1
foreach tx $tx_nodes rx $rx_nodes {
# Create UDP Agents
set udp [new Agent/UDP]
set sink [new Agent/LossMonitor]
$ns attach-agent $node_($tx) $udp
$ns attach-agent $node_($rx) $sink
$ns connect $udp $sink

# Create CBR Application


set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$udp set fid_ $fid
$ns at 1.0 "$cbr start"
$ns at 20.0 "$cbr stop"

# Assign flow colors


$ns at 0.0 "$ns color $fid Red"
incr fid
}

# End simulation setup


$ns at $val(stop) "stop"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam wireless_comm.nam &
exit 0
}

# Run simulation
$ns run
create a random topology with 2 tcps and give different colors for the flows also create a
distance vector in it

set ns [new Simulator]


set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
$ns namtrace-all $nf

# Finish procedure
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}

# Create 12 nodes with random positions


for { set i 0 } { $i < 12 } { incr i } {
set n($i) [$ns node]
set x [expr rand() * 500]
set y [expr rand() * 500]
$n($i) set X_ $x
$n($i) set Y_ $y
$n($i) set Z_ 0.0
$ns at 0.5 "$n($i) label Node$i"
}

# Create duplex links


for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail
}
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail

# Create UDP flows


set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]


$ns attach-agent $n(5) $null0
$ns connect $udp0 $null0

set udp1 [new Agent/UDP]


$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

set null1 [new Agent/Null]


$ns attach-agent $n(5) $null1
$ns connect $udp1 $null1

# Create TCP flows


set tcp0 [new Agent/TCP]
$ns attach-agent $n(2) $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n(6) $sink0
$ns connect $tcp0 $sink0

set tcp1 [new Agent/TCP]


$ns attach-agent $n(3) $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n(7) $sink1
$ns connect $tcp1 $sink1

# Set up routing
$ns rtproto DV

# Link failures and recovery


$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)

# Assign flow IDs and colors


$udp0 set fid_ 1
$udp1 set fid_ 2
$tcp0 set fid_ 3
$tcp1 set fid_ 4

$ns color 1 Red


$ns color 2 Green
$ns color 3 Blue
$ns color 4 Orange

# Start traffic
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 3.0 "$tcp0 send"
$ns at 4.0 "$tcp1 send"

# Simulation end
$ns at 45 "finish"
$ns run

Create a wireless connection with 10 nodes , 4 are connection oriented and 4 are
connectionless , and specify each node with different colours And it was like we need to
just start the function but we should not specify any stop word to stop the running
process. But running should be stopped

# Create a Simulator instance


set ns [new Simulator]
set nf [open wireless.nam w]
set tr [open wireless.tr w]
$ns trace-all $tr
$ns namtrace-all-wireless $nf 500 500

# Create a wireless topology object


set topo [new Topography]
$topo load_flatgrid 500 500

# Define channel and network parameters


set val(chan) Channel/WirelessChannel ;# Channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC protocol
set val(ifq) Queue/DropTail/PriQueue ;# Interface Queue type
set val(ll) LL ;# Link layer type
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(ifqlen) 50 ;# Max packet in IFQ
set val(nn) 10 ;# Number of nodes
set val(stop) 100 ;# Simulation stop time

# Configure the nodes


$ns node-config -adhocRouting DSDV \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF

# Create nodes and assign random positions


for {set i 0} {$i < $val(nn)} {incr i} {
set n($i) [$ns node]
set x [expr rand() * 500]
set y [expr rand() * 500]
$n($i) set X_ $x
$n($i) set Y_ $y
$n($i) set Z_ 0.0
$ns at 0.5 "$n($i) label Node$i"
}

# Assign unique colors to nodes


set colors [list Red Blue Green Orange Yellow Cyan Magenta Purple Brown Gray]
for {set i 0} {$i < $val(nn)} {incr i} {
$ns at 0.5 "$n($i) color [lindex $colors $i]"
}

# Connection-Oriented (TCP) setup


for {set i 0} {$i < 4} {incr i} {
set tcp($i) [new Agent/TCP]
set sink($i) [new Agent/TCPSink]
$ns attach-agent $n($i) $tcp($i)
$ns attach-agent $n([expr $i + 4]) $sink($i)
$ns connect $tcp($i) $sink($i)
set app($i) [new Application/Traffic/Exponential]
$app($i) attach-agent $tcp($i)
$ns at [expr $i + 1].0 "$app($i) start"
$tcp($i) set fid_ [expr $i + 1]
$ns color [expr $i + 1] [lindex $colors $i]
}

# Connectionless (UDP) setup


for {set i 4} {$i < 8} {incr i} {
set udp($i) [new Agent/UDP]
set null($i) [new Agent/Null]
$ns attach-agent $n($i) $udp($i)
$ns attach-agent $n([expr $i + 2]) $null($i)
$ns connect $udp($i) $null($i)
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) attach-agent $udp($i)
$ns at [expr $i - 3].5 "$cbr($i) start"
$udp($i) set fid_ [expr $i + 1]
$ns color [expr $i + 1] [lindex $colors $i]
}

# No explicit stop command; simulation stops based on the logic


proc finish {} {
global ns nf tr
$ns flush-trace
close $nf
close $tr
exec nam wireless.nam &
exit 0
}

# Automatically stop after the simulation period


$ns at $val(stop) "finish"
$ns run

Creation of min 10nodes 4 Tx and 4 Rx 1 connection less service And 3 connected


oriented service
# Create a Simulator instance
set ns [new Simulator]
set nf [open wireless.nam w]
set tr [open wireless.tr w]
$ns trace-all $tr
$ns namtrace-all-wireless $nf 500 500

# Create a wireless topology object


set topo [new Topography]
$topo load_flatgrid 500 500

# Define channel and network parameters


set val(chan) Channel/WirelessChannel ;# Channel type
set val(prop) Propagation/TwoRayGround ;# Propagation model
set val(netif) Phy/WirelessPhy ;# Network interface type
set val(mac) Mac/802_11 ;# MAC protocol
set val(ifq) Queue/DropTail/PriQueue ;# Interface Queue type
set val(ll) LL ;# Link layer type
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(ifqlen) 50 ;# Max packet in IFQ
set val(nn) 10 ;# Number of nodes
set val(stop) 50 ;# Simulation stop time

# Configure the nodes


$ns node-config -adhocRouting DSDV \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF

# Create nodes and assign random positions


for {set i 0} {$i < $val(nn)} {incr i} {
set n($i) [$ns node]
set x [expr rand() * 500]
set y [expr rand() * 500]
$n($i) set X_ $x
$n($i) set Y_ $y
$n($i) set Z_ 0.0
$ns at 0.5 "$n($i) label Node$i"
}

# Assign colors for better visualization


set colors [list Red Blue Green Orange Yellow Cyan Magenta Purple Brown Gray]
for {set i 0} {$i < $val(nn)} {incr i} {
$ns at 0.5 "$n($i) color [lindex $colors $i]"
}

# Connection-Oriented Services (TCP) setup


for {set i 0} {$i < 3} {incr i} {
set tcp($i) [new Agent/TCP]
set sink($i) [new Agent/TCPSink]
$ns attach-agent $n($i) $tcp($i)
$ns attach-agent $n([expr $i + 5]) $sink($i)
$ns connect $tcp($i) $sink($i)
set app($i) [new Application/Traffic/Exponential]
$app($i) attach-agent $tcp($i)
$ns at [expr $i + 1].0 "$app($i) start"
$tcp($i) set fid_ [expr $i + 1]
$ns color [expr $i + 1] [lindex $colors $i]
}

# Connectionless Service (UDP) setup


set udp0 [new Agent/UDP]
set null0 [new Agent/Null]
$ns attach-agent $n(3) $udp0
$ns attach-agent $n(8) $null0
$ns connect $udp0 $null0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
$ns at 2.0 "$cbr0 start"
$udp0 set fid_ 4
$ns color 4 Cyan

# Automatically stop the simulation


proc finish {} {
global ns nf tr
$ns flush-trace
close $nf
close $tr
exec nam wireless.nam &
exit 0
}

$ns at $val(stop) "finish"


$ns run

Ring topology using sliding window protocol

# Sliding Window Protocol with Ring Topology


set ns [new Simulator]

# Open trace and NAM files


set tr [open sliding_ring.tr w]
set nam [open sliding_ring.nam w]
$ns trace-all $tr
$ns namtrace-all $nam

# Number of nodes in the ring topology


set num_nodes 6

# Create nodes in a ring


for {set i 0} {$i < $num_nodes} {incr i} {
set n($i) [$ns node]
$ns at 0.0 "$n($i) label Node$i"
}

# Create duplex links in a ring (node i to node i+1, and last to first)
for {set i 0} {$i < $num_nodes} {incr i} {
set next_node [expr ($i + 1) % $num_nodes]
$ns duplex-link $n($i) $n($next_node) 0.2Mb 10ms DropTail
$ns duplex-link-op $n($i) $n($next_node) orient right
$ns queue-limit $n($i) $n($next_node) 10
}

# Set up sliding window mechanism (TCP) between specific nodes in the ring
set tcp [new Agent/TCP]
$tcp set windowInit_ 4
$tcp set maxcwnd_ 4
$ns attach-agent $n(0) $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n(3) $sink
$ns connect $tcp $sink

# Add an FTP application to generate traffic


set ftp [new Application/FTP]
$ftp attach-agent $tcp

# Start the sliding window mechanism


$ns at 0.1 "$ftp start"

# Annotation for sliding window operation


$ns at 0.0 "$ns trace-annotate \"Sliding Window Protocol in Ring Topology (TCP from Node0 to
Node3)\""

# Add annotations for key events


$ns at 0.1 "$ns trace-annotate \"FTP starts\""
$ns at 1.0 "$ns trace-annotate \"Sliding window in operation\""

# Finish the simulation


proc finish {} {
global ns tr nam
$ns flush-trace
close $tr
close $nam
puts "Running NAM..."
exec nam sliding_ring.nam &
exit 0
}

# Automatically stop the simulation after 5 seconds


$ns at 5.0 "finish"

# Run the simulation


$ns run

Mesh using 2 tcps and 2 udps

# Define simulator
set ns [new Simulator]

# Open trace and nam files


set tr [open mesh.tr w]
set nam [open mesh.nam w]
$ns trace-all $tr
$ns namtrace-all $nam

# Define nodes
set n(0) [$ns node]
set n(1) [$ns node]
set n(2) [$ns node]
set n(3) [$ns node]
set n(4) [$ns node]
set n(5) [$ns node]

# Create mesh links between nodes


$ns duplex-link $n(0) $n(1) 2Mb 10ms DropTail
$ns duplex-link $n(0) $n(2) 2Mb 10ms DropTail
$ns duplex-link $n(1) $n(3) 2Mb 10ms DropTail
$ns duplex-link $n(2) $n(4) 2Mb 10ms DropTail
$ns duplex-link $n(3) $n(4) 2Mb 10ms DropTail
$ns duplex-link $n(4) $n(5) 2Mb 10ms DropTail

# TCP Connection 1 (Node 0 to Node 5)


set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
$ns attach-agent $n(0) $tcp1
$ns attach-agent $n(5) $sink1
$ns connect $tcp1 $sink1

set ftp1 [new Application/FTP]


$ftp1 attach-agent $tcp1

# TCP Connection 2 (Node 1 to Node 4)


set tcp2 [new Agent/TCP]
set sink2 [new Agent/TCPSink]
$ns attach-agent $n(1) $tcp2
$ns attach-agent $n(4) $sink2
$ns connect $tcp2 $sink2

set ftp2 [new Application/FTP]


$ftp2 attach-agent $tcp2

# UDP Connection 1 (Node 2 to Node 3)


set udp1 [new Agent/UDP]
set null1 [new Agent/Null]
$ns attach-agent $n(2) $udp1
$ns attach-agent $n(3) $null1
$ns connect $udp1 $null1

set cbr1 [new Application/Traffic/CBR]


$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.01
$cbr1 attach-agent $udp1

# UDP Connection 2 (Node 3 to Node 5)


set udp2 [new Agent/UDP]
set null2 [new Agent/Null]
$ns attach-agent $n(3) $udp2
$ns attach-agent $n(5) $null2
$ns connect $udp2 $null2

set cbr2 [new Application/Traffic/CBR]


$cbr2 set packetSize_ 500
$cbr2 set interval_ 0.01
$cbr2 attach-agent $udp2

# Set colors for flows


$tcp1 set fid_ 1
$tcp2 set fid_ 2
$udp1 set fid_ 3
$udp2 set fid_ 4

$ns color 1 Red


$ns color 2 Blue
$ns color 3 Green
$ns color 4 Yellow

# Start traffic
$ns at 0.5 "$ftp1 start"
$ns at 1.0 "$ftp2 start"
$ns at 1.5 "$cbr1 start"
$ns at 2.0 "$cbr2 start"

# Stop simulation after 5 seconds


proc finish {} {
global ns tr nam
$ns flush-trace
close $tr
close $nam
puts "Running NAM..."
exec nam mesh.nam &
exit 0
}

$ns at 5.0 "finish"

# Run simulation
$ns run

Distance vector in random topology

# Define the simulator


set ns [new Simulator]

# Open trace and nam files


set tr [open dv_random.tr w]
set nam [open dv_random.nam w]
$ns trace-all $tr
$ns namtrace-all $nam

# Number of nodes
set num_nodes 10

# Create nodes dynamically


for {set i 0} {$i < $num_nodes} {incr i} {
set n($i) [$ns node]
}

# Create random topology


for {set i 0} {$i < $num_nodes} {incr i} {
for {set j [expr $i + 1]} {$j < $num_nodes} {incr j} {
# Randomize whether a link exists between nodes
if {[expr int(rand() * 2)] == 1} {
set bw [expr rand() * 1.5 + 0.5] ; # Random bandwidth between 0.5Mb and 2Mb
set delay [expr rand() * 50 + 5] ; # Random delay between 5ms and 50ms
$ns duplex-link $n($i) $n($j) [format "%.2fMb" $bw] [format "%.2fms" $delay] DropTail
}
}
}

# Set up Distance Vector routing


$ns rtproto DV
# Attach traffic sources
# TCP Connection 1: Random source-destination
set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
set src1 [expr int(rand() * $num_nodes)]
set dst1 [expr ($src1 + int(rand() * ($num_nodes - 1))) % $num_nodes]
$ns attach-agent $n($src1) $tcp1
$ns attach-agent $n($dst1) $sink1
$ns connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1

# TCP Connection 2: Random source-destination


set tcp2 [new Agent/TCP]
set sink2 [new Agent/TCPSink]
set src2 [expr int(rand() * $num_nodes)]
set dst2 [expr ($src2 + int(rand() * ($num_nodes - 1))) % $num_nodes]
$ns attach-agent $n($src2) $tcp2
$ns attach-agent $n($dst2) $sink2
$ns connect $tcp2 $sink2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2

# UDP Connection 1: Random source-destination


set udp1 [new Agent/UDP]
set null1 [new Agent/Null]
set src3 [expr int(rand() * $num_nodes)]
set dst3 [expr ($src3 + int(rand() * ($num_nodes - 1))) % $num_nodes]
$ns attach-agent $n($src3) $udp1
$ns attach-agent $n($dst3) $null1
$ns connect $udp1 $null1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.01
$cbr1 attach-agent $udp1

# UDP Connection 2: Random source-destination


set udp2 [new Agent/UDP]
set null2 [new Agent/Null]
set src4 [expr int(rand() * $num_nodes)]
set dst4 [expr ($src4 + int(rand() * ($num_nodes - 1))) % $num_nodes]
$ns attach-agent $n($src4) $udp2
$ns attach-agent $n($dst4) $null2
$ns connect $udp2 $null2
set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 500
$cbr2 set interval_ 0.01
$cbr2 attach-agent $udp2

# Set flow IDs and colors


$tcp1 set fid_ 1
$tcp2 set fid_ 2
$udp1 set fid_ 3
$udp2 set fid_ 4

$ns color 1 Red


$ns color 2 Blue
$ns color 3 Green
$ns color 4 Yellow

# Start traffic
$ns at 0.5 "$ftp1 start"
$ns at 1.0 "$ftp2 start"
$ns at 1.5 "$cbr1 start"
$ns at 2.0 "$cbr2 start"

# Stop simulation after 10 seconds


proc finish {} {
global ns tr nam
$ns flush-trace
close $tr
close $nam
puts "Simulation finished. Running NAM..."
exec nam dv_random.nam &
exit 0
}

$ns at 10.0 "finish"

# Run the simulation


$ns run

Bus topology using sliding window

# Create the simulator


set ns [new Simulator]
# Open trace and nam files
set tr [open bus_topology.tr w]
set nam [open bus_topology.nam w]
$ns trace-all $tr
$ns namtrace-all $nam

# Number of nodes in the bus topology


set num_nodes 6

# Create nodes for the bus topology


for {set i 0} {$i < $num_nodes} {incr i} {
set n($i) [$ns node]
}

# Set up bus topology (nodes connected in a line)


for {set i 0} {$i < $num_nodes - 1} {incr i} {
$ns duplex-link $n($i) $n([expr $i + 1]) 1Mb 10ms DropTail
$ns duplex-link-op $n($i) $n([expr $i + 1]) orient right
}

# Set up TCP connections with sliding window


set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
$ns attach-agent $n(0) $tcp1
$ns attach-agent $n(5) $sink1
$ns connect $tcp1 $sink1

set ftp1 [new Application/FTP]


$ftp1 attach-agent $tcp1
$tcp1 set windowInit_ 4 ; # Initial window size of 4
$tcp1 set maxcwnd_ 4 ; # Maximum window size

# Set up traffic generation for sliding window protocol


$ns at 0.1 "$ftp1 start"

# Add annotation for trace


$ns at 0.0 "$ns trace-annotate \"Bus Topology with Sliding Window Protocol\""
$ns at 0.1 "$ns trace-annotate \"FTP starts at 0.1\""

# Monitoring TCP flow


$ns add-agent-trace $tcp1 tcp
$ns monitor-agent-trace $tcp1
$tcp1 tracevar cwnd_
# Simulation end time
$ns at 3.0 "$ns detach-agent $n(0) $tcp1 ; $ns detach-agent $n(5) $sink1"
$ns at 3.5 "finish"

# Finish procedure to flush traces and run NAM


proc finish {} {
global ns tr nam
$ns flush-trace
close $tr
close $nam
puts "Simulation finished. Running NAM..."
exec nam bus_topology.nam &
exit 0
}

# Run the simulation


$ns run

Star topology using sliding window

# Create the simulator


set ns [new Simulator]

# Open trace and nam files


set tr [open star_topology.tr w]
set nam [open star_topology.nam w]
$ns trace-all $tr
$ns namtrace-all $nam

# Number of nodes in the star topology (1 hub + 4 nodes)


set num_nodes 5
set hub [$ns node] ; # Central hub node

# Create the other nodes (nodes that connect to the hub)


for {set i 1} {$i < $num_nodes} {incr i} {
set n($i) [$ns node]
# Connect each node to the hub node in a star topology
$ns duplex-link $hub $n($i) 1Mb 10ms DropTail
$ns duplex-link-op $hub $n($i) orient right
}

# Set up TCP connections with sliding window protocol


set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
$ns attach-agent $n(1) $tcp1 ; # Attach TCP agent to node 1
$ns attach-agent $hub $sink1 ; # Attach Sink to the hub node
$ns connect $tcp1 $sink1

set ftp1 [new Application/FTP]


$ftp1 attach-agent $tcp1
$tcp1 set windowInit_ 4 ; # Initial window size of 4
$tcp1 set maxcwnd_ 4 ; # Maximum window size

# Set up traffic generation for sliding window protocol


$ns at 0.1 "$ftp1 start"

# Add annotation for trace


$ns at 0.0 "$ns trace-annotate \"Star Topology with Sliding Window Protocol\""
$ns at 0.1 "$ns trace-annotate \"FTP starts at 0.1\""

# Monitoring TCP flow


$ns add-agent-trace $tcp1 tcp
$ns monitor-agent-trace $tcp1
$tcp1 tracevar cwnd_

# Simulation end time


$ns at 3.0 "$ns detach-agent $n(1) $tcp1 ; $ns detach-agent $hub $sink1"
$ns at 3.5 "finish"

# Finish procedure to flush traces and run NAM


proc finish {} {
global ns tr nam
$ns flush-trace
close $tr
close $nam
puts "Simulation finished. Running NAM..."
exec nam star_topology.nam &
exit 0
}

# Run the simulation


$ns run
HAMMING TX

close all
clear all
Message=input('Enter Message Bits to be transmitted (Binary Data Only in matrix form):');
disp(dec2bin(Message)')
if size(dec2bin(Message)',1)~=1
disp('The Entered Message is not Binary data, Please wait for 5 seconds to re-enter the
Message');
pause(5);
HammingCode_Transmitter;
else
disp('The Message to be Transmitted is');
disp(Message);
Message=flip(Message);
disp('The length of the Message is ')
disp(length(Message));
for K=1:length(Message)
if (power(2,K)-1) >= length(Message)+K
disp('The total number of Parity bits required are ');
disp(K);
break;
end
end
disp('The total length of the message with Parity bits is ');
disp(length(Message)+K);
PB=-K:-1;
disp('The Parity bits to be appended initially are ');
disp(PB);
for s=1:length(Message)+K
for t=0:K
if s==power(2,t)
tx(s)=PB(end-t);
elseif s>power(2,t-1) && s<power(2,t)
tx(s)=Message(s-t);
end
end
end
disp('The Message generated with initially generated parity bits is ');
disp(flip(tx));
txlength=1:length(Message)+K;
dec_num = dec2bin(txlength,K)-'0';
for j=size(dec_num,2):-1:1
Parity_bit_positions=find((dec_num(:,j))');
actual_parity_bit_positions=Parity_bit_positions(2:end);
PB(j)=rem(sum(tx(actual_parity_bit_positions)),2);
end
disp('The actual Parity bits to be appended are ');
disp(PB);
for s=1:length(Message)+K
for t=0:K
if s==power(2,t)
tx(s)=PB(end-t);
end
end
end
end
disp('The actual Message to be transmitted with parity bits is');
disp(flip(tx))

BIT STUFFING

clc
close all;
msg=input('Input message in binary bit stream:');
count=0;
for j=1:length(msg)-5
for i=j:j+4
if msg(i)==1
count=count+1;
else
count=0;
break;
end
end
if count==5
msg=[msg(1:j+4) 0 msg(j+5:end)];
count=0;
elseif msg(end-4:end)==ones(1,5)
msg=[msg 0];
end
end
fprintf('stream after the bit stuffing is:')
disp(msg);

BIT DE-STUFFING
clc
close all
clear all
msg=input('Enter Received Message in Binary Bit Stream: ');
disp('The length of the message is')
disp(length(msg));
count=0;
msg2=msg;
for j=1:length(msg)-6
for i=j:j+4
if msg(i)==1
count=count+1;
else
count=0;
break;
end
end
if count==5 && msg(j+5)==0
msg2=[msg(1:j+4) msg(j+6:end)];
disp('The length of the message after Bit Destuffing is')
msg=msg2;
disp(length(msg2));
disp('Stream after the bit is Destuffed is:');
disp(msg2);
elseif count==5 && msg(j+5)==1
disp('ERROR')
count=0;
else
disp('ERROR')
count=0;
end
end

CRC

clc;
close all;
clear all;
m=input("enter msg: ");
g=input("enter pattern: ");
lm=length(m);
lg=length(g);
t=[m,zeros(1,lg-1)];
lt=length(t);
xm=t(1:lg);
rem1=[];
for i=lg:lt
if(xm(1)==g(1))
rem1=bitxor(xm,g);
end
if i==lt
rem1=rem1(2:end);
break;
end
rem1=[rem1(2:end),t(i+1)];
xm=rem1;
end
disp(rem1);
if(rem1==0)
disp("rececived sequence is correct")
else
disp("received sequence is wrong")
end

DCRC

clc
close all
clear all
tm=input('enter transmitted frame ');
g=input('enter pattern ');
lg=length(g);
ltm=length(tm);
txm=tm(1:lg);
rem1=[];
for i=lg:1:ltm
if(txm(1)==g(1))
rem1=bitxor(txm,g);
end
if i==ltm
rem1=rem1(2:end);
break;
end
rem1=[rem1(2:end),tm(i+1)];
txm=rem1;
end
if rem1==zeros(1,lg-1)
rem1
disp('"NO ERROR" ready to receive another frame');
else
rem1
disp('"ERROR" please retransmit');
dy=input('if u want to retansmit press "y" else "n" ','s');
if(dy=='y')
DCRCmat
end
end

You might also like