CN Lab 18112022
CN Lab 18112022
Experiment-1
Aim: Study of Network Devices in Detail and Connect the computers in Local
Area Network
1. Repeater: Functioning at Physical Layer.Arepeater is an electronic device that receives a signal and
retransmits it at a higher level and/or higher power, or onto the other side of an obstruction, so that the
signal can cover longer distances. Repeater have two ports ,so cannot be use to connect for more than
two devices
2. Hub: An Ethernet hub, active hub, network hub, repeater hub, hub or concentrator is a device for
connecting multiple twisted pair or fiber optic Ethernet devices together and making them act as a
single network segment. Hubs work at the physical layer (layer 1) of the OSI model. The device is a
form of multiport repeater. Repeater hubs also participate in collision detection, forwarding a jam
signal to all ports if it detects a collision.
3. Switch :Anetwork switch or switching hub is a computer networking device that connects network
segments.The term commonly refers to a network bridge that processes and routes data at the data link
layer (layer 2) of the OSI model. Switches that additionally process data at the network layer (layer 3
and above) are often referred to as Layer 3 switches or multilayer switches.
4. Bridge: A network bridge connects multiple network segments at the data link layer (Layer 2) of
the OSI model. In Ethernet networks, the term bridge formally means a device that behaves according
to the IEEE 802.1D standard. A bridge and switch are very much alike; a switch being a bridge with
numerous ports. Switch or Layer 2 switch is often used interchangeably with bridge .Bridges can
analyze incoming data packets to determine if the bridge is able to send the given packet to another
segment of the network.
5. Router: A router is an electronic device that interconnects two or more computer networks, and
selectively interchanges packets of data between them. Each data packet contains address information
that a router can use to determine if the source and destination are on the same network, or if the data
packet must be transferred from one network to
6. Gate Way: In a communications network, a network node equipped for interfacing with another
network that uses different protocols.
• A gateway may contain devices such as protocol translators, impedance matching devices, rate
converters, fault isolators, or signal translators as necessary to provide system interoperability. It also
requires the establishment of mutually acceptable administrative procedures between both networks.
• A protocol translation/mapping gateway interconnects networks with different network protocol
technologies by performing the required protocol conversions
2
Computer Networks Lab CSE Department BVCR
EXPERIMENTNO:2
NAME OF THE EXPERIMENT:
Aim: Write a C/C++ Program to implement the data link layer farming methods such as
Program:
#include<stdio.h>
#include<string.h>
main()
{
char a[30], fs[50] = " ", t[3], sd, ed, x[3], s[3], d[3], y[3];
int i, j, p = 0, q = 0;
clrscr();
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter a character that represents ending delimiter:");
scanf(" %c", &ed);
x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0';
y[0] = d[0] = d[1] = ed;
d[2] = y[1] = '\0';
strcat(fs, x);
for(i = 0; i < strlen(a); i++)
{
t[0] = a[i];
t[1] = '\0';
if(t[0] == sd)
strcat(fs, s);
else if(t[0] == ed)
strcat(fs, d);
else
strcat(fs, t);
}
strcat(fs, y);
3
Computer Networks Lab CSE Department BVCR
getch();
OUTPUT:
Enter characters to be stuffed: goodday
4
Computer Networks Lab CSE Department BVCR
Program:
/*Program to implement bit Stuffing*/
#include<stdio.h>
#include<string.h>
int main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
getch();
OUTPUT:
Enter frame size (Example: 8):12
6
Computer Networks Lab CSE Department BVCR
EXPERIMENTNO:3
Aim:Write a C/C++ Program to implement data link layer farming method checksum.
Program:
#include<stdio.h>
#include<math.h>
int sender(int arr[10],int n)
{
int checksum,sum=0,i;
printf("\n****SENDER SIDE****\n");
for(i=0;i<n;i++)
sum+=arr[i];
printf("SUM IS: %d",sum);
checksum=~sum;
printf("\nCHECKSUM IS:%d",checksum);
return checksum;
}
void receiver(int arr[10],int n,int sch)
{
int checksum,sum=0,i;
printf("\n\n****RECEIVER SIDE****\n");
for(i=0;i<n;i++)
sum+=arr[i];
printf("SUM IS:%d",sum);
sum=sum+sch;
checksum=~sum;
//1's complement of sum
printf("\nCHECKSUM IS:%d",checksum);
}
int main()
{
int n,sch,rch;
printf("\nENTER SIZE OF THE STRING:");
scanf("%d",&n);
int arr[n];
printf("ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE
CHECKSUM:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sch=sender(arr,n);
receiver(arr,n,sch);
}
7
Computer Networks Lab CSE Department BVCR
OUTPUT:
ENTER SIZE OF THE STRING:2
ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:
01010101
10101010
****SENDER SIDE****
SUM IS: 11111111
CHECKSUM IS:-11111112
****RECEIVER SIDE****
SUM IS:11111111
CHECKSUM IS:0
8
Computer Networks Lab CSE Department BVCR
EXPERIMENTNO:4
Aim: Write a C/C++ Program Hamming Code generation for error detection and correction.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
char data[5];
int encoded[8], edata[7], syndrome[3];
int hmatrix[3][7]= { 1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1};
char gmatrix[4][8]={ "0111000", "1010100", "1100010",
"1110001"};
void main()
{
int i,j;
clrscr();
cout<<"Hamming Code --- Encoding\n";
cout<<"Enter 4 bit data : ";
cin>>data;
cout<<"Generator Matrix\n";
for(i=0;i<4;i++) cout<<"\t"<<gmatrix[i]<<"\n";
cout<<"Encoded Data : ";
for(i=0;i<7;i++)
{
for(j=0;j<4;j++)
encoded[i]+=((data[j]- '0')*(gmatrix[j][i]- '0'));
encoded[i]=encoded[i]%2;
cout<<encoded[i]<<" ";
9
Computer Networks Lab CSE Department BVCR
for(j=0;j<7;j++)
if
((syndrome[0]==hmatrix[0][j])&&(syndrome[1]==hmatrix[1][j])&&
(syndrome[2]==hmatrix[2][j]))
break;
if(j==7)
else
{
edata[j]=!edata[j];
OUTPUT
Generator Matrix
0111000
1010100
1100010
1110001
10
Computer Networks Lab CSE Department BVCR
Encoded Data : 1 0 1 1 0 1 0
Generator Matrix
0111000
1010100
1100010
1110001
Encoded Data : 1 0 1 1 0 1 0
EXPERIMENTNO:5
11
Computer Networks Lab CSE Department BVCR
Aim:
Write a C/C++ Program to implement on a data set of characters the three CRC polynomials – CRC
12, CRC
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,msglen,genlen;
char msg[100],gen[30],gen1[30],temp[30],quot[100],
rem[30];
clrscr();
printf("enter the message string ");
gets(msg);
printf("Enter the generator string ");
gets(gen);
msglen=strlen(msg);
genlen=strlen(gen);
strcpy(gen1,gen);
for(i=0;i<genlen-1;i++)
{
msg[msglen+i]='0';
}
for(i=0;i<genlen;i++)
temp[i]=msg[i];
for(i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for(j=0;j<genlen;j++)
gen[j]='0';
else
for(j=0;j<genlen;j++)
gen[j]=gen1[j];
for(j=genlen-1;j>0;j--)
{
if(temp[j]==gen[j])
12
Computer Networks Lab CSE Department BVCR
rem[j-1]='0';
else
rem[j-1]='1';
}
rem[genlen-1]=msg[i+genlen];
strcpy(temp,rem);
}
strcpy(rem,temp);
strcat(msg,rem);
printf("CRC is " );
for(i=0;i<genlen-1;i++)
printf("%c",rem[i]);
printf("\nTransmitted data with checksum is ");
for(i=0;i<msglen;i++)
printf("%c",msg[i]);
for(i=0;i<genlen-1;i++)
printf("%c",rem[i]);
getch();
}
OUTPUT:-
13
Computer Networks Lab CSE Department BVCR
EXPERIMENT:6
Aim:
Write a C/C++ Program to implement congestion control using leaky bucket algorithm
Program:
#include<iostream.h>
#include<dos.h>
#include<stdlib.h>
#define bucketSize 512
void bktInput(int a,int b)
{
if(a>bucketSize)
cout<<"\n\t\tBucket overflow";
else
{
delay(500);
while(a>b)
{
cout<<"\n\t\t"<<b<<" bytes outputted.";
a-=b;
delay(500);
}
if (a>0) cout<<"\n\t\tLast "<<a<<" bytes sent\t";
cout<<"\n\t\tBucket output successful";
}
}
void main()
{
int op, pktSize;
randomize();
cout<<"Enter output rate : "; cin>>op;
14
Computer Networks Lab CSE Department BVCR
for(int i=1;i<=5;i++)
{
delay(random(1000));
pktSize=random(1000);
cout<<"\nPacket no "<<i<<"\tPacket size = "<<pktSize;
bktInput(pktSize,op);
}
}
OUTPUT
15
Computer Networks Lab CSE Department BVCR
EXPERIMENT:7
Aim: Write a C/C++ Program to implement Dijkstra‘s algorithm to compute the
Shortest path through a graph.
#include<stdio.h>
#include<conio.h>
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n***** DIJKSTRA’S ALGORITHM FOR FINDING SHORTEST
16
Computer Networks Lab CSE Department BVCR
OUTPUT:-
Enter the Number of Nodes:6
Enter the Cost Matrix:
024000
001420
000030
000002
17
Computer Networks Lab CSE Department BVCR
000302
000000
18
Computer Networks Lab CSE Department BVCR
EXPERIMENT:8
Aim: Write a C/C++ Program to implement Distance vector routing algorithm by
obtaining routing table at each node.
Program:
#include<stdio.h>
#include<ctype.h>
int graph[12][12];
int e[12][12];
int ad[12];
int no,id,adc,small,chosen;
char nodes[12]={“a b c d e f g h i j k l”};
int main()
{
int i,j,k,ch1,ch2=0;
adc=0;
printf("enter the no nodes");
scanf("%d",&no);
printf("enter the value for adjacency matrix");
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
printf(" enter values for %d,%d position:",(i+1),(j+1));
scanf("%d",&graph[i][j]);
}
}
printf("enter initial estimates");
for(i=0;i<no;i++)
{
printf("estimate for node %c \n",nodes[i]);
19
Computer Networks Lab CSE Department BVCR
for(j=0;j<no;j++)
{
printf("to node%c",nodes[j]);
scanf("%d",&e[i][j]);
}
}
do
{
printf("\n menu:\n 1.routing info for node");
printf("2.estimated table\n ");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\n which node should routing table be built(1-a)(2-b)...");
scanf("%d",&id);
id--;
adc=0;
printf("neighbours for particular node");
for(i=0;i<no;i++)
{
if(graph[id][i]==1)
{
ad[adc]=i;
adc++;
printf("%c",nodes[i]);
}
}
for(i=0;i<no;i++)
{
20
Computer Networks Lab CSE Department BVCR
if(id!=i)
{
small=100;
chosen=-1;
for(j=0;j<adc;j++)
{
int total=e[ad[j]][i]+e[id][ad[j]];
if(total<small)
{
small=total;
chosen=j;
}
}
e[id][i]=small;
printf("\n shortest estimate to %c is %d",nodes[i],small);
printf("\n next hop is %c",nodes[ad[chosen]]);
}
else
e[id][i]=0;
}
break;
case 2:
printf("\n");
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
printf("%d",e[i][j]);
printf("\n");
}
break;
}
21
Computer Networks Lab CSE Department BVCR
OUTPUT:
Enter the no of nodes 4
Enter the values for adjacency matrix:
Enter the value for 1,1 position:0
Enter the value for 1,2 position:1
Enter the value for 1,3 position:1
Enter the value for 1,4 position:0
Enter the value for 2,1 position:1
Enter the value for 2,2 position:0
Enter the value for 2,3 position:0
Enter the value for 2,4 position:1
Enter the value for 3,1 position:1
Enter the value for 3,2 position:0
Enter the value for 3,3 position:0
Enter the value for 3,4 position:1
Enter the value for 4,1 position:0
Enter the value for 4,2 position:1
Enter the value for 4,3 position:1
Enter the value for 4,4 position:0
To node a 0
22
Computer Networks Lab CSE Department BVCR
To node b 32
To node c 25
To node d 66
Estimate for node b
To node a 10
To node b 0
To node c 23
To node d 56
Menu
1. Routing Information for a node
2. Estimate table
Enter the choice (1,2):1
Which node should routing table be built (1-a)(2-b)...1
The neighbours for the particular node: b c
Shortest estimate to b is 32
Next hop is b
Shortest estimate to c is 25
Shortest estimate to d is 66
23
Computer Networks Lab CSE Department BVCR
Nest hop is c
Do you want to continue (1-yes, 2-no)
Menu
1. Routing Information for a node
2. Estimate table
Enter the choice (1, 2):2
0 32 25 66
10 0 23 56
12 23 0 41
13 53 62 0
24
Computer Networks Lab CSE Department BVCR
EXPERIMENT:9
Aim: Write a Program to implement Broadcast tree by taking subnet of hosts.
Program:
#include<stdio.h>
int graph[12][12];
int main()
{
int i,j;
printf(" Enter no. of nodes:");
scanf("%d",&no);
printf("Enter the adjacency matrix:");
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
printf(" Enter values for %d,%d position:",(i+1),(j+1));
scanf("%d",&graph[i][j]);
}
}
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
if(graph[i][j]!=0&&j>i)
printf(“ hop %d sends a packet to hop %d”, i,j);
}
}
}
25
Computer Networks Lab CSE Department BVCR
OUTPUT:
26
Computer Networks Lab CSE Department BVCR
27