0% found this document useful (0 votes)
187 views33 pages

18CSL57 CN Lab Manual

The document provides details of the Computer Network Laboratory course offered at NIE Institute of Technology. It includes the course objectives, description, list of 12 experiments to be conducted in the laboratory as part of the course. The experiments cover topics like implementing simple networks, studying performance of GSM and CDMA networks, implementing protocols like CRC, shortest path algorithms, sockets for client-server programming and RSA encryption. Sample code is provided for the first 3 experiments.

Uploaded by

Manvanth B C
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)
187 views33 pages

18CSL57 CN Lab Manual

The document provides details of the Computer Network Laboratory course offered at NIE Institute of Technology. It includes the course objectives, description, list of 12 experiments to be conducted in the laboratory as part of the course. The experiments cover topics like implementing simple networks, studying performance of GSM and CDMA networks, implementing protocols like CRC, shortest path algorithms, sockets for client-server programming and RSA encryption. Sample code is provided for the first 3 experiments.

Uploaded by

Manvanth B C
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/ 33

NIE Institute of Technology

Koorgalli, Mysore.

Department
Of
Information Science & Engineering.

Computer Network Laboratory


(18CSL57)

Prepared by
K B Manasa
Assistant Professor
Dept of ISE
COMPUTER NETWORK LABORATORY
[As per Choice Based Credit System (CBCS) scheme]
(Effective from the academic year 2016 -2017)

SEMESTER – V

Subject Code 18CSL57 IA Marks 40


Number of Lecture Hours/Week 01I + 02P Exam Marks 60
Total Number of Lecture Hours 40 Exam Hours 03
Course objectives: This course will enable students to
 Demonstrate the operation of network and its management commands
 Simulate and demonstrate the performance of GSM and CDMA
 Analyse and Compare various networking protocols
 Implement data link layer and transport layer protocols
 Demonstrate the working of different concepts of networking.
Description (If any):

For the experiments below modify the topology and parameters set for the experiment and take
multiple rounds of reading and analyse the results available in log files. Plot necessary Graphs and
conclude. Use NS2/NS3.
Lab Experiments:

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.

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.
Computer Network Laboratory 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] /* Letter S is capital */


set nf [open lab1.nam w] /* open a nam trace file in write mode */
$ns namtrace-all $nf /* nf – nam file */
set tf [open lab1.tr w] /* tf- trace file */
$ns trace-all $tf
proc finish { } { /* provide space b/w proc and finish and all are in small case */ global ns nf tf
$ns flush-trace /* clears trace file contents */ close $nf
close $tf
exec nam lab1.nam & exit 0
}
set n0 [$ns node] /* creates 4 nodes */ set n1 [$ns node]
set n2 [$ns node] set n3 [$ns node]
$ns duplex-link $n0 $n2 200Mb 10ms DropTail /*Letter M is capital Mb*/
$ns duplex-link $n1 $n2 100Mb 5ms DropTail /*D and T are capital*/
$ns duplex-link $n2 $n3 1Mb 1000ms DropTail
$ns queue-limit $n0 $n2 10
$ns queue-limit $n1 $n2 10
set udp0 [new Agent/UDP] /* Letters A,U,D and P are capital */
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR] /* A,T,C,B and R are capital*/
$cbr0 set packetSize_ 500 /*S is capital, space after underscore*/
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set udp2 [new Agent/UDP]
$ns attach-agent $n2 $udp2
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp2
set null0 [new Agent/Null] /* A and N are capital */
$ns attach-agent $n3 $null0
$ns connect $udp0 $null0
$ns connect $udp1 $null0
$ns at 0.1 "$cbr0 start"
$ns at 0.2 "$cbr1 start"
$ns at 1.0 "finish"

DEPT OF ISE Page 4


Computer Network Laboratory 18CSL57

$ns run

AWK file (Open a new editor using “vi command” and write awk file and save with “.awk”
extension)
/*immediately after BEGIN should open braces ‘{‘
BEGIN { c=0;
}
{
If ($1= ="d")
{
c++;
printf("%s\t%s\n",$5,$11);
}
}
/*immediately after END should open braces ‘{‘
END{
printf("The number of packets dropped =%d\n",c);
}

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.
//gedit lab2.tcl

set ns [ new Simulator ]


set nf [ open lab2.nam w ]
$ns namtrace-all $nf
set tf [ open lab2.tr w ]
$ns trace-all $tf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$n4 shape box
$ns duplex-link $n0 $n4 1005Mb 1ms DropTail
$ns duplex-link $n1 $n4 50Mb 1ms DropTail
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail
$ns duplex-link $n3 $n4 200Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 1ms DropTail
set p1 [new Agent/Ping]
$ns attach-agent $n0 $p1
$p1 set packetSize_ 50000
$p1 set interval_ 0.0001

DEPT OF ISE Page 5


Computer Network Laboratory 18CSL57

set p2 [new Agent/Ping]


$ns attach-agent $n1 $p2
set p3 [new Agent/Ping]
$ns attach-agent $n2 $p3
$p3 set packetSize_ 30000
$p3 set interval_ 0.00001
set p4 [new Agent/Ping]
$ns attach-agent $n3 $p4
set p5 [new Agent/Ping]
$ns attach-agent $n5 $p5
$ns queue-limit $n0 $n4 5
$ns queue-limit $n2 $n4 3
$ns queue-limit $n4 $n5 2
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received answer from $from with round trip time $rtt msec"
}
$ns connect $p1 $p5
$ns connect $p3 $p4
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab2.nam &
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 1.1 "$p1 send"
$ns at 1.2 "$p1 send"
$ns at 1.3 "$p1 send"
$ns at 1.4 "$p1 send"
$ns at 1.5 "$p1 send"
$ns at 1.6 "$p1 send"
$ns at 1.7 "$p1 send"

DEPT OF ISE Page 6


Computer Network Laboratory 18CSL57

$ns at 1.8 "$p1 send"


$ns at 1.9 "$p1 send"
$ns at 2.0 "$p1 send"
$ns at 2.1 "$p1 send"
$ns at 2.2 "$p1 send"
$ns at 2.3 "$p1 send"
$ns at 2.4 "$p1 send"
$ns at 2.5 "$p1 send"
$ns at 2.6 "$p1 send"
$ns at 2.7 "$p1 send"
$ns at 2.8 "$p1 send"
$ns at 2.9 "$p1 send"
$ns at 0.1 "$p3 send"
$ns at 0.2 "$p3 send"
$ns at 0.3 "$p3 send"
$ns at 0.4 "$p3 send"
$ns at 0.5 "$p3 send"
$ns at 0.6 "$p3 send"
$ns at 0.7 "$p3 send"
$ns at 0.8 "$p3 send"
$ns at 0.9 "$p3 send"
$ns at 1.0 "$p3 send"
$ns at 1.1 "$p3 send"
$ns at 1.2 "$p3 send"
$ns at 1.3 "$p3 send"
$ns at 1.4 "$p3 send"
$ns at 1.5 "$p3 send"
$ns at 1.6 "$p3 send"
$ns at 1.7 "$p3 send"
$ns at 1.8 "$p3 send"
$ns at 1.9 "$p3 send"
$ns at 2.0 "$p3 send"
$ns at 2.1 "$p3 send"
$ns at 2.2 "$p3 send"
$ns at 2.3 "$p3 send"
$ns at 2.4 "$p3 send"
$ns at 2.5 "$p3 send"
$ns at 2.6 "$p3 send"
$ns at 2.7 "$p3 send"
$ns at 2.8 "$p3 send"
$ns at 2.9 "$p3 send"
$ns at 3.0 "finish"
$ns run

DEPT OF ISE Page 7


Computer Network Laboratory 18CSL57

//AWK File:
//gedit lab2.awk

BEGIN{
drop=0;
}
{
if($1= ="d" )
{
drop++;
}
}
END{
printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);
}

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

congestion window for different source / destination.

//gedit lab3.tcl

set ns [new Simulator]


set tf [open lab3.tr w]
$ns trace-all $tf
set nf [open lab3.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
$n0 color "magenta"
$n0 label "src1"
set n1 [$ns node]
set n2 [$ns node]
$n2 color "magenta"
$n2 label "src2"
set n3 [$ns node]
$n3 color "blue"
$n3 label "dest2"
set n4 [$ns node]
set n5 [$ns node]
$n5 color "blue"
$n5 l
abel "dest1"
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
/* should come in single line */
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

DEPT OF ISE Page 8


Computer Network Laboratory 18CSL57

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001
set sink5 [new Agent/TCPSink]
$ns attach-agent $n5 $sink5
$ns connect $tcp0 $sink5
set tcp2 [new Agent/TCP]
$ns attach-agent $n2 $tcp2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set packetSize_ 600
$ftp2 set interval_ 0.001
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp2 $sink3
set file1 [open file1.tr w]
$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp2 attach $file2
$tcp0 trace cwnd_ /* must put underscore ( _ ) after cwnd and no space between
them*/
$tcp2 trace cwnd_
proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam lab3.nam &
exit 0
}
$ns at 0.1 "$ftp0 start"
$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"
$ns at 0.2 "$ftp2 start"
$ns at 8 "$ftp2 stop"
$ns at 14 "$ftp0 stop"
$ns at 10 "$ftp2 start"
$ns at 15 "$ftp2 stop"
$ns at 16 "finish"
$ns run

DEPT OF ISE Page 9


Computer Network Laboratory 18CSL57

//gedit lab3.awk

BEGIN {
}{
if($6= ="cwnd_") /* don’t leave space after writing cwnd_ */
printf("%f\t%f\t\n",$1,$7); /* you must put \n in printf */
}
END {
}

4. Implement simple ESS and with transmitting nodes in wire-less LAN by simulation

and determine the performance with respect to transmission of packets.


//gedit lab4.tcl

set ns [new Simulator]


set tf [open lab4.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1000 1000
set nf [open lab4.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"
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0

DEPT OF ISE Page 10


Computer Network Laboratory 18CSL57

$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
$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"
$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 lab4.nam &
close $tf
exit 0
}
$ns at 250 "finish"
$ns run

//gedit lab4.awk

BEGIN{
count1=0
count2=0
pack1=0
pack2=0

DEPT OF ISE Page 11


Computer Network Laboratory 18CSL57

time1=0
time2=0
}{
if($1= ="r"&& $3= ="_1_" && $4= ="AGT")
{
count1++
pack1=pack1+$6
time1=$2
}
if($1= ="r" && $3= ="_2_" && $4= ="AGT")
{
count2++
pack2=pack2+$6
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %f Mbps \n”, ((count1*pack1*8)/(time1*1000000)));
printf("The Throughput from n1 to n2: %f Mbps", ((count2*pack2*8)/(time2*1000000)));
}

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

# General Parameters
set stop 100 ;# Stop time.
# Topology
set type gsm ;#type of link:
# AQM parameters
set minth 30 ;
set maxth 0 ;
set adaptive 1 ;# 1 for Adaptive RED, 0 for plain RED
# Traffic generation.
set flows 0 ;# number of long-lived TCP flows
set window 30 ;# window for long-lived traffic
set web 2 ;# number of web sessions
# Plotting statics.
set opt(wrap) 100 ;# wrap plots?
set opt(srcTrace) is ;# where to plot traffic
set opt(dstTrace) bs2 ;# where to plot traffic

#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
DEPT OF ISE Page 12
Computer Network Laboratory 18CSL57

#default uplink propagation delay in seconds


set propUL(gsm) .500

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 10nodes(ms) 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 50nodes(ms) DropTail
puts " GSM Cell Topology"
}
proc set_link_para {t} {
global ns nodes bwUL bwDL propUL propDL buf
$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) duplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) duplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) duplex
$ns delay $nodes(bs2) $nodes(ms) $propDL($t) duplex
$ns queue-limit $nodes(bs1) $nodes(ms) 10
$ns queue-limit $nodes(bs2) $nodes(ms) 10
}
# RED and TCP parameters
Queue/RED set adaptive_ $adaptive
Queue/RED set thresh_ $minth
Queue/RED set maxthresh_ $maxth
Agent/TCP set window_ $window

source web.tcl

#Create topology
switch $type {
gsm -
gprs -
umts {cell_topo}
}
set_link_para $type
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]

# Set up forward TCP connection


if {$flows == 0} {
DEPT OF ISE Page 13
Computer Network Laboratory 18CSL57

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 {$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"

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]
set a "out.tr"
set GETRC "../../../bin/getrc"
set RAW2XG "../../../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 xgraph -x time -y packets plot.xgr &
exit 0
}
$ns at $stop "stop"
$ns run

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

Source Code:
# General Parameters
set stop 100 ;# Stop time.
# Topology
set type cdma ;#type of link:
# AQM parameters
set minth 30 ;
set maxth 0 ;
set adaptive 1 ;# 1 for Adaptive RED, 0 for plain RED
# Traffic generation.
set flows 0 ;# number of long-lived TCP flows
DEPT OF ISE Page 14
Computer Network Laboratory 18CSL57

set window 30 ;# window for long-lived traffic


set web 2 ;# number of web sessions
# Plotting statics.
set opt(wrap) 100 ;# wrap plots?
set opt(srcTrace) is ;# where to plot traffic
set opt(dstTrace) bs2 ;# where to plot traffic

#default downlink bandwidth in bps


set bwDL(cdma) 384000
#default uplink bandwidth in bps
set bwUL(cdma) 64000
#default downlink propagation delay in seconds
set propDL(cdma) .150
#default uplink propagation delay in seconds
set propUL(cdma) .150

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 10nodes(ms) 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 50nodes(ms) DropTail
puts " cdma Cell Topology"
}

proc set_link_para {t} {


global ns nodes bwUL bwDL propUL propDL buf
$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) duplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) duplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) duplex
$ns delay $nodes(bs2) $nodes(ms) $propDL($t) duplex
$ns queue-limit $nodes(bs1) $nodes(ms) 20
$ns queue-limit $nodes(bs2) $nodes(ms) 20
}
# RED and TCP parameters
Queue/RED set adaptive_ $adaptive
Queue/RED set thresh_ $minth
Queue/RED set maxthresh_ $maxth
Agent/TCP set window_ $window

source web.tcl
DEPT OF ISE Page 15
Computer Network Laboratory 18CSL57

#Create topology
switch $type {

cdma {cell_topo}
}
set_link_para $type
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]

# Set up forward TCP connection


if {$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 {$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"
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]
set a "out.tr"
set GETRC "../../../bin/getrc"
set RAW2XG "../../../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 xgraph -x time -y packets plot.xgr &
exit 0
}
$ns at $stop "stop"
$ns run

DEPT OF ISE Page 16


Computer Network Laboratory 18CSL57

PART B (Implement the following in Java):

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

import java.io.*;
public class crc {

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


{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[] data,div,divisor,rem,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());
tot_length=data_bits+divisor_bits-1;

div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];

/*------------------ CRC GENERATION --------------------- */


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

DEPT OF ISE Page 17


Computer Network Laboratory 18CSL57

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 ramainder
{
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());

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++)

DEPT OF ISE Page 18


Computer Network Laboratory 18CSL57

if(rem[i]!=0)

{
System.out.println("Error");
break;

}
if(i==rem.length-1)
System.out.println("No Error");

}
}

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;
}
}

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

package bellman;

import java.util.Scanner;
public class BellmanFord
{

DEPT OF ISE Page 19


Computer Network Laboratory 18CSL57

private int D[]; private int num_ver;


public static final int MAX_VALUE = 999;
public BellmanFord(int num_ver)
{
this.num_ver = num_ver; D = new int[num_ver + 1];
}
public void BellmanFordEvaluation(int source, int A[][])
{
for (int node = 1; node <= num_ver; node++)
{
D[node] = MAX_VALUE;
}
D[source] = 0;
int flag =0;
for (int node = 1; node <= num_ver - 1; node++)
{
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
D[dn] = D[sn] + A[sn][dn];
}
}
}
}
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
flag=1;

}
}
}
if(flag==1)
{
System.out.println("The Graph contains negative egde cycle");
}
else

for (int vertex = 1; vertex <= num_ver; vertex++)


{
System.out.println("distance of source " + source + " to "+ vertex + " is " + D[vertex]);
}
}

DEPT OF ISE Page 20


Computer Network Laboratory 18CSL57

public static void main(String[ ] args)


{
int num_ver = 0; int source;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
num_ver = scanner.nextInt();
int A[][] = new int[num_ver + 1][num_ver + 1];
System.out.println("Enter the adjacency matrix");
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
A[sn][dn] = scanner.nextInt(); if (sn == dn)
{
A[sn][dn] = 0; continue;
}
if (A[sn][dn] == 0)
{
A[sn][dn] = MAX_VALUE;
}
}
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
BellmanFord b = new BellmanFord (num_ver);
b.BellmanFordEvaluation(source, A);
scanner.close();
}

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.

// TCP Server File;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class fileServer extends Thread {

private ServerSocket ss;

public fileServer(int port) {


try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
DEPT OF ISE Page 21
Computer Network Laboratory 18CSL57

}
}

public void run() {


while (true) {
try {
Socket s= ss.accept();
sendFile("test.txt",s);

DEPT OF ISE Page 22


Computer Network Laboratory 18CSL57

} catch (IOException e) {
e.printStackTrace();
}
}
}

public void sendFile(String file,Socket s) throws IOException {


DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4096];

while (fis.read(buffer) > 0) {


dos.write(buffer);
}

fis.close();
dos.close();
}

public static void main(String args[]) {


fileServer fs = new fileServer(9886);
fs.start();
}

// TCP Client File;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.IOException;
import java.net.Socket;

public class fileclient{

private Socket s;

public fileclient(String host, int port) {


try {
s = new Socket(host, port);
saveFile(s);
} catch (Exception e) {
e.printStackTrace();
}
}

private void saveFile(Socket clientSock) throws IOException {


DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("D:/test.txt");
byte[] buffer = new byte[4096];
DEPT OF ISE Page 23
Computer Network Laboratory 18CSL57

int read = 0;

while((read = dis.read(buffer, 0,

(buffer.length))) > 0) {

System.out.println(new String(buffer,0,read));
fos.write(buffer, 0, read);
}

fos.close();
dis.close();
}
public static void main(String args[]) {
fileclient fc = new fileclient("localhost", 9886);
}
}

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

//UDP Client

import java.net.*;
public class client
{
public static void main(String[] args) throws Exception
{
byte[] buf = new byte[1024];
System.out.println("Receiver");
DatagramSocket ds = new DatagramSocket(3000);

while(true)
{
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String msg = new String(dp.getData(), 0, dp.getLength());
System.out.println(msg);
}
}
}
// UDP Server

import java.net.*;
import java.util.Scanner;
public class server
{
public static void main(String[] args) throws Exception
{
System.out.println("Sender");
DatagramSocket ds = new DatagramSocket();
DEPT OF ISE Page 24
Computer Network Laboratory 18CSL57

Scanner scanner = new Scanner(System.in);

System.out.println("\n Enter the Message : ");

while(true)
{
String msg = scanner.nextLine();
InetAddress ip = InetAddress.getByName("127.0.0.1");

DatagramPacket dp = new DatagramPacket(msg.getBytes(), msg.length(), ip, 3000);


ds.send(dp);
}
}
}

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

import java.util.*;
import java.math.BigInteger;
import java.lang.*;
class RSA
{
public static void main(String[] args)
{
Random rand1=new Random();
Random rand2=new Random();
Scanner si=new Scanner(System.in);

System.out.println("enter range for D value");


System.out.println();

int pubkey=si.nextInt();//nteger.parseInt(args[0]);
BigInteger bigB_p=BigInteger.probablePrime(32, rand1);
BigInteger bigB_q=BigInteger.probablePrime(32, rand2);
BigInteger bigB_n=bigB_p.multiply(bigB_q);
BigInteger bigB_p_1=bigB_p.subtract(new BigInteger("1"));
BigInteger bigB_q_1=bigB_q.subtract(new BigInteger("1"));
BigInteger bigB_p_1_q_1=bigB_p_1.multiply(bigB_q_1);
while(true)
{
BigInteger BigB_GCD=bigB_p_1_q_1.gcd(new BigInteger(""+pubkey));
if(BigB_GCD.equals(BigInteger.ONE))
{
break;
}
pubkey++;
}

BigInteger bigB_pubkey=new BigInteger(""+pubkey);


BigInteger bigB_prvkey=bigB_pubkey.modInverse(bigB_p_1_q_1);
System.out.println("public key : "+bigB_pubkey+","+bigB_n);
DEPT OF ISE Page 25
Computer Network Laboratory 18CSL57

System.out.println("private key : "+bigB_prvkey+","+bigB_n);


System.out.println("enter plain text ");
int asciiVal=si.nextInt();
BigInteger bigB_val=new BigInteger(""+asciiVal);

BigInteger bigB_cipherVal=bigB_val.modPow(bigB_pubkey,bigB_n);
System.out.println("Cipher text: " + bigB_cipherVal);
BigInteger bigB_plainVal=bigB_cipherVal.modPow(bigB_prvkey,bigB_n);
int plainVal=bigB_plainVal.intValue();
System.out.println("Plain text:" + plainVal);
}
}

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

import java.io.*; import java.util.*; class Queue


{
int q[],f=0,r=0,size; void insert(int n)
{
Scanner in = new Scanner(System.in); q=new int[10];
for(int i=0;i<n;i++)
{
System.out.print("\nEnter " + i + " element: "); int ele=in.nextInt();
if(r+1>10)
{
System.out.println("\nQueue is full \nLost Packet: "+ele); break;
}
else
{
}
}
}
r++;
q[i]=ele;
void delete()
{
Scanner in = new Scanner(System.in); Thread t=new Thread();
if(r==0)
System.out.print("\nQueue empty ");

DEPT OF ISE Page 26


Computer Network Laboratory 18CSL57

else
{
for(int i=f;i<r;i++)
{
try
{
t.sleep(1000);
}
q. insert(size); q.delete();
catch(Exception e){}
System.out.print("\nLeaked Packet: "+q[i]);
f++;
}
}
System.out.println();
}
}
class Leaky extends Thread
{
public static void main(String ar[]) throws Exception
{
Queue q=new Queue();
Scanner src=new Scanner(System.in);
System.out.println("\nEnter the packets to be sent:");
int size=src.nextInt();
}
}

DEPT OF ISE Page 27


Computer Network Laboratory 18CSL57

DEPT OF ISE Page 28


Computer Network Laboratory 18CSL57

DEPT OF ISE Page 29


Computer Network Laboratory 18CSL57

DEPT OF ISE Page 30


Computer Network Laboratory 18CSL57

DEPT OF ISE Page 31


Computer Network Laboratory 18CSL57

DEPT OF ISE Page 32


Computer Network Laboratory 18CSL57

DEPT OF ISE Page 33

You might also like