0% found this document useful (0 votes)
23 views

Network Lab -2022 (1)

Uploaded by

adithkannan04
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)
23 views

Network Lab -2022 (1)

Uploaded by

adithkannan04
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/ 44

COMPUTER NETWORK LABORATORY 18CSL57

PART A-Simulation Exercises

1. Simulate a three nodes point-to-point network with dulpex links between them.Set
the queue size and vary the bandwidth and find the number of packets dropped.

PROCEDURE

 Open the terminal.


 Execute java –jar nsg2.jar.
 In scenario, select new wired scenario.
 Select nodes, create three nodes.
 In links,select duplex-link and connect.
 Apply tcp and tcpsink between two nodes and establish a peer-peer connection
between them.
 Set bandwidth and queue - size
 In application,select ftp for tcp agent.
 In parameters set the simulation time.
 Gototcl and code will be generated.

# This script is created by NSG2 beta1


# <http://wushoupong.googlepages.com/nsg>

#===================================
# Simulation parameters setup
#===================================
set val(stop) 20.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open p1.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open p1.nam w]
$ns namtrace-all $namfile

#===================================
# Nodes Definition
#===================================
#Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

1
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n1 1.0Mb 10ms DropTail
$ns queue-limit $n0 $n1 5
$ns duplex-link $n0 $n2 1.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 5

#Give node position (for NAM)


$ns duplex-link-op $n0 $n1 orient left-down
$ns duplex-link-op $n0 $n2 orient right-down

#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 20.0 "$ftp0 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam p1.nam &
exec echo "number of packets dropped is "&
exec grep -c "^d"pgm1.tr &

exit 0
}

2
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

$ns at $val(stop) "$ns nam-end-wireless $val(stop)"


$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

3
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

2. Implement transmission of ping messages/trace route over a network topology


consisting of 6 nodes and find the number of packets dropped due to congestion.

Procedure

 Open the terminal.


 Execute java –jar nsg2.jar.
 In scenario, select new wired scenario.
 Select nodes, create 6 nodes.
 In links,select duplex-link and connect. Set bandwidth to 1MB and queue - size to 2
 Apply tcp and tcpsink between two nodes and establish a peer-peer connection
between them.
 In application ,select ftp for tcp agent.
 In parameters set the simulation time.
 Gototcl and code will be generated.

# Simulation parameters setup

# This script is created by NSG2 beta1


# <http://wushoupong.googlepages.com/nsg>

#===================================
# Simulation parameters setup
#===================================
set val(stop) 20.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open p2.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open p2.nam w]
$ns namtrace-all $namfile
$ns color 1 red
$ns color 2 blue
$ns color 3 green
$ns color 4 yellow

#===================================
# Nodes Definition
#===================================
#Create 6 nodes
set n0 [$ns node]

4
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

set n1 [$ns node]


set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n1 1.0Mb 10ms DropTail
$ns queue-limit $n0 $n1 5
$ns duplex-link $n2 $n1 1.0Mb 10ms DropTail
$ns queue-limit $n2 $n1 5
$ns duplex-link $n2 $n3 1.0Mb 10ms DropTail
$ns queue-limit $n2 $n3 5
$ns duplex-link $n3 $n4 1.0Mb 10ms DropTail
$ns queue-limit $n3 $n4 5
$ns duplex-link $n4 $n5 1.0Mb 10ms DropTail
$ns queue-limit $n4 $n5 5

#Give node position (for NAM)


$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n2 $n1 orient left-down
$ns duplex-link-op $n2 $n3 orient right-down
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n4 $n5 orient right-down

Agent/Ping instproc recv {from rtt} {


$self instvar node_
puts "node [$node_ id] received a ping answer from $from with with rtt=$rtt ms"
}

#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n3 $sink2
$ns connect $tcp0 $sink2
$tcp0 set packetSize_ 1500

#Setup a UDP connection


set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n2 $udp1
set null3 [new Agent/Null]

5
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

$ns attach-agent $n4 $null3


$ns connect $udp1 $null3
$udp1 set packetSize_ 1500

#setup a ping connection

set p0 [new Agent/Ping]


$p0 set class_ 3
$ns attach-agent $n0 $p0

set p1 [new Agent/Ping]


$p1 set class_ 4
$ns attach-agent $n5 $p1
$ns connect $p0 $p1

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp0
$ns at 1.0 "$ftp1 start"
$ns at 20.0 "$ftp1 stop"

#Setup a CBR Application over UDP connection


set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp1
$cbr2 set packetSize_ 1000
$cbr2 set rate_ 1.0Mb
$cbr2 set random_ null
$ns at 1.0 "$cbr2 start"
$ns at 20.0 "$cbr2 stop"

$ns at 1.0 "$p0 send"


$ns at 2.0 "$p1 send"
$ns at 3.0 "$p0 send"
$ns at 3.2 "$p1 send"
$ns at 3.4 "$p0 send"
$ns at 3.6 "$p1 send"
$ns at 4.0 "$p0 send"
$ns at 4.2 "$p1 send"
$ns at 4.4 "$p0 send"
$ns at 4.6 "$p1 send"

#===================================
# Termination
#===================================
#Define a 'finish' procedure

6
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam p2.nam &
puts " no of packets dropped"
exec grep "^d" p2.tr | grep -c "ping" &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

7
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.

PROCEDURE

 Open the terminal.


 Execute java –jar nsg2.jar.
 In scenario, select new wired scenario.
 Select nodes, create 6-10 nodes
 In links,select duplex-link and connect.
 Apply tcp and tcpsink between two nodesudp and null agent between two nodes and
establish a peer-peer connection between them.
 In application ,select ftp for tcp agent and cbr for udp agent.
 Set bandwidth and queue - size
 In parameters set the simulation time.
 Gototcl and code will be generated.

# Simulation parameters setup

# This script is created by NSG2 beta1


# <http://wushoupong.googlepages.com/nsg>

#===================================
# Simulation parameters setup
#===================================
set val(stop) 20.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open p3.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open p3.nam w]
$ns namtrace-all $namfile
set t [open tahoe w]
set r [open reno w]
set v [open vegas w]
#===================================
# Nodes Definition
#===================================
#Create 11 nodes
set n0 [$ns node]

8
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

set n1 [$ns node]


set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
set n7 [$ns node]
set n8 [$ns node]
set n9 [$ns node]
set n10 [$ns node]

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 1.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 5
$ns duplex-link $n1 $n2 1.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 5
$ns duplex-link $n2 $n3 1.0Mb 10ms DropTail
$ns queue-limit $n2 $n3 5
set lan [$ns newLan "$n3 $n4 $n5 $n6 $n7 $n8 $n9 $n10" 0.5mb 100ms ll
Queue/DropTail MAC/802_3 channel]
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right-up

#===================================
# Agents Definition
#===================================
#Setup a TCP/Reno connection
set tcp0 [new Agent/TCP/Reno]
$ns attach-agent $n0 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n10 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500

#Setup a TCP/FullTcp/Tahoe connection


set tcp2 [new Agent/TCP/FullTcp/Tahoe]
$ns attach-agent $n1 $tcp2
set sink3 [new Agent/TCPSink]
$ns attach-agent $n7 $sink3
$ns connect $tcp2 $sink3
$tcp2 set packetSize_ 1500

#Setup a TCP/Vegas connection


set tcp4 [new Agent/TCP/Vegas]
$ns attach-agent $n4 $tcp4

9
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

set sink5 [new Agent/TCPSink]


$ns attach-agent $n9 $sink5
$ns connect $tcp4 $sink5
$tcp4 set packetSize_ 1500

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP/Reno connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 1.0 "plot $tcp0 $r"
$ns at 20.0 "$ftp0 stop"

#Setup a FTP Application over TCP/FullTcp/Tahoe connection


set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp2
$ns at 1.0 "$ftp1 start"
$ns at 1.0 "plot $tcp2 $t"
$ns at 20.0 "$ftp1 stop"

#Setup a FTP Application over TCP/Vegas connection


set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp4
$ns at 1.0 "$ftp2 start"
$ns at 1.0 "plot $tcp4 $v"
$ns at 20.0 "$ftp2 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam p3.nam &
exec xgraph reno vegas tahoe &
exit 0
}
proc plot { tcps file} {
global ns
set now [$ns now]
set cwnd [$tcps set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+0.1] "plot $tcps $file"

10
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

11
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

4. Implement simple ESS and with transmitting nodes in wire-less LAN by simulation
and determine the performance with respect to transmission of packets.

PROCEDURE
 Open the terminal.
 Execute java –jar nsg2.jar.
 In scenario, select new wireless scenario.
 Select nodes, create nodes.
 In links,select duplex-link and connect.
 Apply udp and null between two nodes and establish a peer-peer connection between
them.
 In application ,selectcbr for udp agent.
 Set bandwidth and queue - size
 In parameters set the simulation time.
 Gototcl and code will be generated.

# Simulation parameters setup

# This script is created by NSG2 beta1


# <http://wushoupong.googlepages.com/nsg>

#===================================
# Simulation parameters setup
#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-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 in ifq
set val(nn) 7 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 1091 ;# X dimension of topography
set val(y) 781 ;# Y dimension of topography
set val(stop) 200.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object

12
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

set topo [new Topography]


$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

#Open the NS trace file


set tracefile [open p4.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open p4.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
# Mobile node parameter setup
#===================================
$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) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON

#===================================
# Nodes Definition
#===================================
#Create 7 nodes
set n0 [$ns node]
$n0 set X_ 604
$n0 set Y_ 681
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 399
$n1 set Y_ 599
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 798
$n2 set Y_ 602
$n2 set Z_ 0.0

13
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

$ns initial_node_pos $n2 20


set n3 [$ns node]
$n3 set X_ 180
$n3 set Y_ 550
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 465
$n4 set Y_ 419
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 796
$n5 set Y_ 433
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
set n6 [$ns node]
$n6 set X_ 991
$n6 set Y_ 600
$n6 set Z_ 0.0
$ns initial_node_pos $n6 20

#===================================
# Agents Definition
#===================================
#Setup a UDP connection
set udp0 [new Agent/UDP]
$ns attach-agent $n3 $udp0
set null1 [new Agent/Null]
$ns attach-agent $n6 $null1
$ns connect $udp0 $null1
$udp0 set packetSize_ 1500

#===================================
# Applications Definition
#===================================
#Setup a CBR Application over UDP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 1.0Mb
$cbr0 set random_ null
$ns at 1.0 "$cbr0 start"
$ns at 200.0 "$cbr0 stop"

#===================================
# Termination
#===================================

14
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

#Define a 'finish' procedure


proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam p4.nam &

set s [exec grep "^s" p4.tr | grep -c "_3_ AGT"]


set r [exec grep "^r" p4.tr | grep -c "_6_ AGT"]

exec echo "number of packets sent= $s" &


exec echo "number of packets received = $r" &
exit 0
}
$ns at 50.0 "$n6 setdest 991 60 20"
$ns at 75.0 "$n6 setdest 399 500 20"

for {set i 0} {$i < $val(nn) } { incr i } {


$ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

15
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

16
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

5. Implement and study the performance of GSM on NS2/NS3 (Using MAC layer) or
equivalent environment.
PROCEDURE

 Open the terminal.


 Execute java –jar nsg2.jar.
 In scenario, select new wired scenario.
 Select nodes, create 6-10 nodes.
 In links, select duplex-link and connect.
 Apply 2tcp and 2tcpsink between two nodes and establish a peer-peer connection
between them.
 In application,select ftp for tcp agent.
 Set bandwidth and queue - size
 In parameters set the simulation time.
 Go to tcl and code will be generated.

# This script is created by NSG2 beta1


# <http://wushoupong.googlepages.com/nsg>
#===================================
# Simulation parameters setup
#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-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 in ifq
set val(nn) 10 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 1035 ;# X dimension of topography
set val(y) 647 ;# Y dimension of topography
set val(stop) 50.0 ;# time of simulation end
set f0 [open throughput.tr w]
set f1 [open lost.tr w]
set f2 [open delay.tr w]
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#Setup topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)
#Open the NS trace file
set tracefile [open p5.tr w]
$ns trace-all $tracefile

17
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

#Open the NAM trace file


set namfile [open p5.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel
#===================================
# Mobile node parameter setup
#===================================
$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) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON
#===================================
# Nodes Definition
#===================================
#Create 10 nodes
set n0 [$ns node]
$n0 set X_ 135
$n0 set Y_ 547
$n0 set Z_ 0.0
set X1(0) 135
set Y1(0) 547
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 335
$n1 set Y_ 547
$n1 set Z_ 0.0
set X1(1) 335
set Y1(1) 547
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 535
$n2 set Y_ 547
$n2 set Z_ 0.0
set X1(2) 535
set Y1(2) 547
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 735
$n3 set Y_ 547

18
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

$n3 set Z_ 0.0


set X1(3) 735
set Y1(3) 547
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 935
$n4 set Y_ 547
$n4 set Z_ 0.0
set X1(4) 935
set Y1(4) 547
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 135
$n5 set Y_ 347
$n5 set Z_ 0.0
set X1(5) 135
set Y1(5) 347
$ns initial_node_pos $n5 20
set n6 [$ns node]
$n6 set X_ 335
$n6 set Y_ 347
$n6 set Z_ 0.0
set X1(6) 335
set Y1(6) 347
$ns initial_node_pos $n6 20
set n7 [$ns node]
$n7 set X_ 535
$n7 set Y_ 347
$n7 set Z_ 0.0
set X1(7) 535
set Y1(7) 347
$ns initial_node_pos $n7 20
set n8 [$ns node]
$n8 set X_ 735
$n8 set Y_ 347
$n8 set Z_ 0.0
set X1(8) 735
set Y1(8) 347
$ns initial_node_pos $n8 20
set n9 [$ns node]
$n9 set X_ 935
$n9 set Y_ 347
$n9 set Z_ 0.0
set X1(9) 935
set Y1(9) 347
$ns initial_node_pos $n9 20
puts "........................................"
set m 0
puts "........................................"
puts "| Node | One hop neighbour |"

19
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

puts "........................................"
for {set i 0} {$i < $val(nn) } {incr i} {
set k 0
for { set j 0} {$j < $val(nn) } {incr j} {
set a [expr $X1($j)-$X1($i)]
set b [expr $a*$a]
set c [expr $Y1($j)-$Y1($i)]
set d [expr $c*$c]
set e [expr $b+$d]
set f 0.5
set g [expr pow($e,$f)]
if {$g <= 200 && $i != $j} {
puts "| node($i) | node($j) |"
set nei($m) $j
set k [expr $k+1]
set m [expr $m+1]
}
}
puts "........................................"
}
puts "Loading connection pattern...."
puts "Loading scenario file...."
#===================================
# Agents Definition
#===================================
#Setup a UDP connection
set udp2 [new Agent/UDP]
$ns attach-agent $n0 $udp2
set sink [new Agent/LossMonitor]
$ns attach-agent $n9 $sink
$ns connect $udp2 $sink
$udp2 set packetSize_ 1500
#===================================
# Applications Definition
#===================================
#Setup a CBR Application over UDP connection
set cbr3 [new Application/Traffic/CBR]
$cbr3 attach-agent $udp2
$cbr3 set packetSize_ 1000
$cbr3 set rate_ 1.0Mb
$cbr3 set random_ null
$ns at 1.0 "$cbr3 start"
$ns at 50.0 "$cbr3 stop"
set holdtime 0
set holdseq 0
set holdrate1 0
proc record {} {
global sink f0 f1 f2 holdtime holdseq holdrate1
set nsi [Simulator instance]
set time 0.9 ;#set sampling time 0.9 sec

20
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

set bw0 [$sink set bytes_]


set bw1 [$sink set nlost_]
set bw2 [$sink set lastPktTime_]
set bw3 [$sink set npkts_]
set now [$nsi now]
# record bit rate in trace files
puts $f0 "$now [expr (($bw0+$holdrate1)*8)/(2*$time*1000000)]"
#record packet loss rate in file
puts $f1 "$now [expr $bw1/$time]"
if { $bw3 > $holdseq } {
puts $f2 "$now [expr ($bw2 - $holdtime)/($bw3 - $holdseq)]"
} else {
puts $f2 "$now [expr ($bw3 - $holdseq)]"
}
$sink set bytes_ 0
$sink set nlost_ 0
set holdtime $bw2
set holdseq $bw3
set holdrate1 $bw0
$nsi at [expr $now+$time] "record" ;#schedule record after $time interval sec
}
#start recording at time 0
$ns at 0.0 "record"
$ns at 10.1 "$n0 setdest 200 560 20"
$ns at 10.1 "$n0 setdest 400 570 20"
$ns at 10.1 "$n0 setdest 600 580 20"
$ns at 10.1 "$n0 setdest 750 590 20"
$ns at 10.1 "$n0 setdest 950 600 20"
$ns at 10.1 "$n0 setdest 300 610 20"
$ns at 10.1 "$n0 setdest 500 620 20"
$ns at 10.1 "$n0 setdest 700 630 20"
$ns at 10.1 "$n0 setdest 800 640 20"
$ns at 10.1 "$n0 setdest 1000 645 20"
$ns at 0.5 "$n0 add-mark m blue square"
$ns at 0.5 "$n9 add-mark m red square"
$ns at 0.5 "$n0 label source"
$ns at 0.5 "$n9 label Destination"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile f0 f1 f2
$ns flush-trace
close $tracefile
close $namfile
close $f0
close $f1
close $f2
exec nam p5.nam &

21
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

exec xgraph throughput.tr &


exec xgraph lost.tr &
exec xgraph delay.tr &
exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

22
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

23
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

24
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

6. Implement and study the performance of CDMA on NS2/NS3 (Using stack called
Call net) or equivalent environment.
PROCEDURE

 Open the terminal.


 Execute java –jar nsg2.jar.
 In scenario, select new wireless scenario.
 Select nodes, create nodes.
 In links, select duplex-link and connect.
 Apply udp and null between two nodes and establish a peer-peer connection between
them.
 In application, select cbr for udp agent.
 Set bandwidth and queue - size
 In parameters set the simulation time.
 Go to tcl and code will be generated.

# This script is created by NSG2 beta1


# <http://wushoupong.googlepages.com/nsg>

#===================================
# Simulation parameters setup
#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-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 in ifq
set val(nn) 20 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 1500 ;# X dimension of topography
set val(y) 800 ;# Y dimension of topography
set val(stop) 200.0 ;# time of simulation end

Mac/802_11 set cdma_code_bw_start_ 0


Mac/802_11 set cdma_code_bw_stop_ 63
Mac/802_11 set cdma_code_init_start_ 64
Mac/802_11 set cdma_code_init_stop_ 127
Mac/802_11 set cdma_code_cqich_start_ 128
Mac/802_11 set cdma_code_cqich_stop_ 195
Mac/802_11 set cdma_code_handover_start_ 196
Mac/802_11 set cdma_code_handover_stop_ 255

set f0 [open througput.tr w]


set f1 [open lost.tr w]
set f2 [open delay.tr w]

25
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object


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

#Open the NS trace file


set tracefile [open p6.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open p6.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
# Mobile node parameter setup
#===================================
$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) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

#===================================
# Nodes Definition
#===================================
#Create 20 nodes
set n0 [$ns node]
$n0 set X_ 246
$n0 set Y_ 609
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]

26
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

$n1 set X_ 446


$n1 set Y_ 609
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 646
$n2 set Y_ 609
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 846
$n3 set Y_ 609
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 1046
$n4 set Y_ 609
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 246
$n5 set Y_ 409
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
set n6 [$ns node]
$n6 set X_ 446
$n6 set Y_ 409
$n6 set Z_ 0.0
$ns initial_node_pos $n6 20
set n7 [$ns node]
$n7 set X_ 646
$n7 set Y_ 409
$n7 set Z_ 0.0
$ns initial_node_pos $n7 20
set n8 [$ns node]
$n8 set X_ 846
$n8 set Y_ 409
$n8 set Z_ 0.0
$ns initial_node_pos $n8 20
set n9 [$ns node]
$n9 set X_ 1046
$n9 set Y_ 409
$n9 set Z_ 0.0
$ns initial_node_pos $n9 20
set n10 [$ns node]
$n10 set X_ 246
$n10 set Y_ 209
$n10 set Z_ 0.0
$ns initial_node_pos $n10 20
set n11 [$ns node]

27
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

$n11 set X_ 446


$n11 set Y_ 209
$n11 set Z_ 0.0
$ns initial_node_pos $n11 20
set n12 [$ns node]
$n12 set X_ 646
$n12 set Y_ 209
$n12 set Z_ 0.0
$ns initial_node_pos $n12 20
set n13 [$ns node]
$n13 set X_ 846
$n13 set Y_ 209
$n13 set Z_ 0.0
$ns initial_node_pos $n13 20
set n14 [$ns node]
$n14 set X_ 1046
$n14 set Y_ 209
$n14 set Z_ 0.0
$ns initial_node_pos $n14 20
set n15 [$ns node]
$n15 set X_ 246
$n15 set Y_ 9
$n15 set Z_ 0.0
$ns initial_node_pos $n15 20
set n16 [$ns node]
$n16 set X_ 446
$n16 set Y_ 9
$n16 set Z_ 0.0
$ns initial_node_pos $n16 20
set n17 [$ns node]
$n17 set X_ 646
$n17 set Y_ 9
$n17 set Z_ 0.0
$ns initial_node_pos $n17 20
set n18 [$ns node]
$n18 set X_ 846
$n18 set Y_ 9
$n18 set Z_ 0.0
$ns initial_node_pos $n18 20
set n19 [$ns node]
$n19 set X_ 1046
$n19 set Y_ 9
$n19 set Z_ 0.0
$ns initial_node_pos $n19 20

#===================================
# Agents Definition
#===================================
#Setup a UDP connection
set udp0 [new Agent/UDP]

28
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

$ns attach-agent $n0 $udp0


set sink [new Agent/LossMonitor]
$ns attach-agent $n19 $sink
$ns connect $udp0 $sink
$udp0 set packetSize_ 1500

#===================================
# Applications Definition
#===================================
#Setup a CBR Application over UDP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 1.0Mb
$cbr0 set random_ null
$ns at 1.0 "$cbr0 start"
$ns at 200.0 "$cbr0 stop"

set holdtime 0
set holdseq 0
set holdrate1 0

proc record {} {
global sink f0 f1 f2 holdtime holdseq holdrate1

set nsi [Simulator instance]


set time 0.9 ;#Set Sampling Time to 0.9 Sec

set bw0 [$sink set bytes_]


set bw1 [$sink set nlost_]
set bw2 [$sink set lastPktTime_]
set bw3 [$sink set npkts_]
set now [$nsi now]

# Record Bit Rate in Trace Files


puts $f0 "$now [expr (($bw0+$holdrate1)*8)/(2*$time*1000000)]"

# Record Packet Loss Rate in File


puts $f1 "$now [expr $bw1/$time]"

if { $bw3 > $holdseq } {


puts $f2 "$now [expr ($bw2 - $holdtime)/($bw3 - $holdseq)]"
} else {
puts $f2 "$now [expr ($bw3 - $holdseq)]"
}

$sink set bytes_ 0


$sink set nlost_ 0

29
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

set holdtime $bw2


set holdseq $bw3
set holdrate1 $bw0
$nsi at [expr $now+$time] "record" ;# Schedule Record after $time interval sec
}

# Start Recording at Time 0


$ns at 0.0 "record"

$ns at 10.1 "$n0 setdest 350 700 20"


$ns at 10.1 "$n1 setdest 550 700 20"
$ns at 10.1 "$n2 setdest 750 700 20"
$ns at 10.1 "$n3 setdest 950 700 20"
$ns at 10.1 "$n4 setdest 1150 700 20"
$ns at 10.1 "$n5 setdest 350 500 20"
$ns at 10.1 "$n6 setdest 550 500 20"
$ns at 10.1 "$n7 setdest 750 500 20"
$ns at 10.1 "$n8 setdest 950 500 20"
$ns at 10.1 "$n9 setdest 1150 500 20"
$ns at 10.1 "$n10 setdest 350 300 20"
$ns at 10.1 "$n11 setdest 550 300 20"
$ns at 10.1 "$n12 setdest 750 300 20"
$ns at 10.1 "$n13 setdest 950 300 20"
$ns at 10.1 "$n14 setdest 1150 300 20"
$ns at 10.1 "$n15 setdest 350 100 20"
$ns at 10.1 "$n16 setdest 550 100 20"
$ns at 10.1 "$n17 setdest 750 100 20"
$ns at 10.1 "$n18 setdest 950 100 20"
$ns at 10.1 "$n19 setdest 1150 100 20"

$ns at 0.5 "$n0 add-mark m blue square"


$ns at 0.5 "$n19 add-mark m red square"

$ns at 0.5 "$n0 label source"


$ns at 0.5 "$n19 label Destination"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile f0 f1 f2
$ns flush-trace
close $tracefile
close $namfile
exec nam p6.nam &
exec xgraph throughput.tr &

30
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

exec xgraph lost.tr &


exec xgraph delay.tr &
exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

31
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

PART-B

7. Write a program for error detecting code using CRC-CCITT(16-bits).

package crc;

import java.util.*;

public class Crc {


public static void main(String[] args) {
Scanner scan =new Scanner(System.in);
int n;
System.out.println("enter the size of data:");
n=scan.nextInt();
int data[] = new int[n];
system.out.println("enter the data ,bit by bit");
for(int i=0;i<n;i++)
{
data[i]=scan.nextInt();
}
System.out.println("enter the size of divisor:");
n=scan.nextInt();
int divisor[] = new int[n];
System.out.println("enter the divisor , bit by bit");
for(int i=0;i<n;i++)
{
divisor[i] = scan.nextInt();
}
int remainder[] = divide(data,divisor);
System.out.println("remainder=");
for(int i=0;i<remainder.length-1;i++)
{
System.out.println("remainder[i]");

}
System.out.println("\n the CRC code generated is :");
for(int i=0;i<data.length;i++)
{
System.out.println(data[i]);
}
for(int i=0;i<remainder.length-1;i++)
{
System.out.println(remainder[i]);
}
System.out.println();
int sent_data[]=new int[data.length+remainder.length-1];
System.out.println("enter the DATA recevied at the receiver");
for(int i=0;i<sent_data.length;i++){
sent_data[i]=scan.nextInt();

32
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

}
recieve(sent_data,divisor);
}
static int[] divide(int old_data[],int divisor[]){
int remainder[],i;
int data[]=new int[old_data.length+divisor.length];
System.arraycopy(old_data,0,data,0,old_data.length);
remainder=new int[divisor.length];
System.arraycopy(data,0,remainder,0,divisor.length);
for(i=0;i<old_data.length;i++){
if(remainder[0]==1){
for(int j=1; j<divisor.length;j++)
{
remainder[j-1]=exor(remainder[j],divisor[j]);
}
}
else
{
for(int j=1;j<divisor.length;j++)
{
remainder[j-1]=exor(remainder[j],0);
}
}
remainder [divisor.length-1]= data[i+divisor.length];
}
return remainder;
}
static int exor(int a,int b)
{
if(a==b)
{
return 0;
}
return 1;
}
static void recieve(int data[],int divisor[]){
int remainder[]=divide(data,divisor);
for(int i=0;i<remainder.length;i++)
{
if(remainder[i]!=0)
{
System.out.println("there is an error in recieved data......");
return;
}
}
System.out.println("data recieved without any error");
}
}

33
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

34
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

8.Write a program to find the shortest path between vertices using bellman-ford algorithm.

import java.util.Scanner;
public class BellmanFord
{
private int dist[];
private int n;
public static final int MAX_VALUE=999;
public BellmanFord(int n)
{
this.n=n;
dist=new int[n+1];
}
public void BellmanFordEvaluation(int src, int adj[][])
{
for(int nd=1; nd<=n; nd++)
{
dist[nd]=MAX_VALUE;
}
dist[src]=0;
for(int nd=1; nd<=n-1; nd++)
{
for(int sn=1; sn<=n; sn++)
{
for(int dn=1; dn<=n; dn++)
{
if(adj[sn][dn]!=MAX_VALUE)
{
if(dist[dn]>dist[sn]+adj[sn][dn])
dist[dn]=dist[sn]+adj[sn][dn];
}
}
}
}
for(int sn=1; sn<=n; sn++)
{
for(int dn=1; dn<=n; dn++)
{
if(adj[sn][dn]!=MAX_VALUE)
{
if(dist[dn]>dist[sn]+adj[sn][dn])
System.out.println("The graph contains negative edge cycle.");
}
}
}
for(int vertex=1; vertex<=n; vertex++)
{
System.out.println("Distance of src"+src+"to"+vertex+"is"+dist[vertex]);
}
}

35
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

public static void main(String[] args)


{
int n=0;
int src;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of vertices:");
n=sc.nextInt();
int adj[][]=new int[n+1][n+1];
System.out.println("Enter the adjacency matrix:");
for(int sn=1; sn<=n; sn++)
{
for(int dn=1; dn<=n; dn++)
{
adj[sn][dn]=sc.nextInt();
if(sn==dn)
{
adj[sn][dn]=0;
continue;
}
if(adj[sn][dn]==0)
{
adj[sn][dn]=MAX_VALUE;
}}}
System.out.println("Enter the src vertex:");
src=sc.nextInt();
BellmanFord bellmanford=new BellmanFord(n);
bellmanford.BellmanFordEvaluation(src,adj);
}}
OUTPUT

36
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

9.Using TCP/IP sockets, write a client-server program to make the client send the file
name and to make the server send back the contents of the requested file if present.

/*client*/

import java.net.*;
import java.io.*;
import java.util.*;
public class TCPClient
{
public static void main( String args[]) throws Exception
{
Socket sock = new Socket("127.0.0.1",4000);
System.out.println("Enter the filename");
BufferedReader BufRead1 =new BufferedReader(new InputStreamReader(System.in));
String fname =BufRead1.readLine();
OutputStream ostream =sock.getOutputStream();
PrintWriter pw=new PrintWriter(ostream,true);
pw.println(fname);
InputStream istream =sock.getInputStream();
BufferedReader BufRead2 =new BufferedReader(new InputStreamReader(istream));
String str;
while((str=BufRead2.readLine())!=null)
{
System.out.println(str);
}
}
}

/*server*/

import java.net.*;
import java.io.*;
public class TCPServer
{
public static void main(String args []) throws Exception
{
ServerSocket sersock=new ServerSocket(4000);
System.out.println("server ready for transmission");
Socket sock=sersock.accept();
System.out.println("connection is successful and waiting for chatting");
InputStream istream=sock.getInputStream();
BufferedReader bufRead1=new BufferedReader(new InputStreamReader(istream));
String fname=bufRead1.readLine();
System.out.println("the requested file from client is:"+fname);
BufferedReader bufRead2=new BufferedReader(new FileReader(fname));
OutputStream ostream =sock.getOutputStream();

37
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

PrintWriter pw = new PrintWriter(ostream,true);


String str;
while((str=bufRead2.readLine())!=null)
{
pw.println(str);
}
sock.close();
}
}
OUTPUT
Server Side

Client Side

38
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

10. Write a program on datagram socket for client/server to display the messages on
client side, typed at the server side.

/*client side*/

import java.io.*;
import java.net.*;
public class UDPClient{
public static void main(String args[]) throws Exception{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the message");
String message=in.readLine();
DatagramSocket clientSocket=new DatagramSocket();
InetAddress IPAddress=InetAddress.getByName("localhost");
byte[] sendData=new byte[1024];
byte[] receiveData=new byte[1024];
sendData=message.getBytes();

DatagramPacket sendPacket=new
DatagramPacket(sendData,sendData.length,IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);
String ServerMessage=new String(receivePacket.getData());
System.out.println("from server:"+ServerMessage);
clientSocket.close();
}
}

/*server side*/
import java.io.*;
import java.util.*;
import java.net.*;
public class UDPServer{
public static void main(String args[]) throws Exception{

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


DatagramSocket ss = new DatagramSocket(9876);
byte[] sendData=new byte[1024];
byte[] receiveData=new byte[1024];

while(true){
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
ss.receive(receivePacket);
String clientmessage=new String(receivePacket.getData());
System.out.println("from client:"+clientmessage);
InetAddress IPAdd=receivePacket.getAddress();

39
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

int port=receivePacket.getPort();
String message= in.readLine();
sendData=message.getBytes();
DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAdd,port);
ss.send(sendPacket);
}
}
}

Client Side

Server Side

40
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

11. Write a program for simple RSA algorithm to encrypt and decrypt the data.

package rsa;
import java.io.*;
import java.math.BigInteger;
import java.util.Random;
public class Rsa {
private BigInteger p,q,n,z,e,d;
private int bitlength=1024;
private Random r;
public Rsa()
{
r=new Random();
p=BigInteger.probablePrime(bitlength, r);
q=BigInteger.probablePrime(bitlength, r);
n=p.multiply(q);
z=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e=BigInteger.probablePrime(bitlength/2, r);
while (z.gcd(e).compareTo(BigInteger.ONE)>1 && e.compareTo(z)>0)
{
e.add(BigInteger.ONE);
}
d=e.modInverse(z);
}

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


Rsa rsa = new Rsa();
DataInputStream in = new DataInputStream(System.in);
String teststring;
System.out.println("enter the plain text:");
teststring = in.readLine();
System.out.println("Encrypting String:" +teststring);
byte[] bytes = teststring.getBytes();

byte[] encrypted = rsa.encrypt(bytes);


System.out.println("String in encrypted: "+encrypted);

byte[] decrypted = rsa.decrypt(encrypted);


System.out.println("Decrypted String: " +new String(decrypted));
}
public byte[] encrypt(byte[] message)
{
return(new BigInteger(message)).modPow(e, n).toByteArray();
}
public byte[] decrypt(byte[] message)
{
return(new BigInteger(message)).modPow(d, n).toByteArray();
}

41
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

}
OUTPUT

42
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

12. Write a program for congestion control using leaky bucket algorithm.

package leakybucket;
import java.util.Random;
import java.util.Scanner;

public class LeakyBucket {

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


int incoming, outgoing, buck_size, n, store = 0;
Scanner Sc = new Scanner(System.in);

System.out.println("Enter bucket size, outgoing rate,no of inputs and incoming size: ");
buck_size = Sc.nextInt();
outgoing = Sc.nextInt();
n = Sc.nextInt();

while (n != 0) {
System.out.println("Enter the incoming packet size : " );
incoming = Sc.nextInt();
if (incoming <= (buck_size - store)){
store += incoming;
System.out.println("Bucket buffer size is " + store + "out of " + buck_size);
}
else {
System.out.println("Packet Loss " + (incoming - (buck_size - store)));
store = buck_size;
System.out.println("Bucket buffer size is " + store + "out of " + buck_size);

}
store = store - outgoing;
System.out.println("After outgoing : " + store + "packets left out of " + buck_size + "in
buffer");
n--;

Thread.sleep(3000);
}
}
}
OUTPUT

43
Dept.of.CS&E, KVGCE, Sullia
COMPUTER NETWORK LABORATORY 18CSL57

44
Dept.of.CS&E, KVGCE, Sullia

You might also like