0% found this document useful (0 votes)
17 views9 pages

CN Exp1,2

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)
17 views9 pages

CN Exp1,2

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/ 9

Expt.

No:
Page No:
Date:
EXPERIMENT-1
Aim:To write a Program to implement the data link layer farming methods such as
Character stuffing

Description:
Character stuffing is commonly used in data communication protocols to ensure the
receiving system correctly interprets the transmitted data. It helps frame the data so that the
receiver can easily identify the start and end of each data frame. One common use case of
character stuffing is serial communication, where data is transmitted one bit at a time over a
communication channel.
Example:

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50];
printf("enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again less than %d:",n);
scanf("%d",&pos);

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:
}
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='r';
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\nframe after stuffing:\n");
printf("%s",b);

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:
getch();
}

Output:

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:

Aim:To write a Program to implement the data link layer farming methods such as Bit
stuffing

Description:
Bit stuffing is a process of inserting an extra bit as 0, once the frame sequence
encountered 5 consecutive 1’s. Given an array, arr[] of size N consisting of 0’s and 1’s, the
task is to return an array after the bit stuffing.

Approach: The idea is to check if the given array consists of 5 consecutive 1’s. Follow the
steps below to solve the problem:
 Initialize the array brr[] which stores the stuffed array. Also, create a variable count
which maintains the count of the consecutive 1’s.
 Traverse in a while loop using a variable i in the range [0, N) and perform the
following tasks:
o If arr[i] is 1 then check for the next 4 bits if they are set bits as well. If they are,
then insert a 0 bit after inserting all the 5 set bits into the array brr[].
o Otherwise, insert the value of arr[i] into the array brr[].

Program:

#include <stdio.h>
#include <string.h>
void bitStuffing(int N, int arr[])
{
int brr[30];
int i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 1) {
int count = 1;
brr[j] = arr[i];
for (k = i + 1;arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
j++;

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:
brr[j] = 0;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
printf("%d", brr[i]);
}
int main()
{
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
return 0;
}

Output:

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:
EXPERIMENT-2

Aim:
To Write a Program to implement data link layer farming method checksum.

Description:
The Checksum is an error detection method that detected errors in data/message while it is
transmitted from sender to receiver. This method is used by the higher layer protocols and
makes use of the Checksum Generator on the Sender side and Checksum Checker on the
Receiver side.

Approach: The given problem can be divided into two following parts:
 Generating the Checksum value of the sender’s message can be done using the
following steps:
o Divide the message into the binary strings of the given block size.
o All the binary strings are added together to get the sum.
o The One’s Complement of the binary string representing the sum is the
required checksum value.
 Check if the value of the received message (i.e, rec_message + senders_checksum) is
equal to 0.
o The checksum of the received message can be calculated similarly to the
checksum calculated in the above process.
o If the checksum value is 0, the message is transmitted properly with no errors
otherwise, some error has occurred during the transmission.

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

int sender(int b[10],int k)


{
int checksum,sum=0,i;
printf("\n****SENDER****\n");

for(i=0;i<k;i++)
sum+=b[i];
printf("SUM IS: %d",sum);
ADITYA ENGINEERING COLLEGE, SURAMPALEM
2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:

checksum=~sum;
printf("\nSENDER's CHECKSUM IS:%d",checksum);
return checksum;
}

int receiver(int c[10],int k,int scheck)


{
int checksum,sum=0,i;
printf("\n\n****RECEIVER****\n");
for(i=0;i<k;i++)
sum+=c[i];
printf(" RECEIVER SUM IS:%d",sum);
sum=sum+scheck;
checksum=~sum;
printf("\nRECEIVER's CHECKSUM IS:%d",checksum);
return checksum;
}
main()
{
int a[10],i,m,scheck,rcheck;
printf("\nENTER SIZE OF THE STRING:");
scanf("%d",&m);
printf("\nENTER THE ELEMENTS OF THE ARRAY:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
scheck=sender(a,m);
rcheck=receiver(a,m,scheck);
if(rcheck==0)
printf("\n\nNO ERROR IN TRANSMISSION\n\n");
else
printf("\n\nERROR DETECTED");
getch();
}

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:

Output:

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1
Expt. No:
Page No:
Date:

ADITYA ENGINEERING COLLEGE, SURAMPALEM


2 2 A 9 1 A 6 1

You might also like