Professional Documents
Culture Documents
LAB MANUAL-2019-20
COMPUTER NETWORK LABORATORY-17CSL57
SEMESTER-05
CSE
Prepared By
SIJIN P
HoD, CSE, ICEAS
Approved By
Dr.Khaleel Ahmed
Principal, CSE, ICEAS
CSE Page 1
Computer Network Laboratory-17CSL57
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.
Design
Source code
//PROGRAM1
#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
CSE Page 2
Computer Network Laboratory-17CSL57
#===================================
#Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 0.2Mb 10ms DropTail
$ns queue-limit $n0 $n2 10
$ns duplex-link $n1 $n2 0.4Mb 15ms DropTail
$ns queue-limit $n1 $n2 10
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp0 $sink2
$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 6.0 "$ftp0 stop"
CSE Page 3
Computer Network Laboratory-17CSL57
$ns at 4.0 "$ftp1 start"
$ns at 9.0 "$ftp1 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr1n2.nam &
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
Result
//AWK FILE
//pr1n2.awk
BEGIN{
count=0;
}
{
if($1=="d")
CSE Page 4
Computer Network Laboratory-17CSL57
count++
}
END{
printf("The Total no of Packets Drop is :%d\n\n", count)
}
CSE Page 5
Computer Network Laboratory-17CSL57
Source code
//program2
#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
#===================================
#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
CSE Page 6
Computer Network Laboratory-17CSL57
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 $n4 1005.0Mb 1ms DropTail
$ns queue-limit $n0 $n4 5
$ns duplex-link $n1 $n4 50.0Mb 1ms DropTail
$ns queue-limit $n1 $n4 0
$ns duplex-link $n2 $n4 2000.0Mb 1ms DropTail
$ns queue-limit $n2 $n4 3
$ns duplex-link $n3 $n4 200.0Mb 1ms DropTail
$ns queue-limit $n3 $n4 0
$ns duplex-link $n5 $n4 1.0Mb 1ms DropTail
$ns queue-limit $n5 $n4 2
CSE Page 7
Computer Network Laboratory-17CSL57
$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 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 2.0 "finish"
$ns run
#===================================
# Agents Definition
#===================================
#===================================
# Applications Definition
#===================================
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr2n1.nam &
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"
CSE Page 8
Computer Network Laboratory-17CSL57
$ns run
Result
//AWK FILE
//pr2n1.awk
BEGIN { drop=0;}
{
if($1=="d" )
{
drop++;
}
}
END{
printf("Total number of %s packets dropped due to congestion
=%d\n",$5,drop);
}
CSE Page 9
Computer Network Laboratory-17CSL57
3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
Design
Source code
#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Nodes Definition
#===================================
#Create 6 nodes
CSE Page 10
Computer Network Laboratory-17CSL57
set n0 [$ns node]
$n0 label "src1"
$n0 color "red"
set n1 [$ns node]
set n2 [$ns node]
$n2 label "src2"
$n2 color "red"
set n3 [$ns node]
$n3 label "dest2"
$n3 color "blue"
set n4 [$ns node]
set n5 [$ns node]
$n5 label "dest1"
$n5 color "blue"
#===================================
# Links Definition
#===================================
#Createlinks between nodes
set lan [$ns newLan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/
DropTail Mac/802_3]
$ns duplex-link $n4 $n5 10.0Mb 1ms DropTail
$ns queue-limit $n4 $n5 50
#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set tcp1 [new Agent/TCP]
$ns attach-agent $n2 $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n5 $sink2
$ns connect $tcp0 $sink2
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp1 $sink3
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001
CSE Page 11
Computer Network Laboratory-17CSL57
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set packetSize_ 600
$ftp1 set interval_ 0.001
#===================================
# Termination
#===================================
set file1 [open file1.tr w]
$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp1 attach $file2
$tcp0 trace cwnd_ # must put underscore ( _ ) after cwnd and no space
between them
$tcp1 trace cwnd_
CSE Page 12
Computer Network Laboratory-17CSL57
Result
//AWK FILE
//pr3n1.awk
BEGIN{
}
{
if( $6=="cwnd_")
printf("%f\t%f\t\n",$1,$7);
}
END{
}
CSE Page 13
Computer Network Laboratory-17CSL57
CSE Page 14
Computer Network Laboratory-17CSL57
4. Implement simple ESS and with transmitting nodes in wire-less LAN by simulation and
determine the performance with respect to transmission of packets.
Design
Source code
#===================================
# 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) 3 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 478 ;# X dimension of topography
set val(y) 347 ;# Y dimension of topography
CSE Page 15
Computer Network Laboratory-17CSL57
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Mobile node parameter setup
#===================================
$ns node-config -adhocRouting DSDV\
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail\
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType $val(prop) \
-phyType $val(netif) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON \
#===================================
# Nodes Definition
#===================================
#Create 3 nodes
set n0 [$ns node]
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
CSE Page 16
Computer Network Laboratory-17CSL57
$n1 set X_ 400
$n1 set Y_ 50
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 200
$n2 set Y_ 30
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1 "$ftp0 start"
CSE Page 17
Computer Network Laboratory-17CSL57
$ns flush-trace
close $tracefile
close $namfile
exec nam l4.nam &
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 at 250 "finish"
$ns run
Result
CSE Page 18
Computer Network Laboratory-17CSL57
//AWK FILE
//p4.awk
BEGIN{
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: %f Mbps \n",
((count1*pack1*8)/(time1*1000000)));
printf("The Throughput from n1 to n2: %f Mbps \n",
((count2*pack2*8)/(time2*1000000)));
}
CSE Page 19
Computer Network Laboratory-17CSL57
5. Implement and study the performance of GSM on NS2/NS3 (Using MAC layer) or
equivalent environment.
Design
#===================================
# 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) 6 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 603 ;# X dimension of topography
set val(y) 405 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
CSE Page 20
Computer Network Laboratory-17CSL57
#===================================
# 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 6 nodes
set n0 [$ns node]
$n0 set X_ 178
$n0 set Y_ 234
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 296
$n1 set Y_ 237
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 390
$n2 set Y_ 237
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 503
$n3 set Y_ 239
CSE Page 21
Computer Network Laboratory-17CSL57
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 135
$n4 set Y_ 305
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 302
$n5 set Y_ 142
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
$ns at 1 " $n5 setdest 400 142 20 "
#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP]
$ns attach-agent $n4 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $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 10.0 "$ftp0 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr5n1.nam &
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
CSE Page 22
Computer Network Laboratory-17CSL57
Result
Program5,6:
//AWK FILE
//prog5.awk
BEGIN{
PacketRcvd=0;
Throughput=0.0;
}
{
if(($1=="r") && ($5=="tcp") && ($10=4.0))
{
PacketRcvd++;
}
}
END {
Throughput=((PacketRcvd*1000*8)/(95.0*1000000));
printf("packet received:%f\n", PacketRcvd);
printf( "the throughput is:%f\n",Throughput);
}
CSE Page 23
Computer Network Laboratory-17CSL57
6. Implement and study the performance of CDMA on NS2/NS3 (Using stack called Call
net) or equivalent environment.
Design
#===================================
# 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) 6 ;# number of mobilenodes
set val(rp) TORA ;# routing protocol CDMA
set val(x) 603 ;# X dimension of topography
set val(y) 405 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
CSE Page 24
Computer Network Laboratory-17CSL57
#Open the NS trace file
set tracefile [open pr5n1.tr w]
$ns trace-all $tracefile
#===================================
# 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 6 nodes
set n0 [$ns node]
$n0 set X_ 178
$n0 set Y_ 234
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 296
$n1 set Y_ 237
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 390
$n2 set Y_ 237
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 503
$n3 set Y_ 239
$n3 set Z_ 0.0
CSE Page 25
Computer Network Laboratory-17CSL57
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 135
$n4 set Y_ 305
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 302
$n5 set Y_ 142
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
$ns at 1 " $n5 setdest 400 142 20 "
#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP]
$ns attach-agent $n4 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $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 10.0 "$ftp0 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr5n1.nam &
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
CSE Page 26
Computer Network Laboratory-17CSL57
Result
CSE Page 27
Computer Network Laboratory-17CSL57
//prog5.tcl
# 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 prog5.tr w]
set nf [open prog5.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
CSE Page 28
Computer Network Laboratory-17CSL57
CSE Page 29
Computer Network Laboratory-17CSL57
CSE Page 30
Computer Network Laboratory-17CSL57
7. Write a program for error detecting code using CRC-CCITT (16- bits)
//Crc.java
import java.io.*;
class Crc
{
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());
tot_length=data_bits+divisor_bits;
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];
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]);
CSE Page 31
Computer Network Laboratory-17CSL57
}
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];
}
CSE Page 32
Computer Network Laboratory-17CSL57
Output:
C:\Program Files\Java\jdk1.7.0_80\bin>javac Crc.java
C:\Program Files\Java\jdk1.7.0_80\bin>java Crc
Enter number of data bits :
4
Enter data bits :
0
1
0
1
Enter number of bits in divisor :
4
Enter Divisor bits :
1
0
1
1
Dividend (after appending 0's) are : 01010000
CRC code :
01010011
Enter CRC code of 8 bits :
0
1
0
1
0
0
1
1
No Error
CSE Page 33
Computer Network Laboratory-17CSL57
8. Write a program to find the shortest path between vertices using bellman-ford
algorithm.
//Source code
import java.util.Scanner;
public class BellmanFord
{
int n;
int[][] a; int[] d;
int[] p;
int s;
public final static int INFTY=999;
BellmanFord(int n)
{
this.n = n;
a = new int[n][n];
d = new int[n];
p = new int[n];
}
void bellmanFord()
{
// Initialization
for(int i=0; i < n; i++)
{
d[i] = a[s][i];
p[i] = a[s][i] == INFTY ? -1 : s;
}
p[s] = -1;
for(int i=0; i < n-1; i++)
{
// Relax all edges iteratively (n-1) times
for(int u=0; u < n; u++)
{
for(int v=0; v < n; v++)
{
if(d[v] > d[u]+a[u][v])
{
d[v] = d[u]+a[u][v]; p[v] =
u;
CSE Page 34
Computer Network Laboratory-17CSL57
}
}
}
}
}
void input(Scanner scanner)
{
System.out.println("Enter the graph G: ");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
a[i][j] = scanner.nextInt();
if (i != j && a[i][j] == 0) a[i][j] = INFTY;
}
}
System.out.print("Enter the source vertex: ");
s =scanner.nextInt();
scanner.close();
}
void path(int v)
{
if (v == -1) return;
path(p[v]); System.out.print("."+v);
}
void output()
{
int i;
for(i=0; i < n; i++)
{
System.out.print("d(" + s + "," + i + ")=" + d[i]+" :p");
path(i);
System.out.println();
}
}
public static void main(String[] args)
{
int n;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of nodes: ");
CSE Page 35
Computer Network Laboratory-17CSL57
n = scanner.nextInt();
BellmanFord bf = new BellmanFord(n);
bf.input(scanner);
bf.bellmanFord();
bf.output();
}
}
//output
C:\Program Files\Java\jdk1.7.0_80\bin>javac BellmanFord.java
C:\Program Files\Java\jdk1.7.0_80\bin>java BellmanFord
Enter the number of nodes: 4
Enter the graph G:
0224
2030
2301
4010
Enter the source vertex: 3
d(3,0)=3 :p.3.2.0
d(3,1)=4 :p.3.2.1
d(3,2)=1 :p.3.2
d(3,3)=0 :p.3
CSE Page 36
Computer Network Laboratory-17CSL57
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.
//TcpClient.java
import java.io.*;
import java.net.*;
import java.util.*;
public class TcpClient
{
void client() throws Exception
{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
Scanner scanner = new Scanner(System.in);
System.out.print("Enter filename:");
String fileName = scanner.next();
dout.writeUTF(fileName);
String message;
do
{
message = din.readUTF();
System.out.println("Client: " +message);
}
while(!message.equals("eof"));
scanner.close();
dout.close();
s.close();
}
public static void main(String[] args) throws Exception
{
TcpClient tc = new TcpClient();
tc.client();
}
}
CSE Page 37
Computer Network Laboratory-17CSL57
//Tcpserver.java
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.nio.charset.*;
import java.util.*;
public class TcpServer
{
void server() throws Exception
{
ServerSocket ss=new ServerSocket(3333);
System.out.println("Server waiting for connection from client");
Socket cs=ss.accept();
DataInputStream din=new DataInputStream(cs.getInputStream());
DataOutputStream dout=new DataOutputStream(cs.getOutputStream());
String fileName = din.readUTF();
List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());
for (int i=0; i < lines.size(); i++)
{
System.out.println("server: "+lines.get(i));
dout.writeUTF(lines.get(i));
}
din.close();
cs.close();
ss.close();
}
public static void main(String[] args) throws Exception {
TcpServer ts = new TcpServer();
ts.server();
}
}
//testfile.txt
I am from ICEAS
I like network programs
eof
CSE Page 38
Computer Network Laboratory-17CSL57
//output
Open two command prompt or terminals(linux/ubuntu) and execute java files separately.
CSE Page 39
Computer Network Laboratory-17CSL57
10. Write a program on datagram socket for client/server to display the messages on client
side, typed at the server side.
//UdpClient.java
import java.net.*;
class UdpClient
{
public void client() throws Exception
{
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet;
String message = "Start";
packet = new DatagramPacket(message.getBytes(), message.length(),
InetAddress.getByName("127.0.0.1"),3333);
socket.send(packet);
System.out.println("Client: Sent data to Server ; Message = " + message);
byte[] msgBuffer = new byte[1024];
packet = new DatagramPacket(msgBuffer, msgBuffer.length);
do
{
socket.receive(packet);
message = new String(msgBuffer, 0, packet.getLength());
System.out.println(message);
}
while (!message.equals("stop"));
socket.close();
}
public static void main(String args[]) throws Exception
{
UdpClient uc = new UdpClient(); uc.client();
}
}
CSE Page 40
Computer Network Laboratory-17CSL57
//UdpServer.java
import java.net.*;
import java.util.Scanner;
class UdpServer
{
public void server() throws Exception
{
DatagramSocket socket = new DatagramSocket(3333);
DatagramPacket packet;
System.out.println("UDP Server Listening in " + 3333);
byte[] msgBuffer = new byte[1024];
packet = new DatagramPacket(msgBuffer, msgBuffer.length);
socket.receive(packet);
String message = new String(msgBuffer, 0, packet.getLength());
System.out.println("Client: Message received = " + message);
InetAddress address = packet.getAddress();
int port = packet.getPort();
Scanner scanner = new Scanner(System.in);
System.out.println("Server: type lines of text; type 'stop' to terminate");
do
{
message = scanner.nextLine();
packet = new DatagramPacket(message.getBytes(), message.length(), address, port);
socket.send(packet);
}
while (message.compareTo("stop") != 0);
scanner.close();
socket.close();
}
public static void main(String args[]) throws Exception
{
UdpServer us = new UdpServer(); us.server();
}
}
CSE Page 41
Computer Network Laboratory-17CSL57
//Output
C:\Program Files\Java\jdk1.7.0_80\bin>javac UdpServer.java
C:\Program Files\Java\jdk1.7.0_80\bin>java UdpServer
UDP Server Listening in 3333
Client: Message received = Start
Server: type lines of text; type 'stop' to terminate
hi
welcome to network programs
stop
CSE Page 42
Computer Network Laboratory-17CSL57
11. Write a program for simple RSA algorithm to encrypt and decrypt the data.
//RSA.java
import java.util.Scanner;
public class RSA
{
int d,e,n;
String message;
int gcd(int m, int n)
{
int rv = n == 0 ? m : gcd(n, m % n);
return rv;
}
int pow(int a, int m, int n)
{
int r = 1;
while (m-- != 0)
r = (r*a) % n;
return r ;
}
void rsa()
{
int p,q,z;
//odd prime numbers 3, 5 (3, 11?)
p = 11;
q = 13;
n = p*q;
z = (p-1)*(q-1);
// choose e
for (e=2; gcd(e, z) != 1; e++);
// choose d as inverse of e
for (d=2; (d*e) % z != 1; d++);
}
void input()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the message:");
message = scanner.next();
scanner.close();
}
void output()
{
System.out.println("S=(d,n)=(" + d + "," + n + ")");
System.out.println("P=(e,n)=(" + e + "," + n + ")");
System.out.println("Plain Text: " + message);
CSE Page 43
Computer Network Laboratory-17CSL57
char[] T = message.toCharArray();
for (int i=0; i < message.length(); i++)
{
T[i] = (char)pow((int)T[i], e, n);
}
System.out.println("Cipher Text: " + String.valueOf(T));
for (int i=0; i < message.length(); i++)
{
T[i] = (char)pow((int)T[i], d, n); //convert(a, d, n);
}
System.out.println("Plain Text: " + String.valueOf(T));
}
public static void main(String[] args)
{
RSA r = new RSA();
r.input();
r.rsa();
r.output();
}
}
//Output
C:\Program Files\Java\jdk1.7.0_80\bin>javac RSA.java
C:\Program Files\Java\jdk1.7.0_80\bin>java RSA
Enter the message:ICEAS
S=(d,n)=(103,143)
P=(e,n)=(7,143)
Plain Text: ICEAS
Cipher Text: SYlA
Plain Text: ICEAS
CSE Page 44
Computer Network Laboratory-17CSL57
12. Write a program for congestion control using leaky bucket algorithm.
//LeakyBucket.java
import java.util.*;
import java.util.Random;
public class LeakyBucket
{
int n;
int burst,outgoingRate,bucketSize,incoming,outgoing;
int pending,overflow,duration,interval;
LeakyBucket()
{
pending = 0;
incoming = 0;
overflow = 0;
outgoing = 0;
}
void leakyBucket()
{
System.out.println("Time Incoming Pending Overflow Outgoing");
Random rand = new Random();
int time=0;
while (time < duration)
{
incoming = rand.nextInt(burst);
if ((pending + incoming) > bucketSize)
{
overflow = (pending + incoming) - bucketSize;
pending = bucketSize;
}
else pending += incoming;
//interval = rand.nextInt(); // Next packet will come at time interval = 1;
for(int clk = 0; clk < 1; ++clk)
{
output(time, incoming, pending, overflow, outgoing);
outgoing = Math.min(outgoingRate, pending);
pending -= outgoing;
incoming = 0;
++time;
}
CSE Page 45
Computer Network Laboratory-17CSL57
}
}
void input()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter burst size: ");
burst = scanner.nextInt();
System.out.println("Enter bucket size: ");
bucketSize = scanner.nextInt();
System.out.println("Enter outgoing rate: ");
outgoingRate = scanner.nextInt();
System.out.println("Enter duration: ");
duration = scanner.nextInt();
scanner.close();
}
void output(int time, int incoming, int pending, int overflow, int outgoing)
{
System.out.printf("%d\t%d\t%d\t%d\t%d\n",time,incoming,pending,overflow,outgoing);
}
public static void main(String[] args)
{
LeakyBucket lb = new LeakyBucket();
lb.input();
lb.leakyBucket();
}
}
CSE Page 46
Computer Network Laboratory-17CSL57
//output
C:\Program Files\Java\jdk1.7.0_80\bin>java LeakyBucket
Enter burst size:
6
Enter bucket size:
5
Enter outgoing rate:
2
Enter duration:
6
Time Incoming Pending Overflow Outgoing
0 4 4 0 0
1 3 5 0 2
2 1 4 0 2
3 4 5 1 2
4 3 5 1 2
5 5 5 3 2
CSE Page 47