2023-24
Computer Networks Lab Manual
Lab Manual 21CS52
V SEMESTER VTU
LIST OF EXPERIMENTS
COMPUTER NETWORKS
Course Code: 21CS52 CIE Marks 50
Teaching Hours/Week (L:T:P:S) 3:0:2:0 SEE Marks
50
Total Hours of Pedagogy 40T + 20P Total Marks
100
Credits 04 Exam Hours 03
1. Implement Three nodes point – to – point network with
duplex links between them for different topologies. Set the
queue size, vary the bandwidth, and find the number of
packets dropped for various iterations.
2. Implement simple ESS and with transmitting nodes in wire-
less LAN by simulation and determine the throughput with
respect to transmission of packets
3. Write a program for error detecting code using CRC-CCITT
(16- bits).
4. 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 in the network.
5. Write a program to find the shortest path between vertices
using bellman-ford algorithm.
6. Implement an Ethernet LAN using n nodes and set multiple
traffic nodes and plot congestion window for different source
/ destination.
7. Write a program for congestion control using leaky bucket
algorithm
Course Coordinators
1 Implement Three nodes point – to – point network with duplex links between
them for different topologies. Set the queue size, vary the bandwidth, and
find the number of packets dropped for various iterations.
2 #Create Simulator
3 set ns [new Simulator]
4
5 #Open Trace file and NAM file set ntrace [open prog1.tr w]
6 $ns trace-all $ntrace
7 set namfile [open prog1.nam w]
8 $ns namtrace-all $namfile
9
10 #Finish Procedure proc Finish {} {
11 global ns ntrace namfile
12
13 #Dump all the trace data and close the files
14 $ns flush-trace close $ntrace close $namfile
15
16 #Execute the nam animation file exec nam prog1.nam &
17
18 #Show the number of packets dropped
19 exec echo "The number of packet drops is " & exec grep -c
"^d" prog1.tr &
20 exit 0
21 }
22
23 #Create 3 nodes set n0 [$ns node] set n1 [$ns node] set n2
[$ns node]
24
25 #Label the nodes
26 $n0 label "TCP Source"
27 $n2 label "Sink"
28
29 #Set the color
30
31 $ns color 1 blue
32
33
34 #Create Links between nodes
35 #You need to modify the bandwidth to observe the variation
in packet drop
36 $ns duplex-link $n0 $n1 1Mb 10ms DropTail
37 $ns duplex-link $n1 $n2 1Mb 10ms DropTail
38
39 #Make the Link Orientation
40 $ns duplex-link-op $n0 $n1 orient right
41 $ns duplex-link-op $n1 $n2 orient right
42
43 #Set Queue Size
44 #You can modify the queue length as well to observe the
variation in packet drop
45 $ns queue-limit $n0 $n1 10
46 $ns queue-limit $n1 $n2 10
47
48 #Set up a Transport layer connection. set tcp0 [new
Agent/TCP]
49 $ns attach-agent $n0 $tcp0
50 set sink0 [new Agent/TCPSink]
51 $ns attach-agent $n2 $sink0
52 $ns connect $tcp0 $sink0
53
54 #Set up an Application layer Traffic
55 set cbr0 [new Application/Traffic/CBR]
56 $cbr0 set type_ CBR
57 $cbr0 set packetSize_ 100
58 $cbr0 set rate_ 1Mb
59 $cbr0 set random_ false
60 $cbr0 attach-agent $tcp0
61
62 $tcp0 set class_ 1
63
64 #Schedule Events
65 $ns at 0.0 "$cbr0 start"
66 $ns at 5.0 "Finish"
67
68 #Run the Simulation
69 $ns run
2. Implement simple ESS and with transmitting nodes in wire-less LAN by
simulation and determine the throughput with respect to transmission of
packets
Program2.tcl
#Create a ns simulator
set ns [new Simulator]
#Setup topography object
set topo [new Topography]
$topo load_flatgrid 1500 1500
#Open the NS trace file
set tracefile [open p4.tr w]
$ns trace-all $tracefile
#Open the NAM trace file
set namfile [open p4.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile 1500 1500
#===================================
#Mobile node parameter setup
#===================================
$ns node-config -adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 20 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON
#===================================
#Nodes Definition
#===================================
create-god 6
#Create 6 nodes
set n0 [$ns node]
$n0 set X_ 630
$n0 set Y_ 501
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 454
$n1 set Y_ 340
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 785
$n2 set Y_ 326
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 270
$n3 set Y_ 190
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 539
$n4 set Y_ 131
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 964
$n5 set Y_ 177
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
#===================================
#Agents Definition
#===================================
#Setup a UDP connection
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set null1 [new Agent/Null]
$ns attach-agent $n4 $null1
$ns connect $udp0 $null1
$udp0 set packetSize_ 1500
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n3 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $sink1
$ns connect $tcp0 $sink1
#===================================
#Applications Definition
#===================================
#Setup a CBR Application over UDP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set rate_ 1.0Mb
$cbr0 set random_ null
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
#===================================
#Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam p4.nam &
exec echo "Number of packets dropped is:" &
exec grep -c "^D" p4.tr &
exit 0
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$ftp0 start"
$ns at 180.0 "$ftp0 stop"
$ns at 200.0 "$cbr0 stop"
$ns at 200.0 "finish"
$ns at 70 "$n4 set dest 100 60 20"
$ns at 100 "$n4 set dest 700 300 20"
$ns at 150 "$n4 set dest 900 200 20"
$ns run
#===================================
Program 2.awk
#CODE TO BE SAVED AS .awk FILE
#===================================
BEGIN{
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1=="r"&&$3=="_1_"&&$4=="RTR")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1=="r"&&$3=="_2_"&&$4=="RTR")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %fMbps\n",
((count1*pack1*8)/(time1*1000000)));
printf("The Throughput from n1 to n2: %fMbps\n",
((count2*pack2*8)/(time2*1000000)));
}
3. Write a program for error detecting code using CRC-CCITT (16- bits).
import java.io.*;
import java.util.*;
class CRC
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int[ ] data;
int[ ] msg;
int[ ] generator;
int[ ] rem;
int[ ] crc;
int data_bits, generator_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits= s.nextInt();
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=s.nextInt();
System.out.println("Enter number of bits in generator : ");
generator_bits=s.nextInt();
generator=new int[generator_bits];
System.out.println("Enter generator bits : ");
for(int i=0; i<generator_bits; i++)
generator[i]=s.nextInt();
tot_length=data_bits+generator_bits-1;
msg=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/*------------------ CRC GENERATION-----------------------*/
for(int i=0;i<data.length;i++)
msg[i]=data[i];
System.out.print("Message after appending 0's are : ");
for(int i=0; i< msg.length; i++)
System.out.print(msg[i]);
System.out.println();
for(int j=0; j<msg.length; j++){
rem[j] = msg[j];
}
rem=xor(generator, rem);
for(int i=0;i<msg.length;i++)
//append message and ramainder
{
crc[i]=(msg[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++)
rem[i]=s.nextInt();
rem=xor(generator, 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");
}
System.out.println("THANK YOU.... :)");
}
static int[] xor(int generator[], int rem[])
{
int cur=0;
while(true)
{
for(int i=0;i<generator.length;i++)
rem[cur+i]=(rem[cur+i]^generator[i]);
while(rem[cur]==0 && cur!=rem.length-1)
cur++;
if((rem.length-cur)<generator.length)
break;
}
return rem;
}
}
4 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 in the network.
#Create Simulator
set ns [new Simulator]
#Use colors to differentiate the traffic
$ns color 1 Blue
$ns color 2 Red
#Open trace and NAM trace file set ntrace [open prog3.tr w]
$ns trace-all $ntrace
set namfile [open prog3.nam w]
$ns namtrace-all $namfile
#Finish Procedure proc Finish {} {
global ns ntrace namfile
#Dump all trace data and close the file
$ns flush-trace close $ntrace close $namfile
#Execute the nam animation file exec nam prog3.nam &
#Find the number of ping packets dropped
puts "The number of ping packets dropped are "
exec grep "^d" prog3.tr | cut -d " " -f 5 | grep -c "ping" &
exit 0
}
#Create six nodes
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}
#Connect the nodes
for {set j 0} {$j < 5} {incr j} {
$ns duplex-link $n($j) $n([expr ($j+1)]) 0.1Mb 10ms DropTail
}
#Define the recv function for the class 'Agent/Ping'
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from $from with
round trip time $rtt
ms"
}
#Create two ping agents and attach them to n(0) and n(5)
set p0 [new Agent/Ping]
$p0 set class_ 1
$ns attach-agent $n(0) $p0
set p1 [new Agent/Ping]
$p1 set class_ 1
$ns attach-agent $n(5) $p1
$ns connect $p0 $p1
#Set queue size and monitor the queue
#Queue size is set to 2 to observe the drop in ping packets
$ns queue-limit $n(2) $n(3) 2
$ns duplex-link-op $n(2) $n(3) queuePos 0.5
#Create Congestion
#Generate a Huge CBR traffic between n(2) and n(4)
set tcp0 [new Agent/TCP]
$tcp0 set class_ 2
$ns attach-agent $n(2) $tcp0 set sink0 [new Agent/TCPSink]
$ns attach-agent $n(4) $sink0
$ns connect $tcp0 $sink0
#Apply CBR traffic over TCP
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set rate_ 1Mb
$cbr0 attach-agent $tcp0
#Schedule events
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.4 "$cbr0 start"
$ns at 0.8 "$p0 send"
$ns at 1.0 "$p1 send"
$ns at 1.2 "$cbr0 stop"
$ns at 1.4 "$p0 send"
$ns at 1.6 "$p1 send"
$ns at 1.8 "Finish"
#Run the Simulation
$ns run
5 Write a program to find the shortest path between vertices using bellman-
ford algorithm.
import java.util.Scanner;
public class BF {
public static final int MAX_VALUE = 999;
public void BellmanFordEvaluation(int source, int A[][], int num_ver) {
int D[],node,vertex,sn,dn;
D = new int[num_ver];
for (node = 0; node < num_ver; node++) {
D[node] = MAX_VALUE;
}
D[source] = 0;
for (node = 0; node < num_ver; node++) {
for (sn = 0; sn < num_ver; sn++) {
for (dn = 0; 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 (sn = 0; sn < num_ver; sn++) {
for (dn = 0; dn < num_ver; dn++) {
if (A[sn][dn] != MAX_VALUE) {
if (D[dn] > D[sn] + A[sn][dn])
System.out.println("The Graph contains negative egde cycle");
}
}
}
for (vertex = 0; vertex < num_ver; vertex++) {
System.out.println("distance of source " + source + " to " + vertex + " is
" + D[vertex]);
}
}
public static void main(String[] args) {
int num_ver = 0;
int source,sn,dn;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
num_ver = scanner.nextInt();
int A[][] = new int[num_ver][num_ver];
System.out.println("Enter the adjacency matrix");
for (sn = 0; sn <num_ver; sn++) {
for (dn = 0; dn <num_ver; dn++) {
A[sn][dn] = scanner.nextInt();
if (sn == dn) {
A[sn][dn] = 0; // distance from to that perticular node is 0
continue;
}
if (A[sn][dn] == 0) {
A[sn][dn] = MAX_VALUE;
}
}
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
BF b = new BF();
b.BellmanFordEvaluation(source, A, num_ver);
scanner.close();
}
}
6. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and
plot congestion window for different source / destination.
#Create Simulator
set ns [new Simulator]
#Use colors to differentiate the traffics
$ns color 1 Blue
$ns color 2 Red
#Open trace and NAM trace file set ntrace [open prog5.tr w]
$ns trace-all $ntrace
set namfile [open prog5.nam w]
$ns namtrace-all $namfile
#Use some flat file to create congestion graph windows set
winFile0 [open WinFile0 w]
set winFile1 [open WinFile1 w]
#Finish Procedure proc Finish {} {
#Dump all trace data and Close the files global ns ntrace
namfile
$ns flush-trace close $ntrace close $namfile
#Execute the NAM animation file exec nam prog5.nam &
#Plot the Congestion Window graph using xgraph exec xgraph
WinFile0 WinFile1 &
exit 0
}
#Plot Window Procedure
proc PlotWindow {tcpSource file} { global ns
set time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_] puts $file "$now $cwnd"
$ns at [expr $now+$time] "PlotWindow $tcpSource $file"
}
#Create 6 nodes
for {set i 0} {$i<6} {incr i} { set n($i) [$ns node]
}
#Create duplex links between the nodes
$ns duplex-link $n(0) $n(2) 2Mb 10ms DropTail
$ns duplex-link $n(1) $n(2) 2Mb 10ms DropTail
$ns duplex-link $n(2) $n(3) 0.6Mb 100ms DropTail
#Nodes n(3) , n(4) and n(5) are considered in a LAN
set lan [$ns newLan "$n(3) $n(4) $n(5)" 0.5Mb 40ms LL
Queue/DropTail MAC/802_3 Channel]
#Orientation to the nodes
$ns duplex-link-op $n(0) $n(2) orient right-down
$ns duplex-link-op $n(1) $n(2) orient right-up
$ns duplex-link-op $n(2) $n(3) orient right
#Setup queue between n(2) and n(3) and monitor the queue
$ns queue-limit $n(2) $n(3) 20
$ns duplex-link-op $n(2) $n(3) queuePos 0.5
#Set error model on link n(2) to n(3) set loss_module [new
ErrorModel]
$loss_module ranvar [new RandomVariable/Uniform]
$loss_module drop-target [new Agent/Null]
$ns lossmodel $loss_module $n(2) $n(3)
#Set up the TCP connection between n(0) and n(4) set tcp0 [new
Agent/TCP/Newreno]
$tcp0 set fid_ 1
$tcp0 set window_ 8000
$tcp0 set packetSize_ 552
$ns attach-agent $n(0) $tcp0
set sink0 [new Agent/TCPSink/DelAck]
$ns attach-agent $n(4) $sink0
$ns connect $tcp0 $sink0
#Apply FTP Application over TCP set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set type_ FTP
#Set up another TCP connection between n(5) and n(1) set tcp1
[new Agent/TCP/Newreno]
$tcp1 set fid_ 2
$tcp1 set window_ 8000
$tcp1 set packetSize_ 552
$ns attach-agent $n(5) $tcp1
set sink1 [new Agent/TCPSink/DelAck]
$ns attach-agent $n(1) $sink1
$ns connect $tcp1 $sink1
#Apply FTP application over TCP set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP
#Schedule Events
$ns at 0.1 "$ftp0 start"
$ns at 0.1 "PlotWindow $tcp0 $winFile0"
$ns at 0.5 "$ftp1 start"
$ns at 0.5 "PlotWindow $tcp1 $winFile1"
$ns at 25.0 "$ftp0 stop"
$ns at 25.1 "$ftp1 stop"
$ns at 25.2 "Finish"
#Run the simulation
$ns run
7 Write a program for congestion control using leaky bucket algorithm.
import java.util.*;
public class Leakybucket
{
public static void main(String[] args)
{
Scanner my = new Scanner(System.in);
int no_groups,bucket_size;
System.out.print("\n Enter the bucket size : \t");
bucket_size = my.nextInt();
System.out.print("\n Enter the no of groups : \t");
no_groups = my.nextInt();
int no_packets[] = new int[no_groups];
int in_bw[] = new int[no_groups];
int out_bw,reqd_bw=0,tot_packets=0;
for(int i=0;i<no_groups;i++)
{
System.out.print("\n Enter the no of packets for group " + (i+1) +
"\t");
no_packets[i] = my.nextInt();
System.out.print("\n Enter the input bandwidth for the group " +
(i+1) + "\t");
in_bw[i] = my.nextInt();
if((tot_packets+no_packets[i])<=bucket_size)
{
tot_packets += no_packets[i];
}
else
{
do
{
System.out.println(" Bucket Overflow ");
System.out.println(" Enter value less than " + (bucket_size-
tot_packets));
no_packets[i] = my.nextInt();
}while((tot_packets+no_packets[i])>bucket_size);
tot_packets += no_packets[i];
}
reqd_bw += (no_packets[i]*in_bw[i]);
} System.out.println("\nThe total required bandwidth is " +
reqd_bw);
System.out.println("Enter the output bandwidth ");
out_bw = my.nextInt();
while((out_bw<=reqd_bw)&&(tot_packets>0))
{
System.out.println("Data Sent \n" + (--tot_packets) + " packets
remaining");
System.out.println("Remaining Bandwidth " + (reqd_bw -= out_bw));
if((out_bw>reqd_bw)&&(tot_packets>0))
System.out.println(tot_packets + " packet(s) discarded due to
insufficient bandwidth");
}
}
}