0% found this document useful (0 votes)
13 views27 pages

CN Lab 18112022

Uploaded by

maya
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)
13 views27 pages

CN Lab 18112022

Uploaded by

maya
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/ 27

Computer Networks Lab CSE Department BVCR

Experiment-1
Aim: Study of Network Devices in Detail and Connect the computers in Local
Area Network

Network Devices in Detail


• Repeater
• Hub
• Switch
• Bridge
• Router
• Gate Way

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

Procedure: Connect the computers in Local Area Network


1. Log on to the host computer as Administrator or as Owner.
2. Click Start, and then click Control Panel.
1
Computer Networks Lab CSE Department BVCR

3. Click Network and Internet Connections.


4. Click Network Connections.
5. Right-click the connection that you use to connect to the Internet. For example, if you connect to
the Internet by using a modem, right-click the connection that you want under Dial-up / other network
available.
6. Click Properties.
7. Click the Advanced tab.
8. Under Internet Connection Sharing, select the Allow other network users to connect through this
computer's Internet connection check box.
9. If you are sharing a dial-up Internet connection, select the Establish a dial-up connection whenever
a computer on my network attempts to access the Internet check box if you want to permit your
computer to automatically connect to the Internet
. 10. Click OK. You receive the following message: When Internet Connection Sharing is enabled,
your LAN adapter will be set to use IP address.
11. Click Yes. The connection to the Internet is shared to other computers on the local area network
(LAN). The network adapter that is connected to the LAN is configured with a static IP address of a
subnet mask of 255.255.255.0
12.On the client computer To connect to the Internet by using the shared connection, you must
confirm the LAN adapter IP configuration, and then configure the client computer. To confirm the
LAN adapter IP configuration, follow these steps:
1. Log on to the client computer as Administrator or as Owner.
2. Click Start, and then click Control Panel. Then goto network and internet settings
3.Go for change adapter settings properties then set the ip address of your client system in the protocol
versionIPV4

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

i) Character stuffing ii) bit stuffing.

Program:

/*Program to implement Character stuffing*/

#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);

printf("\n After stuffing:%s", fs);

3
Computer Networks Lab CSE Department BVCR

getch();

OUTPUT:
Enter characters to be stuffed: goodday

Enter a character that represents starting delimiter: d

Enter a character that represents ending delimiter: g

After stuffing: dggooddddayg.

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 :");

for(i=0; i<j; i++)


printf("%d",b[i]);
5
Computer Networks Lab CSE Department BVCR

getch();

OUTPUT:
Enter frame size (Example: 8):12

Enter the frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0 0 1

After Bit Stuffing :0101111101001

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]<<" ";

cout<<"\nHamming code --- Decoding\n";


cout<<"Enter Encoded bits as received : ";
for(i=0;i<7;i++) cin>>edata[i];
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
syndrome[i]=syndrome[i]+(edata[j]*hmatrix[i][j]);
syndrome[i]=syndrome[i]%2;

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)

cout<<"Data is error free!!\n";

else
{

cout<<"Error received at bit number "<<j+1<<" of the data\n";

edata[j]=!edata[j];

cout<<"The Correct data Should be : ";

for(i=0;i<7;i++) cout<<edata[i]<<" ";

OUTPUT

Hamming Code --- Encoding

Enter 4 bit data : 1 0 1 0

Generator Matrix

0111000

1010100

1100010

1110001
10
Computer Networks Lab CSE Department BVCR

Encoded Data : 1 0 1 1 0 1 0

Hamming code --- Decoding

Enter Encoded bits as received : 1 0 1 1 0 1 1

Error received at bit number 7 of the data

The Correct data Should be : 1 0 1 1 0 1 0

Hamming Code --- Encoding

Enter 4 bit data : 1 0 1 0

Generator Matrix

0111000

1010100

1100010

1110001

Encoded Data : 1 0 1 1 0 1 0

Hamming code --- Decoding

Enter Encoded bits as received : 1 0 1 1 0 1 0

Data is error free!!

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:-

enter the message string 0001


Enter the generator string 1011
CRC is 011
Transmitted data with checksum is 0001011

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

Enter output rate : 100


Packet no 0 Packet size = 3
Bucket output successful
Last 3 bytes sent
Packet no 1 Packet size = 33
Bucket output successful
Last 33 bytes sent
Packet no 2 Packet size = 117
Bucket output successful
100 bytes outputted.
Last 17 bytes sent
Packet no 3 Packet size = 95
Bucket output successful
Last 95 bytes sent
Packet no 4 Packet size = 949
Bucket overflow

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

PATHS TO EACH NODE *****\n");


printf("\nEnter the Number of Nodes:");
scanf("%d",&n);
printf("\nEnter the Cost Matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("\nEnter the Source Node:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n\n***** Shortest Paths to Each Node *****:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("Node %d -> Node %d, cost=%d\n",v,i,dist[i]);
getch();
}

OUTPUT:-
Enter the Number of Nodes:6
Enter the Cost Matrix:
024000

001420

000030

000002

17
Computer Networks Lab CSE Department BVCR

000302

000000

Enter the Source Node:1

***** Shortest Paths to Each Node *****:

Node 1 -> Node 2, cost=2

Node 1 -> Node 3, cost=3

Node 1 -> Node 4, cost=6

Node 1 -> Node 5, cost=4

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

printf("\n do u want to continue (1-yes,2-no)");


scanf("%d",&ch2);
}
while(ch2==1);
getch();
}

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

Enter the initial estimates for node a

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

Enter the initial estimates for node c


To node a 12
To node b 23
To node c 0
To node d 41

Enter the initial estimates for node d


To node a 13
To node b 53
To node c 62
To node d 0

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:

Enter 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:1

Enter the value for 2,1 position:1

Enter the value for 2,2 position:0

Enter the value for 2,3 position:1

Enter the value for 2,4 position:1

Enter the value for 3,1 position:1

Enter the value for 3,2 position:1

Enter the value for 3,3 position:0

Enter the value for 3,4 position:1

Enter the value for 4,1 position:1

Enter the value for 4,2 position:1

Enter the value for 4,3 position:1

Enter the value for 4,4 position:0

Hop 1 sends a packet to hop 2

26
Computer Networks Lab CSE Department BVCR

Hop 1 sends a packet to hop 3

Hop 1 sends a packet to hop 4

Hop 2 sends a packet to hop 3

Hop 2 sends a packet to hop 4

Hop 3 sends a packet to hop 4

27

You might also like