CN LAB
CN LAB
Experiment 1
AIM: Implement the data link layer framing methods such as character, character-stuffing and
bit stuffing.
Theory: In Data Link layer, the stream of bits from the physical layer is divided into data frames.
The data frames can be of fixed length or variable length. In variable – length framing, the size of
each frame to be transmitted may be different. So, a pattern of bits is used as a delimiter to mark
the end of one frame and the beginning of the next frame. However, if the pattern occurs in the
message, then mechanisms needs to be incorporated so that this situation is avoided.
The two common approaches are −
• Bit - Stuffing − A pattern of bits of arbitrary length is stuffed in the message to
differentiate from the delimiter. This is also called bit - oriented framing.
In a data link frame, the delimiting flag sequence generally contains six or more consecutive 1s.
In order to differentiate the message from the flag in case of the same sequence, a single bit is
stuffed in the message. Whenever a 0 bit is followed by five consecutive 1bits in the message, an
extra 0 bit is stuffed at the end of the five 1s.
When the receiver receives the message, it removes the stuffed 0s after each sequence of five 1s.
The un-stuffed message is then sent to the upper layers.
• Byte - Stuffing − A byte is stuffed in the message to differentiate from the delimiter.
This is also called character-oriented framing.
If the pattern of the flag byte is present in the message byte sequence, there should be a strategy
so that the receiver does not consider the pattern as the end of the frame. Here, a special byte
called the escape character (ESC) is stuffed before every byte in the message with the same
pattern as the flag byte. If the ESC sequence is found in the message byte, then another ESC byte
is stuffed before it.
1
Program Code: // BIT Stuffing program
#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 (0's & 1's only):");
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 stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
}
Output:
Enter frame length:5
Enter input frame (0's & 1's only):
1
1
2
1
1
1
After stuffing the frame is:111110
3
Program Code:
#include <stdio.h>
#include <string.h>
void main() {
int i = 0, j = 0, n, pos;
char a[20], b[50], ch;
n = strlen(a);
// Validate position
while (pos > n || pos < 1) {
printf("Invalid position, Enter again: ");
scanf("%d", &pos);
}
while (i < n) {
// Insert stuffing at the given position
if (i == pos - 1) {
b[j++] = 'd';
b[j++] = 'l';
b[j++] = 'e';
b[j++] = ch;
b[j++] = 'd';
b[j++] = 'l';
b[j++] = 'e';
}
b[j++] = a[i++];
}
OUTPUT
5
Experiment 2
AIM: Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC,
CCIP.
Theory: CRC method can detect a single burst of length n, since only one bit per column will be
changed, a burst of length n+1 will pass undetected, if the first bit is inverted, the last bit is
inverted and all other bits are correct. If the block is badly garbled by a long burst or by multiple
shorter burst, the probability that any of the n columns will have the correct parity that is 0.5. so
the probability of a bad block being expected when it should not be 2 power(-n). This scheme
sometimes is known as Cyclic Redundancy Code.
• 9 bits (CRC-8)
• 17 bits (CRC-16)
• 33 bits (CRC-32)
• 65 bits (CRC-64)
Algorithm Steps:
Step 1: Declare int crc 16, SHIFT_CRC, shift Byte, Byte_SIZE as global variables.
Step 4: In cal CRC 16 each character from input shifted with shift-byte where value 987.
6
Source Code:
#include <stdio.h>
int data[20], gen[10], temp[20];
int dl, gl;
void left_shift(int c) {
for (int i = 0; i < gl - 1; i++) {
temp[i] = temp[i + 1];
}
if (c < dl) {
temp[gl - 1] = data[c];
} else {
temp[gl - 1] = 0;
}
}
void xor() {
if (temp[0] == 1) {
for (int i = 0; i < gl; i++) {
temp[i] = temp[i] ^ gen[i];
}
}
}
int main() {
int choice;
printf("1. Generate CRC\n2. Check CRC\nEnter choice: ");
scanf("%d", &choice);
printf("Enter the length of data: ");
scanf("%d", &dl);
printf("Enter the data bits: ");
for (int i = 0; i < dl; i++) {
scanf("%d", &data[i]);
}
printf("Enter the length of generator: ");
scanf("%d", &gl);
printf("Enter the generator bits: ");
for (int i = 0; i < gl; i++) {
scanf("%d", &gen[i]);
}
for (int i = 0; i < gl - 1; i++) {
data[dl + i] = 0;
}
for (int i = 0; i < gl; i++) {
temp[i] = data[i];
}
if (choice == 1) {
for (int c = gl; c < dl + gl; c++) {
xor();
left_shift(c);
}
printf("CRC: ");
7
for (int i = 1; i < gl; i++) {
data[dl + i - 1] = temp[i];
printf("%d", temp[i]);
}
printf("\nFinal Data with CRC: ");
for (int i = 0; i < dl + gl - 1; i++) {
printf("%d", data[i]);
}
printf("\n");
} else {
printf("Enter received data: ");
for (int i = 0; i < dl + gl - 1; i++) {
scanf("%d", &data[i]);
}
int error = 0;
printf("Remainder: ");
for (int i = 1; i < gl; i++) {
printf("%d", temp[i]);
if (temp[i] != 0) {
error = 1;
}
}
printf("\n");
if (error) {
printf("Error detected in received data.\n");
} else {
printf("No error detected.\n");
}
}
return 0;
}
OUTPUT
1. Generate CRC
2. Check CRC
Enter choice: 1
Enter the length of data: 6
Enter the data bits: 1
0
1
1
0
1
8
Enter the length of generator: 4
Enter the generator bits: 1
1
0
1
CRC: 100
Final Data with CRC: 101101100
9
Experiment 3
AIM: Develop a simple data link layer that performs the flow control using the sliding window
protocol, and loss recovery using the Go-Back-N mechanism.
Theory: In Go-Back-N ARQ, N is the sender's window size. Suppose we say that Go-
Back-3, which means that the three frames can be sent at a time before expecting the
acknowledgment from the receiver.
It uses the principle of protocol pipelining in which the multiple frames can be sent
before receiving the acknowledgment of the first frame. If we have five frames and the
concept is Go-Back-3, which means that the three frames can be sent, i.e., frame no 1,
frame no 2, frame no 3 can be sent before expecting the acknowledgment of frame no
1.
In Go-Back-N ARQ, the frames are numbered sequentially as Go-Back-N ARQ sends the
multiple frames at a time that requires the numbering approach to distinguish the
frame from another frame, and these numbers are known as the sequential numbers.
The number of frames that can be sent at a time totally depends on the size of the
sender's window. So, we can say that 'N' is the number of frames that can be sent at a
time before receiving the acknowledgment from the receiver.
The sequence number of the outbound frames depends upon the size of the sender's
window. Suppose the sender's window size is 2, and we have ten frames to send, then
the sequence numbers will not be 1,2,3,4,5,6,7,8,9,10. Let's understand through an
example.
The number of bits in the sequence number is 2 to generate the binary sequence
00,01,10,11.
10
Algorithm Steps:
Step 6: Transfer the packet until it reaches the maximum defined size.
Step 7: Resume the window size and repeat the above two steps until packets in.
Source Code:
#include <stdio.h>
int main() {
int w, i, f, frames[50];
printf("\nWith sliding window protocol, the frames will be sent in the following manner\n");
printf("(assuming no corruption of frames)\n\n");
11
printf("After sending %d frames at each stage, sender waits for acknowledgement from the receiver\n\n", w);
if ((i + 1) % w == 0) {
printf("\nAcknowledgement of above frames sent is received by sender\n\n");
}
}
return 0;
}
OUTPUT
With sliding window protocol, the frames will be sent in the following manner
(assuming no corruption of frames)
After sending 3 frames at each stage, sender waits for acknowledgement from the receiver
2 23 4
Acknowledgement of above frames sent is received by sender
12 14
Acknowledgement of above frames sent is received by sender
12
13
14