0% found this document useful (0 votes)
19 views25 pages

21CS52 CN Lab Manual

The document outlines a series of laboratory experiments for a Computer Networks course, detailing various programming tasks to simulate network scenarios using TCL scripts. Each experiment focuses on different aspects of networking, such as packet transmission, congestion control, and performance evaluation in both wired and wireless networks. The document includes specific commands for executing the simulations and analyzing the results using AWK scripts.

Uploaded by

debugthebug336
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)
19 views25 pages

21CS52 CN Lab Manual

The document outlines a series of laboratory experiments for a Computer Networks course, detailing various programming tasks to simulate network scenarios using TCL scripts. Each experiment focuses on different aspects of networking, such as packet transmission, congestion control, and performance evaluation in both wired and wireless networks. The document includes specific commands for executing the simulations and analyzing the results using AWK scripts.

Uploaded by

debugthebug336
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/ 25

GOVERNMENT S K S J TECHNOLOGICAL INSITITUTE

[AFFILIATED TO VISVESWARAIAH TECHNOLOGICAL UNIVERSITY]

K R CIRCLE.BENGALURU 560001

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING
V Semester

COMPUTER NETWORK LABORATORY


Subject Code: 21CS52

FACULTY INCHARGE:
Dr. SOMESHA M
ASSISTANT PROFESSOR
DEPT OF CSE
PROGRAM LIST

Expt. Program Name


No.
Implement three nodes point – to – point network with duplexlinks between them. Set the queue
1 size, vary the bandwidth and find the number of packets dropped.

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

Implement transmission of ping messages/trace route over anetwork topology consisting of 6


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

Implement an Ethernet LAN using n nodes and set multipletraffic nodes and plot
4 congestion window for different source / destination.

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

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

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


Program 1: Implement three nodes point – to – point network with duplex links between them. Set
the queue size, vary the bandwidth and find the numberof packets dropped.

lab1.tcl
set ns [new Simulator]

set tf [open lab1.tr w]


$ns trace-all $tf

set nf [open lab1.nam w]


$ns namtrace-all $nf

set n0 [$ns node] set n1


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

$ns color 1 "red"


$ns color 2 "blue"

$n0 label "Source/udp0"


$n1 label "Source/udp1"
$n2 label "Router"
$n3 label "Destination/Null"

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


$ns duplex-link $n1 $n2 10Mb 300ms DropTail
$ns duplex-link $n2 $n3 1Mb 300ms DropTail
$ns set queue-limit $n0 $n2 10

$ns set queue-limit $n1 $n2 10


$ns set queue-limit $n2 $n3 5

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 $udp
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1

$udp0 set class_ 1


$udp1 set class_ 2

$ns connect $udp0 $null3


$ns connect $udp1 $null3

$cbr1 set packetSize_ 500Mb


$cbr1 set interval_ 0.005

proc finish {} {global ns


nf tf
$ns flush-trace exec nam
lab1.nam &close $tf
close $nfexit 0
}

$ns at 0.1 "$cbr0 start"


$ns at 0.1 "$cbr1 start"
$ns at 10.0 "finish"
$ns run

lab1.awk

BEGIN {
c=0;
}
{
if($1=="d")
{ c++;
}
}
END{
printf("the number of packets dropped=%d\n",c);
}

Commands:
gedit lab1.tcl (to open the tcl program file, type the program andsave.)

gedit lab1.awk (to open the awk program file, type the program andsave)

ns lab1.tcl (to execute and simulate the tcl program)


awk –f lab1.awk lab1.tr (to view the result)
Output Topology:

Trace file:
Program 2: Implement simple ESS and with transmitting nodes in wire-less LAN by
simulation and determine the performance with respect to transmission ofpackets.

lab2.tcl

set ns [new Simulator]

set tf [open lab2.tr w]


$ns trace-all $tf

set topo [new Topography]


$topo load_flatgrid 1000 1000

set nf [open lab2.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
$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
lab2.nam &close $tf
exit 0
}

$ns at 250 "finish"


$ns run

lab2.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)));
}

Commands:

gedit lab2.tcl (to open the tcl program file, type the program andsave.)

gedit lab2.awk (to open the awk program file, type the program andsave)

ns lab2.tcl (to execute and simulate the tcl program)awk –f lab2.awk

lab2.tr (to view the output)

Topology:
Trace file:

Here “M” indicates mobile nodes, “AGT” indicates Agent Trace, “RTR”
indicates Router Trace.

Output:
Program 3: 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.

lab3.tcl
set ns [ new Simulator ]

set nf [ open lab3.nam w ]


$ns namtrace-all $nf

set tf [ open lab3.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]

$ns color 1 "red"


$ns color 2 "blue"
$n4 shape box

$ns duplex-link $n0 $n4 1000Mb 1ms DropTail


$ns duplex-link $n1 $n4 1000Mb 1ms DropTail
$ns duplex-link $n2 $n4 1000Mb 1ms DropTail
$ns duplex-link $n3 $n4 1Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set p0 [new Agent/Ping]


$ns attach-agent $n0 $p0
$p0 set packetSize_ 50000
$p0 set interval_ 0.001

set p1 [new Agent/Ping]


$ns attach-agent $n1 $p1

set p2 [new Agent/Ping]


$ns attach-agent $n2 $p2
$p2 set packetSize_ 50000
$p2 set interval_ 0.001

set p3 [new Agent/Ping]


$ns attach-agent $n3 $p3

set p5 [new Agent/Ping]


$ns attach-agent $n5 $p5
$ns queue-limit $n0 $n4 5
$ns queue-limit $n2 $n4 5
$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"
}

$p0 set class_ 1


$p2 set class_ 2

$ns connect $p0 $p5


$ns connect $p2 $p3

proc finish {} {global ns


nf tf
$ns flush-traceclose $nf
close $tf
exec nam lab3.nam &exit 0
}

$ns at 0.1 "$p0 send"


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

lab3.awk

BEGIN {
c=0;
}
{
if($1=="d")
{ c++;
}
}
END{
printf("the number of packets dropped=%d\n",c)
}

Commands:

gedit lab3.tcl (to open the tcl program file, type the program andsave.)

gedit lab3.awk (to open the awk program file, type the program and save)ns lab3.tcl (to execute and

simulate the tcl program)

awk –f lab3.awk lab3.tr (to view the result)

Output Topology

Output:
Program 4: Implement an Ethernet LAN using n nodes and set multiple trafficnodes and
plot congestion window for different source/destination.

lab4.tcl

set ns [new Simulator]

set tf [open lab4.tr w]


$ns trace-all $tf

set nf [open lab4.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]

$n0 color "magenta"


$n0 label "src1"
$n2 color "magenta"
$n2 label "src2"
$n3 color "blue"
$n3 label "dest2"
$n5 color "blue"
$n5 label "dest1"

$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTailMac/802_3

$ns duplex-link $n4 $n5 1Mb 1ms DropTailset 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_


$tcp2 trace cwnd_

proc finish { } {global ns


nf tf
$ns flush-traceclose $tf
close $nf
exec nam lab4.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

lab4.awk

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

gedit lab4.tcl (to open the tcl program file, type the program andsave.)

gedit lab4.awk (to open the awk program file, type the program andsave)

ns lab4.tcl (to execute and simulate the tcl program)

After simulation is completed run awk file to see the output ,

awk –f lab4.awk file1.tr > a1 awk –f lab4.awk


file2.tr > a2xgraph a1 a2

Output Topology:
Output:
Program 5: 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-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];
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());


"); 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");
}
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) Without Error

2) With Error
Program 6: Program to find the shortest path between vertices usingbellman-ford algorithm.

BellmanFord.java

import java.util.Scanner;
public class BellmanFord
{
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;
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]) System.out.println("The Graph contains

negative egde cycle");


}
}
}
for (int vertex = 1; 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;
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();
}
}

Output:
Program 7: Write a Java Program Congestion control using Leaky BucketAlgorithm.

Leaky.java

import java.util.*;
public class Leaky
{
static int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
public static void main(String[] args)
{
int drop=0,mini,nsec,cap,count=0,i,process;
int inp[]=new int[25];
Scanner sc=new Scanner(System.in); System.out.print("Enter The
Bucket Size : ");cap= sc.nextInt();
System.out.print("Enter The Operation Rate : ");
process= sc.nextInt();
System.out.print("Enter the no. of seconds you want tosimulate : ");
nsec=sc.nextInt();
for(i=0;i<nsec;i++)
{
System.out.print("Enter the size of the packet enteringat "+ i+1 + " sec ");
inp[i] = sc.nextInt();
}
System.out.println("\nSecond | Packet Received | Packet Sent |Packet Left | Packet
Dropped|\n");
for(i=0;i<nsec;i++)
{
count+=inp[i];
if(count>cap)
{
drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t"+inp[i]);
mini=min(count,process);
System.out.print("\t\t"+mini); count=count-
mini; System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);drop=0;
System.out.println();
}
for(;count!=0;i++)
{
if(count>cap)
{
drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t0");
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
System.out.println();
}
}
}

Output:

You might also like