CN Program1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Course Outcomes

CO1: Gain the knowledge layer functionality of the OSI and TCP/IP reference model.
CO2: Implement Encoding and Decoding techniques used in Presentation layer.
CO3: Understand and Implement the Data link layer its design issues.
CO4: To understand layer and its design issues.
CO5: To Analyze the Congestion control algorithms and experiment with the Transport
layer, its services and Protocols.
CO6: To be able to work with different network tools.

Procedure for executing the programs in GCC (Graphic Carbon Compiler):


1. Open Terminal and check whether GCC is installed or not using below command
gcc - - version
2. Create a new file save it with any filename.c using below commands
cd Desktop
cd foldername
> filename.c
3. Open the above created C file and type your program and save it.
4. Now complie the program using the command
gcc filename.c – o filename
5. If there are no errors run the program using below command
.\filename
Program No.1
1. Implement the data link layer framing methods such as character, character-stuffing
and bit stuffing.

1.a) AIM: To implement the data link layer framing methods such as character count

Software Required: PC loaded with GCC/Turbo C

Theory:

This method is rarely used and is generally required to count total number of characters that
are present in frame. This is done by using field in header. Character count method ensures
data link layer at the receiver or destination about total number of characters that follow, and
about where the frame ends.
There is a disadvantage also of using this method i.e., if character count is disturbed or
distorted by an error occurring during transmission, then destination or receiver might lose
synchronization. The destination or receiver might also be not able to locate or identify
beginning of next frame.

Algorithm:

Begin
Step 1: Read the data
Step 2: Read the string length

Step 3: Read the frame size


Step 4: Initialize s=frame size
Step 5: Initialize n=(datasize/trueframe)+1
Step 6: Initialize j=0
Step 7: Create a loop for i=0 to i<(datasize+n)
Step 8: If i% fsize is 0
Step 9: If datasize-j < framesize
Step 10: framesize=datasize – j+1
Step 11: stuff[i]=(char)(48+framesize)
Step 12: framesize=s
Step 13: else
Step 14: stuff [i]= data[j]

Step 15: Increment j

Step 16: print the stuffed data


End

Source Code:

#include<stdio.h>
#include<string.h>
int main()
{
char data[50],stuff[50],dstuff[50];
int i,j,fsize,n,tframe,dsize,s;
printf("Enter Data \n");
scanf("%s",data);
dsize=strlen(data);
printf("Enter frame size \n");
scanf("%d",&fsize);
s=fsize;
tframe=fsize-1;
n=(dsize/tframe)+1;
j=0;
for(i=0;i<(dsize+n);i++)
{
if(i%fsize==0)
{
if((dsize-j)<fsize)
{
fsize=dsize-j+1;
}
stuff[i]=(char)(48+fsize);
fsize=s;
}
else
{
stuff[i]=data[j];
j++;
}
}
stuff[dsize+n]='\0';
printf("stuffed data is %s\n",stuff);
return 0;
}

OUTPUT:
Enter Data
welcome
Enter frame size
4
stuffed data is 4wel4com2e

1.b) AIM: To implement the data link layer framing methods such as bit stuffing.

Software Required: PC loaded with GCC/Turbo C

Theory:

The new technique allows data frames to contain an arbitrary number if bits and allows character
codes with an arbitrary no of bits per character. Each frame begins and ends with special bit
pattern, 01111110, called a flag byte. Whenever the sender’s data link layer encounters five
consecutive ones in the data, it automatically stuffs a 0 bit into the outgoing bit stream.
Algorithm:
Begin
Step 1: Read frame length n
Step 2: Repeat step (3 to 4) until i<n(: Read values in to the input frame (0’s
and 1’s) i.e.

Step 3: initialize I i=0;


Step 4: read a[i] and increment i
Step 5: Initialize i=0, j=0,count =0
Step 6: repeat step (7 to 22) until i<n
Step 7: If a[i] == 1 then
Step 8: b[j] = a[i]
Step 9: Repeat step (10 to 18) until (a[k] =1 and k<n and count <5)
Step 10: Initialize k=i+1;
Step 11: Increment j and b[j]= a[k];
Step 12: Increment count ;
Step 13: if count =5 then

Step 14: increment j,

Step 15: b[j] =0

Step 16: end if


Step 17: i=k;
Step 18: increment k
Step 19: else
Step 20: b[j] = a[i]
Step 21: end if
Step 22: increment I and j
Step 23: print the frame after bit stuffing
Step 24: repeat step (25 to 26) until i< j
Step 25: print b[i]

Step 26: increment i


End

Source Code:

#include<stdio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame length");
scanf("%d",&n);
printf("Enter input frame:");
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]);
}

OUTPUT:
Enter frame length 9
Enter input frame:1
0
0
1
1
1
1
1
0
After bit stuffing:1001111100
1.c) AIM: To implement the data link layer framing methods such as character stuffing.

Software Required: PC loaded with GCC/Turbo C

Theory:

The framing method gets around the problem of resynchronization after an error by having each
frame start with the ASCII character sequence DLE STX and the sequence DLE ETX and extra
character that is stuffed is added with DLE before and after it. If the destination ever losses the
track of the frame boundaries all it has to do is look for DLE STX or DLE ETX characters to
figure out. The data link layer on the receiving end removes the DLE before the data are given to
the network layer. This technique is called character stuffing.

Algorithm:

Begin
Step 1: Initialize i and j as 0
Step 2: Declare n and pos as integer and a[20],b[50],ch as character
Step 3: read the string a
Step 4: find the length of the string n, i.e n-strlen(a)
Step 5: read the position, pos
Step 6: if pos > n then
Step 7: print invalid position and read again the position, pos
Step 8: end if
Step 9: read the character, ch
Step 10: Initialize the array b , b[0…5] as ’d’, ’l’, ’e’, ’s’ ,’t’,’x’ respectively

Step 11: j=6;


Step 12: Repeat step[(13to22) until i<n
Step 13: if i==pos-1 then
Step 14: initialize b array,b[j],b[j+1]…b[j+6] as‘d’, ‘l’, ‘e’ ,’ch, ’d’,
‘l’,‘e’ respectively

Step 15: increment j by 7, i.e j=j+7


Step 16: end if
Step 17: if a[i]==’d’ and a[i+1]==’l’ and a[i+2]==’e’ then
Step 18: initialize array b, b[13…15]=’d’, ‘l’, ‘e’
respectively

Step 19: increment j by 3, i.e j=j+3

Step 20: end if


Step 21: b[j]=a[i]
Step 22: increment I and j;
Step 23: initialize b array,b[j],b[j+1]…b[j+6] as‘d’, ‘l’,‘e’ ,’e’,‘t’,
‘x’,‘\0’ respectively

Step 24: print frame after stiuffing


Step 25: print b
End
Source Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void charc(void);
void main()
{
int choice;
while(1)
{
printf("\n\n\n1.character stuffing");
printf("\n\n2.exit");
printf("\n\n\nenter choice");
scanf("%d",&choice);
printf("%d",choice);
if(choice>2)
printf("\n\n invalid option....please renter");
switch(choice)
{
case 1:
charc();
break;
case 2:
exit(0);
}
}
}
void charc(void)
{
char c[50],d[50],t[50];
int i,m,j;
//clrscr();
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m;i++)
{
printf("%c", d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf("\n\n received data:");
for(i=0;i<j;i++)
{
printf("%c",t[i]);
}
}

OUTPUT

1.character stuffing
2.exit

enter choice1
enter the number of characters
9

enter the characters


dledleabc

original data
dledleabc

transmitted data:
dlestx
dledledledleabcdleetx

received data:
dledleabc
1.character stuffing
2.exit
enter choice 2
RESULT: Character count, Bit-stuffing and Character-stuffing data link framing methods are
implemented in GCC compiler.

You might also like