0% found this document useful (0 votes)
113 views36 pages

CN Lab Manual

The document describes implementing different computer network simulations using NS2/NS3. It includes instructions for setting up point-to-point networks, running ping and trace route over a 6 node topology, creating an Ethernet LAN with multiple traffic nodes, and measuring congestion windows for different source/destination pairs.

Uploaded by

SHREYAMS JAIN
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)
113 views36 pages

CN Lab Manual

The document describes implementing different computer network simulations using NS2/NS3. It includes instructions for setting up point-to-point networks, running ping and trace route over a 6 node topology, creating an Ethernet LAN with multiple traffic nodes, and measuring congestion windows for different source/destination pairs.

Uploaded by

SHREYAMS JAIN
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/ 36

Computer Networks Lab (18CSL57)

COMPUTER NETWORK LABORATORY


PART A
1. Implement three nodes point – to – point network with duplex links between
them. Set the queue size, vary the bandwidth and find the number of packets
dropped.
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.
3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and
plot congestion window for different source / destination.
4. Implement simple ESS and with transmitting nodes in wire-less LAN by
simulation and determine the performance with respect to transmission of
packets.
5. Implement and study the performance of GSM on NS2/NS3 (Using MAC layer)
or equivalent environment.
6. Implement and study the performance of CDMA on NS2/NS3 (Using stack
called Call net) or equivalent environment.

PART B

Implement the following in Java:


7. Write a program for error detecting code using CRC-CCITT (16- bits).
8. Write a program to find the shortest path between vertices using bellman-ford
algorithm.
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. Implement the above program using as message queues or
FIFOs as IPC channels.
10. Write a program on datagram socket for client/server to display the messages
on client side, typed at the server side.
11. Write a program for simple RSA algorithm to encrypt and decrypt the data.
12. Write a program for congestion control using leaky bucket algorithm.

Department of CS & E, BCE, Shravanabelagola Page 1


Computer Networks Lab (18CSL57)

PART-A
1. Implement three nodes point – to – point network with duplex
links between them. Set the queue size, vary the bandwidth and find
the number of packets dropped.
set ns [ new Simulator ]

set tf [ open program1.tr w ]


$ns trace-all $tf

set nf [ open program1.nam w ]


$ns namtrace-all $nf

# The below code is used to create the nodes.


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#This is used to give color to the packets.


$ns color 1 "red"
$ns color 2 "blue"
$n0 label "Source/udp0"
$n1 label "Source/udp1"
$n2 label "Router"
$n3 label "Destination/Null"

#Vary the below Bandwidth and see the number of packets


dropped.
$ns duplex-link $n0 $n2 10Mb 300ms DropTail
$ns duplex-link $n1 $n2 10Mb 300ms DropTail
$ns duplex-link $n2 $n3 1Mb 300ms DropTail

#The below code is used to set the queue size b/w the nodes
$ns set queue-limit $n0 $n2 10
$ns set queue-limit $n1 $n2 10
$ns set queue-limit $n2 $n3 5

#The below code is used to attach an UDP agent to n0, UDP


#agent to n1 and null agent to n3.
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
set null3 [new Agent/Null]
$ns attach-agent $n3 $null3

set udp1 [new Agent/UDP]


$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1

Department of CS & E, BCE, Shravanabelagola Page 2


Computer Networks Lab (18CSL57)

#The below code sets the udp0 packets to red and udp1
#packets to blue color
$udp0 set class_ 1
$udp1 set class_ 2

#The below code is used to connect the agents.


$ns connect $udp0 $null3
$ns connect $udp1 $null3
#The below code is used to set the packet size to 500

$cbr1 set packetSize_ 500Mb


#The below code is used to set the interval of the packets,
#i.e., Data rate of the packets. if the data rate is high
#then packets drops are high.
$cbr1 set interval_ 0.005
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam program1.nam &
close $tf
close $nf
exit 0
}
$ns at 0.1 "$cbr0 start"
$ns at 0.1 "$cbr1 start"
$ns at 10.0 "finish"
$ns run

Type gedit awk1.awk and type the program


BEGIN{
#include<stdio.h>
count=0;
}
{
if($1=="d") #d stands for the packets drops.
count++
}
END{
printf("The Total no of Packets Dropped due to Congestion
:%d\n\n", count)
}

Department of CS & E, BCE, Shravanabelagola Page 3


Computer Networks Lab (18CSL57)

Output:

Department of CS & E, BCE, Shravanabelagola Page 4


Computer Networks Lab (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.

set ns [new Simulator]

set tf [open program2.tr w]


$ns trace-all $tf

set nf [open program2.nam w]


$ns namtrace-all $nf

set n0 [$ns node]


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

$n0 label "Ping0"


$n4 label "Ping4"
$n5 label "Ping5"
$n6 label "Ping6"
$n2 label "Router"
$ns color 1 "red"
$ns color 2 "green"

$ns duplex-link $n0 $n2 100Mb 300ms DropTail


$ns duplex-link $n2 $n6 1Mb 300ms DropTail
$ns duplex-link $n5 $n2 100Mb 300ms DropTail
$ns duplex-link $n2 $n4 1Mb 300ms DropTail
$ns duplex-link $n3 $n2 1Mb 300ms DropTail
$ns duplex-link $n1 $n2 1Mb 300ms DropTail

$ns queue-limit $n0 $n2 5


$ns queue-limit $n2 $n6 2
$ns queue-limit $n2 $n4 3
$ns queue-limit $n5 $n2 5

#The below code is used to connect between the ping agents


#to the node n0, n4 , n5 and n6.
set ping0 [new Agent/Ping]
$ns attach-agent $n0 $ping0

set ping4 [new Agent/Ping]


$ns attach-agent $n4 $ping4

set ping5 [new Agent/Ping]


$ns attach-agent $n5 $ping5

Department of CS & E, BCE, Shravanabelagola Page 5


Computer Networks Lab (18CSL57)

set ping6 [new Agent/Ping]


$ns attach-agent $n6 $ping6
$ping0 set packetSize_ 50000
$ping0 set interval_ 0.0001
$ping5 set packetSize_ 60000
$ping5 set interval_ 0.00001
$ping0 set class_ 1
$ping5 set class_ 2

$ns connect $ping0 $ping4


$ns connect $ping5 $ping6

#The below function is executed when the ping agent receives


#a reply from the destination
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts " The node [$node_ id] received an reply from $from with
round trip
time of $rtt"
}
proc finish {} {
global ns nf tf
exec nam program2.nam &
$ns flush-trace
close $tf
close $nf
exit 0
}
#The below code makes the link down(failure) at 0.9 from n2
#to n6 and when the time becomes 1.5 the link between n2 to
#n6 is enabled.
$ns rtmodel-at 0.9 down $n2 $n6
$ns rtmodel-at 1.5 up $n2 $n6
$ns at 0.1 "$ping0 send"
$ns at 0.2 "$ping0 send"
$ns at 0.3 "$ping0 send"
$ns at 0.4 "$ping0 send"
$ns at 0.5 "$ping0 send"
$ns at 0.6 "$ping0 send"
$ns at 0.7 "$ping0 send"
$ns at 0.8 "$ping0 send"
$ns at 0.9 "$ping0 send"
$ns at 1.0 "$ping0 send"
$ns at 1.1 "$ping0 send"
$ns at 1.2 "$ping0 send"
$ns at 1.3 "$ping0 send"
$ns at 1.4 "$ping0 send"
$ns at 1.5 "$ping0 send"
$ns at 1.6 "$ping0 send"
$ns at 1.7 "$ping0 send"
$ns at 1.8 "$ping0 send"

Department of CS & E, BCE, Shravanabelagola Page 6


Computer Networks Lab (18CSL57)

$ns at 0.1 "$ping5 send"


$ns at 0.2 "$ping5 send"
$ns at 0.3 "$ping5 send"
$ns at 0.4 "$ping5 send"
$ns at 0.5 "$ping5 send"
$ns at 0.6 "$ping5 send"
$ns at 0.7 "$ping5 send"
$ns at 0.8 "$ping5 send"
$ns at 0.9 "$ping5 send"
$ns at 1.0 "$ping5 send"
$ns at 1.1 "$ping5 send"
$ns at 1.2 "$ping5 send"
$ns at 1.3 "$ping5 send"
$ns at 1.4 "$ping5 send"
$ns at 1.5 "$ping5 send"
$ns at 1.6 "$ping5 send"
$ns at 1.7 "$ping5 send"
$ns at 1.8 "$ping5 send"
$ns at 5.0 "finish"
$ns run

awk2.awk
BEGIN{
#include<stdio.h>
tcp=0;
udp=0;
}
{
if($1=="r"&&$3=="2"&&$4=="3"&& $5=="tcp")
tcp++;
if($1=="r"&&$3=="2"&&$4=="3"&&$5=="cbr")
udp++;
}
END{
printf("\n Total number of packets sent by TCP : %d\n",tcp);
printf("\n Total number of packets sent by UDP : %d\n",udp);
}

Department of CS & E, BCE, Shravanabelagola Page 7


Computer Networks Lab (18CSL57)

Output:

Department of CS & E, BCE, Shravanabelagola Page 8


Computer Networks Lab (18CSL57)

3. Implement an Ethernet LAN using n nodes and set multiple traffic


nodes and plot congestion window for different source / destination.
set ns [new Simulator]

set tf [open program3.tr w]


$ns trace-all $tf

set nf [open program3.nam w]


$ns namtrace-all $nf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns make-lan "$n0 $n1 $n2 $n3" 10mb 10ms LL Queue/DropTail


Mac/802_3

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0

set ftp0 [new Application/FTP]


$ftp0 attach-agent $tcp0

set sink3 [new Agent/TCPSink]


$ns attach-agent $n3 $sink3
$ns connect $tcp0 $sink3

set tcp2 [new Agent/TCP]


$ns attach-agent $n2 $tcp2

set ftp2 [new Application/FTP]


$ftp2 attach-agent $tcp2

set sink1 [new Agent/TCPSink]


$ns attach-agent $n1 $sink1

$ns connect $tcp2 $sink1

######To trace the congestion window##########


set file1 [open file1.tr w]
$tcp0 attach $file1
$tcp0 trace cwnd_
$tcp0 set maxcwnd_ 10

set file2 [open file2.tr w]


$tcp2 attach $file2
$tcp2 trace cwnd_
proc finish { } {
global nf tf ns
$ns flush-trace

Department of CS & E, BCE, Shravanabelagola Page 9


Computer Networks Lab (18CSL57)

exec nam program3.nam &


close $nf
close $tf
exit 0
}
$ns at 0.1 "$ftp0 start"
$ns at 1.5 "$ftp0 stop"
$ns at 2 "$ftp0 start"
$ns at 3 "$ftp0 stop"
$ns at 0.2 "$ftp2 start"
$ns at 2 "$ftp2 stop"
$ns at 2.5 "$ftp2 start"
$ns at 4 "$ftp2 stop"
$ns at 5.0 "finish"
$ns run

awk3.awk
BEGIN {
}
{
if($6=="cwnd_")
printf("%f\t%f\t\n",$1,$7); }
END {
}

Output:

Department of CS & E, BCE, Shravanabelagola Page 10


Computer Networks Lab (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.
set ns [new Simulator]
set tf [open program4.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1000 1000
set nf [open program4.nam w]
$ns namtrace-all-wireless $nf 1000 1000
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON
create-god 3

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]

$n0 label "tcp0"


$n1 label "sink1/tcp1"
$n2 label "sink2"

#The below code is used to give the initial node positions.


$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0
$n2 set X_ 600
$n2 set Y_ 600
$n2 set Z_ 0
$ns at 0.1 "$n0 setdest 50 50 15"
$ns at 0.1 "$n1 setdest 100 100 25"
$ns at 0.1 "$n2 setdest 600 600 25"
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1

Department of CS & E, BCE, Shravanabelagola Page 11


Computer Networks Lab (18CSL57)

$ns connect $tcp0 $sink1


set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp1 $sink2
$ns at 5 "$ftp0 start"
$ns at 5 "$ftp1 start"
#The below code is used to provide the node movements.
$ns at 100 "$n1 setdest 550 550 15"
$ns at 190 "$n1 setdest 70 70 15"
proc finish {} {
global ns nf tf
$ns flush-trace
exec nam program4.nam &
close $tf
exit 0
}
$ns at 250 "finish"
$ns run

awk4.awk
BEGIN{
#include<stdio.h>
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1=="r"&&$3=="_1_"&&$4=="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1=="r"&&$3=="_2_"&&$4=="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %fMbps\n",
((count1*pack1*8)/(time1*1000000)))

Department of CS & E, BCE, Shravanabelagola Page 12


Computer Networks Lab (18CSL57)

printf("The Throughput from n1 to n2: %f Mbps\n",


((count2* pack2 * 8) /(time2*1000000)))
}

Output:

Department of CS & E, BCE, Shravanabelagola Page 13


Computer Networks Lab (18CSL57)

5. Implement and study the performance of GSM on NS2/NS3 (Using


MAC layer) or equivalent environment.
# General Parameters
set opt(title) zero;
set opt(stop) 100;# Stop time.
set opt(ecn) 0;
# Topology
set opt(type) gsm;#type of link:
set opt(secondDelay) 55;# average delay of access links in ms
# AQM parameters
set opt(minth) 30;
set opt(maxth) 0;
set opt(adaptive) 1;
# 1 for Adaptive RED, 0 for plain RED # Traffic generation.
set opt(flows) 0;# number of long-lived TCP flows
set opt(window) 30 ;# window for long-lived traffic
set opt(web) 2;# number of web sessions
# Plotting statistics.
set opt(quiet) 0;# popup anything
set opt(wrap) 100 ;# wrap plots
set opt(srcTrace) is ;# where to plot traffic
set opt(dstTrace) bs2 ;# where to plot traffic
set opt(gsmbuf) 10; # buffer size for gsm
#default downlink bandwidth in bps
set bwDL(gsm) 9600
#default uplink bandwidth in bps
set bwUL(gsm) 9600
#default downlink propagation delay in seconds
set propDL(gsm) .500
#default uplink propagation delay in seconds
set propUL(gsm) .500
#default buffer size in packets
set buf(gsm) 10
set ns [new Simulator]
set tf [open out.tr w]
$ns trace-all $tf
set nodes(is) [$ns node]
set nodes(ms) [$ns node]
set nodes(bs1) [$ns node]
set nodes(bs2) [$ns node]
set nodes(lp) [$ns node]
proc cell_topo {} {
global ns nodes
$ns duplex-link $nodes(lp) $nodes(bs1) 3Mbps 10ms DropTail
$ns duplex-link $nodes(bs1) $nodes(ms) 1 1 RED
$ns duplex-link $nodes(ms) $nodes(bs2) 1 1 RED
$ns duplex-link $nodes(bs2) $nodes(is) 3Mbps 50ms DropTail
puts "Cell Topology"
}
proc set_link_params {t} {
global ns nodes bwUL bwDL propUL propDL buf

Department of CS & E, BCE, Shravanabelagola Page 14


Computer Networks Lab (18CSL57)

$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) simplex


$ns bandwidth $nodes(ms) $nodes(bs1) $bwUL($t) simplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) simplex
$ns bandwidth $nodes(ms) $nodes(bs2) $bwUL($t) simplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) simplex
$ns delay $nodes(ms) $nodes(bs1) $propDL($t) simplex
$ns delay $nodes(bs2) $nodes(ms) $propDL($t) simplex
$ns delay $nodes(ms) $nodes(bs2) $propDL($t) simplex
$ns queue-limit $nodes(bs1) $nodes(ms) $buf($t)
$ns queue-limit $nodes(ms) $nodes(bs1) $buf($t)
$ns queue-limit $nodes(bs2) $nodes(ms) $buf($t)
$ns queue-limit $nodes(ms) $nodes(bs2) $buf($t)
}
# RED and TCP parameters
Queue/RED set summarystats_ true
Queue/DropTail set summarystats_ true
Queue/RED set adaptive_ $opt(adaptive)
Queue/RED set q_weight_ 0.0
Queue/RED set thresh_ $opt(minth)
Queue/RED set maxthresh_ $opt(maxth)
Queue/DropTail set shrink_drops_ true
Agent/TCP set ecn_ $opt(ecn)
Agent/TCP set window_ $opt(window)
DelayLink set avoidReordering_ true
#source web.tcl
#Create topology
switch $opt(type) {
gsm -
gprs -
umts {cell_topo}
}
set_link_params $opt(type)
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(bs1) $nodes(ms) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]
$ns insert-delayer $nodes(bs2) $nodes(ms) [new Delayer]
# Set up forward TCP connection
if {$opt(flows) == 0} {
set tcp1 [$ns create-connection TCP/Sack1 $nodes(is)
TCPSink/Sack1 $nodes(lp) 0]
set ftp1 [[set tcp1] attach-app FTP]
$ns at 0.8 "[set ftp1] start"
}
if {$opt(flows) > 0} {
set tcp1 [$ns create-connection TCP/Sack1 $nodes(is)
TCPSink/Sack1 $nodes(lp) 0]
set ftp1 [[set tcp1] attach-app FTP]
$tcp1 set window_ 100
$ns at 0.0 "[set ftp1] start"
$ns at 3.5 "[set ftp1] stop"

Department of CS & E, BCE, Shravanabelagola Page 15


Computer Networks Lab (18CSL57)

set tcp2 [$ns create-connection TCP/Sack1 $nodes(is)


TCPSink/Sack1 $nodes(lp) 0]
set ftp2 [[set tcp2] attach-app FTP]
$tcp2 set window_ 3
$ns at 1.0 "[set ftp2] start"
$ns at 8.0 "[set ftp2] stop"
}
proc stop {} {
global nodes opt nf
set wrap $opt(wrap)
set sid [$nodes($opt(srcTrace)) id]
set did [$nodes($opt(dstTrace)) id]
if {$opt(srcTrace) == "is"} {
set a "-a out.tr"
} else {
set a "out.tr"
}
set GETRC "//home/bce/ns-allinone-2.35/ns-2.35/bin/getrc"
set RAW2XG "//home/bce/ns-allinone-2.35/ns-2.35/bin/raw2xg"
exec $GETRC -s $sid -d $did -f 0 out.tr | \
$RAW2XG -s 0.01 -m $wrap -r > plot.xgr
exec $GETRC -s $did -d $sid -f 0 out.tr | \
$RAW2XG -a -s 0.01 -m $wrap >> plot.xgr
exec $GETRC -s $sid -d $did -f 1 out.tr | \
$RAW2XG -s 0.01 -m $wrap -r >> plot.xgr
exec $GETRC -s $did -d $sid -f 1 out.tr | \
$RAW2XG -s 0.01 -m $wrap -a >> plot.xgr
exec ./xg2gp.awk plot.xgr
if {!$opt(quiet)} {
exec xgraph -bb -tk -nl -m -x time -y packets plot.xgr &
}
exit 0
}
$ns at $opt(stop) "stop"
$ns run

xg2gp.awk
#!/usr/bin/awk -f
# convert from raw2xg output into gnuplot format
BEGIN {system("rm -f acks packets drops")}
{if (ack) print $1 " " $2 >>"acks"}
{if (pkt) print $1 " " $2 >>"packets"}
{if (drp) print $1 " " $2 >>"drops"}
$1 ~ /^.ack/ {ack=1}
$1 ~ /^.packets/ {pkt=1}
$1 ~ /^.drops/ {drp=1}
$1 ~ /^$/ {ack=0; pkt=0; drp=0}

Department of CS & E, BCE, Shravanabelagola Page 16


Computer Networks Lab (18CSL57)

Output:

Department of CS & E, BCE, Shravanabelagola Page 17


Computer Networks Lab (18CSL57)

6. Implement and study the performance of CDMA on NS2/NS3


(Using stack called Call net) or equivalent environment.
# General Parameters
set opt(ecn) 0 ;
set opt(window) 30 ;
# Topology
set opt(type) gsm ; #type of link:
# AQM parameters
set opt(minth) 5 ;
set opt(maxth) 10 ;
set opt(adaptive) 1 ; # 1 for Adaptive RED, 0 for plain RED
#default downlink bandwidth in bps
set bwDL(gsm) 9600
#default uplink bandwidth in bps
set bwUL(gsm) 9600
#default downlink propagation delay in seconds
set propDL(gsm) .500
#default uplink propagation delay in seconds
set propUL(gsm) .500
#default buffer size in packets
set buf(gsm) 10
set ns [new Simulator]
set tf [open program6.tr w]
set nf [open program6.nam w]
$ns trace-all $tf
$ns namtrace-all $nf
set nodes(s) [$ns node]
set nodes(bs1) [$ns node]
set nodes(ms) [$ns node]
set nodes(bs2) [$ns node]
set nodes(d) [$ns node]
proc cell_topo {} {
global ns nodes
$ns duplex-link $nodes(s) $nodes(bs1) 3Mbps 10ms DropTail
$ns duplex-link $nodes(bs1) $nodes(ms) 1Mbps 1ms RED
$ns duplex-link $nodes(ms) $nodes(bs2) 1Mbps 1ms RED
$ns duplex-link $nodes(bs2) $nodes(d) 3Mbps 50ms DropTail
puts "Cell Topology"
}

proc set_link_params {t} {


global ns nodes bwUL bwDL propUL propDL buf
$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) simplex
$ns bandwidth $nodes(ms) $nodes(bs1) $bwUL($t) simplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) simplex
$ns bandwidth $nodes(ms) $nodes(bs2) $bwUL($t) simplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) simplex
$ns delay $nodes(ms) $nodes(bs1) $propDL($t) simplex
$ns delay $nodes(bs2) $nodes(ms) $propDL($t) simplex
$ns delay $nodes(ms) $nodes(bs2) $propDL($t) simplex
$ns queue-limit $nodes(bs1) $nodes(ms) $buf($t)

Department of CS & E, BCE, Shravanabelagola Page 18


Computer Networks Lab (18CSL57)

$ns queue-limit $nodes(ms) $nodes(bs1) $buf($t)


$ns queue-limit $nodes(bs2) $nodes(ms) $buf($t)
$ns queue-limit $nodes(ms) $nodes(bs2) $buf($t)

}
# RED and TCP parameters
Queue/RED set summarystats_ true
Queue/DropTail set summarystats_ true
Queue/RED set adaptive_ $opt(adaptive)
Queue/RED set q_weight_ 0.0
Queue/RED set thresh_ $opt(minth)
Queue/RED set maxthresh_ $opt(maxth)
Queue/DropTail set shrink_drops_ true
Agent/TCP set ecn_ $opt(ecn)
Agent/TCP set window_ $opt(window)
DelayLink set avoidReordering_ true
#Create topology
switch $opt(type) {
gsm - gprs - umts {cell_topo}
}

set_link_params $opt(type)
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(bs1) $nodes(ms) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]
$ns insert-delayer $nodes(bs2) $nodes(ms) [new Delayer]

# Set up forward TCP connection


set tcp1 [$ns create-connection TCP/Sack1 $nodes(s)
TCPSink/Sack1 $nodes(d) 0]
set ftp1 [[set tcp1] attach-app FTP]
$ns at 0.5 "$ftp1 start"
proc stop {} {
global nodes ns opt nf tf
$ns flush-trace
close $nf
close $tf
exec nam program6.nam &
exit 0
}
$ns at 100 "stop"
$ns run

awk6.awk
BEGIN {
PacketRcvd=0;
Throughput=0.0;
}
{
if(($1=="r") && ($5=="tcp") && ($10=4.0))

Department of CS & E, BCE, Shravanabelagola Page 19


Computer Networks Lab (18CSL57)

{
PacketRcvd++;
}
}
END {
Throughput=((PacketRcvd*1000*8)/(95.0*1000000));
printf("packet received:%f\n", PacketRcvd);
printf( "the throughput is:%f\n",Throughput);
}

Output:

Department of CS & E, BCE, Shravanabelagola Page 20


Computer Networks Lab (18CSL57)

PART B
7. Write a program for error detecting code using CRC-CCITT (16- bits).
import java.io.*;
class crc_gen
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];

System.out.println("Enter data bits : ");


for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];

System.out.println("Enter Divisor bits : ");


for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
System.out.print("Data bits are : ");
for(int i=0; i< data_bits; i++)
System.out.print(data[i]);
System.out.println();

System.out.print("divisor bits are : ");


for(int i=0; i< divisor_bits; i++)
System.out.print(divisor[i]);
System.out.println();
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];

/* CRC GENERATION */

Department of CS & E, BCE, Shravanabelagola Page 21


Computer Networks Lab (18CSL57)

for(int i=0;i<data.length;i++)
div[i]=data[i];

System.out.print("Dividend (after appending 0's) are : ");


for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++)
{
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
for(int i=0;i<div.length;i++) //append dividend and remainder
{
crc[i]=(div[i]^rem[i]);
}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
/* ERROR DETECTION */
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
System.out.print("crc bits are : ");
for(int i=0; i< crc.length; i++)
System.out.print(crc[i]);
System.out.println();
for(int j=0; j<crc.length; j++)
{
rem[j] = crc[j];
}
rem=divide(crc, divisor, rem);
for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)
{
System.out.println("Error");
break;
}
if(i==rem.length-1)
System.out.println("No Error");
}

Department of CS & E, BCE, Shravanabelagola Page 22


Computer Networks Lab (18CSL57)

System.out.println("THANK YOU.");
}

static int[] divide(int div[],int divisor[], int rem[])


{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);

while(rem[cur]==0 && cur!=rem.length-1)


cur++;

if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}
OUTPUT 1:
Enter number of data bits :
10
Enter data bits :
1
1
0
1
0
1
1
0
1
1
Enter number of bits in divisor :
5
Enter Divisor bits :
1
0
0
1
1
Data bits are : 1101011011
divisor bits are : 10011
Dividend (after appending 0's) are : 11010110110000

CRC code :

Department of CS & E, BCE, Shravanabelagola Page 23


Computer Networks Lab (18CSL57)

11010110111110
Enter CRC code of 14 bits :
1
1
0
1
0
1
1
0
1
1
1
1
1
0
crc bits are : 11010110111110
No Error
THANK YOU.

OUTPUT 2:
Enter number of data bits :
10
Enter data bits :
1
1
0
1
0
1
1
0
1
1
Enter number of bits in divisor :
5
Enter Divisor bits :
1
0
0
1
1
Data bits are : 1101011011
divisor bits are : 10011
Dividend (after appending 0's) are : 11010110110000

CRC code :
11010110111110
Enter CRC code of 14 bits :
1

Department of CS & E, BCE, Shravanabelagola Page 24


Computer Networks Lab (18CSL57)

1
0
1
0
1
1
0
1
1
1
1
1
1
crc bits are : 11010110111111
Error
THANK YOU.

Department of CS & E, BCE, Shravanabelagola Page 25


Computer Networks Lab (18CSL57)

8. Write a program to find the shortest path between vertices using


bellman-ford

java.util.Scanner;
public class bellmanford
{
private int distances[];
private int numberofvertices;
public static final int MAX_VALUE = 999;
public bellmanford(int numberofvertices)
{
this.numberofvertices = numberofvertices;
distances = new int[numberofvertices + 1];
}
public void BellmanFordEvaluation(int source, int
destination,int adjacencymatrix[][])
{
for (int node = 1; node <= numberofvertices; node++)
{
distances[node] = MAX_VALUE;
}
distances[source] = 0;
for (int node = 1; node <= numberofvertices - 1;
node++)
{
for (int sourcenode = 1; sourcenode <=
numberofvertices; sourcenode++)
{
for (int destinationnode = 1;
destinationnode <= numberofvertices; destinationnode++)
{
if
(adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
{
if (distances[destinationnode] >
distances[sourcenode]+
adjacencymatrix[sourcenode][destinationnode])
distances[destinationnode]
= distances[sourcenode]+
adjacencymatrix[sourcenode][destinationnode];
}
}
}
}
for (int sourcenode = 1; sourcenode <=
numberofvertices; sourcenode++)
{
for (int destinationnode = 1; destinationnode
<= numberofvertices; destinationnode++)
{

Department of CS & E, BCE, Shravanabelagola Page 26


Computer Networks Lab (18CSL57)

if
(adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
{
if (distances[destinationnode] >
distances[sourcenode]+
adjacencymatrix[sourcenode][destinationnode])
System.out.println("The Graph
contains negative egde cycle");
}
}
}
for (int vertex = 1; vertex <= numberofvertices;
vertex++)
{
if (vertex == destination)
System.out.println("distance of source " +
source + " to "+ vertex + " is " + distances[vertex]);
}
}
public static void main(String[ ] args)
{
int numberofvertices = 0;
int source, destination;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
numberofvertices = scanner.nextInt();
int adjacencymatrix[][] = new int[numberofvertices +
1][numberofvertices + 1];
System.out.println("Enter the adjacency matrix");
for (int sourcenode = 1; sourcenode <=
numberofvertices; sourcenode++)
{
for (int destinationnode = 1; destinationnode
<= numberofvertices; destinationnode++)
{

adjacencymatrix[sourcenode][destinationnode] =
scanner.nextInt();
if (sourcenode == destinationnode)
{

adjacencymatrix[sourcenode][destinationnode] = 0;
continue;
}
if
(adjacencymatrix[sourcenode][destinationnode] == 0)
{

adjacencymatrix[sourcenode][destinationnode] = MAX_VALUE;
}
}

Department of CS & E, BCE, Shravanabelagola Page 27


Computer Networks Lab (18CSL57)

}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
System.out.println("Enter the destination vertex:
");
destination = scanner.nextInt();
bellmanford bellmanford = new
bellmanford(numberofvertices);
bellmanford.BellmanFordEvaluation(source,
destination, adjacencymatrix);
scanner.close();
}
}

Output:

Enter the number of vertices


5
Enter the adjacency matrix
0 6 5 0 0
0 0 0 -1 0
0 -2 0 4 3
0 0 0 0 3
0 0 0 0 0
Enter the source vertex
1
Enter the destination vertex:
5
distance of source 1 to 5 is 5

Department of CS & E, BCE, Shravanabelagola Page 28


Computer Networks Lab (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 Program:
import java.io.*;
import java.net.*;
public class client1
{
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 1136);
DataOutputStream outToServer = new DataOutputStream
(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader
(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}

Server Program:

import java.io.*;
import java.net.*;
public class server1
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(1136);
while (true)
{

Department of CS & E, BCE, Shravanabelagola Page 29


Computer Networks Lab (18CSL57)

Socket connectionSocket = welcomeSocket.accept();


BufferedReader inFromClient = new BufferedReader(new
InputStreamReader
(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream
(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() +
'\n';
outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
}
}
}

Output:
Run Server program first and then run Client program.
hiiiiii, how are you. computer networks.
FROM SERVER: HIIIIII, HOW ARE YOU. COMPUTER NETWORKS.

Department of CS & E, BCE, Shravanabelagola Page 30


Computer Networks Lab (18CSL57)

10. Write a program on datagram socket for client/server to display


the messages onclient side, typed at the server side.

Client Program
import java.io.*;
import java.net.*;
public class client
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, 9012);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new
String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}

Server Program
import java.io.*;
import java.net.*;
public class server
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9012);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();

Department of CS & E, BCE, Shravanabelagola Page 31


Computer Networks Lab (18CSL57)

int port = receivePacket.getPort();


String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
serverSocket.send(sendPacket);
}
}
}
Output:
welcome back. client server program using udp. program 10. udp
connectionless service.
FROM SERVER:WELCOME BACK. CLIENT SERVER PROGRAM USING
UDP. PROGRAM 10. UDP CONNECTIONLESS SERVICE

Department of CS & E, BCE, Shravanabelagola Page 32


Computer Networks Lab (18CSL57)

11. Write a program for simple RSA algorithm to encrypt and decrypt
the data.
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.io.*;
import javax.crypto.Cipher;
public class rsa {
public static void main(String args[])
{
String srci="";
try{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please enter any string you want to
encrypt");
srci=br.readLine();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
try{
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);//initialize key pairs to 512 bits ,you can
//also take 1024 or 2048 bits
KeyPair kp=kpg.genKeyPair();
PublicKey publi=kp.getPublic();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publi);
byte[]src=srci.getBytes();//converting source data into byte
//array
byte[] cipherData = cipher.doFinal(src);//use this method to
finally encrypt data
String srco=new String(cipherData);//converting byte array into
//string
System.out.println();
System.out.println("Encrypted data is:-"+srco);
PrivateKey privatei=kp.getPrivate();//Generating private key

Department of CS & E, BCE, Shravanabelagola Page 33


Computer Networks Lab (18CSL57)

Cipher cipheri=Cipher.getInstance("RSA");//Intializing 2nd


//instance of Cipher class
cipheri.init(Cipher.DECRYPT_MODE, privatei);//Setting to
//decrypt_mode
byte[] cipherDat = cipheri.doFinal(cipherData);//Finally
//decrypting data
String decryptdata=new String(cipherDat);
System.out.println("Decrypted data:-"+decryptdata);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Output:
Please enter any string you want to encrypt
Bahubali College of Engineering, Shravanabelagola

Encrypted data is:-


_?®e£H´·è}€ x¥’__ƒ7É¢\n_EÚ®™_7_å_Ï~È_§ÞoS_NŸ4åpës3îùœ[o¼~ŽðÍ|
–?
Decrypted data:-Bahubali College of Engineering,
Shravanabelagola

Department of CS & E, BCE, Shravanabelagola Page 34


Computer Networks Lab (18CSL57)

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

import java.util.*;
public class leaky
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the bucket size");
int bktsize=scan.nextInt();
System.out.println("Enter the output rate:");
int oprate=scan.nextInt();
for(int i=1;i<=5;i++)
{
System.out.println("Enter the size of the packet of"+i);
int pktsize=scan.nextInt();
if(pktsize>bktsize)
{
System.out.println("Bucket Overflow");
}
else
{
while(pktsize>=oprate)
{
System.out.println("Bytes
Outputted"+oprate);
try
{
Thread.sleep(1000);
}
catch( Exception e)
{
}
pktsize=pktsize-oprate;
}
if(pktsize>0)
{
System.out.println("Last Byte
Outputted"+pktsize);
}
}

}
}

Department of CS & E, BCE, Shravanabelagola Page 35


Computer Networks Lab (18CSL57)

Output:
Enter the bucket size
500
Enter the output rate:
100
Enter the size of the packet of1
400
Bytes Outputted100
Bytes Outputted100
Bytes Outputted100
Bytes Outputted100
Enter the size of the packet of2
250
Bytes Outputted100
Bytes Outputted100
Last Byre Outputted50
Enter the size of the packet of3
375
Bytes Outputted100
Bytes Outputted100
Bytes Outputted100
Last Byre Outputted75
Enter the size of the packet of4
550
Bucket Overflow
Enter the size of the packet of5
250
Bytes Outputted100
Bytes Outputted100
Last Byre Outputted50

Department of CS & E, BCE, Shravanabelagola Page 36

You might also like