Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Winter Semester 2021-22

Course Course
Class Group Course Title Class Id Slot
Code Type
General Network and Embedded
CSE1004 VL2021220502954 L51+L52
(Semester) Communication Lab

Student Details: Student Name: MEGHA BHATTACHARYA


Roll/Reg No: 20BCE0793
Email: megha.bhattacharya2020@vitstudent.ac.in
Mobile: 6382933694
Faculty Details: Faculty Name: VENKATA PHANIKRISHNA B
School: SCOPE
Email: venkata.phanikrishna@vit.ac.in
Assessment No. 3
Assessment Title. Error Handling Protocols in C/C++
Date of Submission 08-Mar-2022
20BCE0793 MEGHA BHATTACHARYA LAB 03

TABLE OF CONTENTS

Setup used for coding: Online GDB Compiler -


https://www.onlinegdb.com/online_c_compiler

SN QUESTION PG NO
O
01 Implementation of Go Back N 03
Sliding Window Protocol
02 Implementation of Check Sum 06
03 Implementation of CRC 14

For each of the above –


Related Theory information, Algorithm, Program code,
Output, Observation is provided in the document.
Also for few codes some sites from the internet was
referred.

2
20BCE0793 MEGHA BHATTACHARYA LAB 03

01. Implementation of Go Back N Sliding


Window Protocol
Related Description:
In Go-Back-N ARQ, N is the sender's window size. Suppose we say tha t
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. Receiver window size is
one. If the acknow ledgment of a frame is not received within an agree
upon time period, then all the frames available in the current window will
be retra nsmitted.

Program Code:
//GO BACK N
#include<stdio.h>
int main()
{
printf ("NAME: MEGHA BHATTACHARYA\n");
printf ("REG NO: 20BCE0793\n");
printf ("SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n");
printf ("LAB ASSESSMENT: 03\n");
printf ("GO BACK N SLIDING WINDOW PROTOCOL\n\n\n\n");

int windowsize,sent=0,ack,i;
printf("Enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\
n");
scanf("%d",&ack);

3
20BCE0793 MEGHA BHATTACHARYA LAB 03

if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}

Screenshot of code in terminal:

Output:
PTO 

4
20BCE0793 MEGHA BHATTACHARYA LAB 03

5
20BCE0793 MEGHA BHATTACHARYA LAB 03

Observation:
In the above we can see that, in Go-Back-N ARQ, N is the sender's window
size. If the acknow ledgment of a frame is not received within a time
period, then all the frames available in the current window will be
retra nsmitted. Like in the case there were 8 frames and we told that the last
acknowledgement is of 6th frame. So it retransmitted the next two frames
that is frame 7 and frame 8.

02. Implementation of Check Sum in C++


Related Description:
A Checksum is generated at the sending side. Checksum generator
subdivides the data into equal segments of n bits each, and all these
segments are added together by using one's complement arithmetic. The
sum is complemented and appended to the original data, known as
checksum field. The extended data is transmitted across the network.

Algorithm:
a. Start
b. Divide the message into the binary string of given block size
c. All the binary strings are added together to get the sum
d. The one’s complement of the binary string representing the sum is the
required checksum value
e. Check if the value of the received message is equal to zero
f. The checksum of the received message can be calculated similarly to the
checksum calculated in the above process
g. If the checksum value is zero, the message is transmitted properly with no
errors otherwise some error has occurred during the transmission
h. End

Program Code:
//CHECKSUM
#include <iostream>
#include <string>
using namespace std;
// Function to find the One's complement of given string

6
20BCE0793 MEGHA BHATTACHARYA LAB 03

string ones_comp(string s)
{
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '0')
{
s[i] = '1';
}
else
{
s[i] = '0';
}
}
return s;
}
// Function to return the checksum value of
// the give string when divided in K size blocks
string checkSum(string inp, int blocksize)
{
int n = inp.size();
if (n % blocksize != 0)
{
int pad_size = blocksize - (n % blocksize);
for (int i = 0; i < pad_size; i++)
{
inp = '0' + inp;
}
}
string result = "";
for (int i = 0; i < blocksize; i++)
{
result += inp[i];
}
for (int i = blocksize; i < n; i += blocksize)
{
string nextblock = "";
for (int j = i; j < i + blocksize; j++)
{
nextblock += inp[j];
}
string add = "";
int sum = 0, carry = 0;
for (int k = blocksize - 1; k >= 0; k--)

7
20BCE0793 MEGHA BHATTACHARYA LAB 03

{
sum += (nextblock[k] - '0') + (result[k] - '0');
carry = sum / 2;
if (sum == 0)
{
add = '0' + add;
sum = carry;
}
else if (sum == 1)
{
add = '1' + add;
sum = carry;
}
else if (sum == 2)
{
add = '0' + add;
sum = carry;
}
else
{
add = '1' + add;
sum = carry;
}
}
string final = "";
if (carry == 1)
{
for (int l = add.size() - 1; l >= 0; l--)
{
if (carry == 0)
{
final = add[l] + final;
}
else if (((add[l] - '0') + carry) % 2 == 0)
{
final = "0" + final;
carry = 1;
}
else
{
final = "1" + final;
carry = 0;
}

8
20BCE0793 MEGHA BHATTACHARYA LAB 03

}
result = final;
}
else
{
result = add;
}
}
cout << "Sum is: " << result << endl;
string chres = ones_comp(result);
cout << "Check_Sum is: " << chres << endl;
return ones_comp(result);
}
int count0(string inp)
{
int res = 0;
for (int i = 0; i < inp.size(); ++i)
{
if (inp[i] == '0')
{
res++;
}
}
return res;
}
bool checkboth_checksum(string sent_message, string rec_message, int
block_size)
{
cout << "\nSender side calculation:" << endl;
string sender_checksum = checkSum(sent_message, block_size);
cout << "\nReciever side calculation:" << endl;
string receiver_checksum = checkSum(rec_message, block_size);
int gotsize = count0(receiver_checksum);
if (gotsize == block_size)
{
return true;
}
else
{
return false;
}
}
// Driver Code

9
20BCE0793 MEGHA BHATTACHARYA LAB 03

int main()
{
cout << "Enter the original data:" << endl;
string sent_message;
cin >> sent_message;
cout << "Enter the recieved message:" << endl;
string recv_message;
cin >> recv_message;
int block_size = 8;
if (checkboth_checksum(sent_message, recv_message, block_size))
{
cout << "No Error";
}
else
{
cout << "Error";
}
return 0;
}

Screenshot of code in terminal:

10
20BCE0793 MEGHA BHATTACHARYA LAB 03

11
20BCE0793 MEGHA BHATTACHARYA LAB 03

12
20BCE0793 MEGHA BHATTACHARYA LAB 03

Output:

13
20BCE0793 MEGHA BHATTACHARYA LAB 03

Observation:
The Sender followed the given steps:
 The block unit is divided into k sections, and each of n bits.
 All the k- sections are added together by using one's complement to get
the sum.
 The sum is complemented and it becomes the checksum field.
 The original data and checksum field are sent across the network.

The Receiver followed the given steps:


 The block unit is divided into k sections and each of n bits.
 All the k sections are added together by using one's complement
algorithm to get the sum.
 The sum is complemented.
 If the result of the sum is zero, then the data is accepted otherwise
the data is discarded.
In this way the Check Sum Error handling is carried out.

03. Implementation of CRC in C++


Related Description:
CRC is also known as Cyclic Redundancy Check. Here, It uses a cyclic code.
Some redundant bits are added to the data by the sender. The receiver checks
the bits received. A CRC generator is known both by the sender and the
receiver. If the CRC generator is 4 bits long then 3 bits will be added by the
sender as it adds (n-1) bit. The algorithm is further described below.
Algorithm:
a. Start
a. In sender side
The binary data is first augmented by adding k-1 zeros in the end of the
data
b. Use modulo-2 binary division to divide binary data by the key and store
remainder of division.
c. Append the remainder at the end of the data to form the encoded data and
send the same

d. In receiver side

14
20BCE0793 MEGHA BHATTACHARYA LAB 03

In each step, a copy of the divisor (or data) is XORed with the k bits of
the dividend (or key).
e. The result of the XOR operation (remainder) is (n-1) bits, which is used
for the next step after 1 extra bit is pulled down to make it n bits long.
f. When there are no bits left to pull down, we have a result. The (n-1)-bit
remainder which is appended at the sender side.
g. End

Program Code:
//CRC
#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;
char exor(char a, char b) // perform exor operation
{
if (a == b)
return '0';
else
return '1';
}

void crc(char data[], char key[])


{
int datalen = strlen(data);
int keylen = strlen(key);
cout << "\nSender side calculation:" << endl;
for (int i = 0; i < keylen - 1; i++) // appending n-1 zeroes to data
data[datalen + i] = '0';
data[datalen + keylen - 1] = '\0';
int codelen = datalen + keylen - 1; // lenght of appended data word
char temp[20], rem[20];
for (int i = 0; i < keylen; i++)
rem[i] = data[i]; // considering n bits of data for each step of binary
division/EXOR
for (int j = keylen; j <= codelen; j++)
{
for (int i = 0; i < keylen; i++)
temp[i] = rem[i]; // remainder of previous step is divident of current step
if (rem[0] == '0') // if 1's bit of remainder is 0 then shift the rem by 1 bit
{
for (int i = 0; i < keylen - 1; i++)

15
20BCE0793 MEGHA BHATTACHARYA LAB 03

rem[i] = temp[i + 1];


}
else // else exor the divident with generator key
{
for (int i = 0; i < keylen - 1; i++)
rem[i] = exor(temp[i + 1], key[i + 1]);
}
if (j != codelen)
rem[keylen - 1] = data[j]; // appending next bit of data to remainder obtain by
division
else
rem[keylen - 1] = '\0';
}
for (int i = 0; i < keylen - 1; i++)
data[datalen + i] = rem[i]; // replace n-1 zeros with n-1 bit CRC
data[codelen] = '\0';
cout << "Our remainder is: " << rem << endl;
cout << "Our Data is after adding CRC: " << data << endl;
char temp2[20], rem2[20];
cout << "\nReciever side claculation: " << endl;
for (int i = 0; i < keylen; i++)
rem2[i] = data[i]; // considering n bits of data for each step of binary
division/EXOR
for (int j = keylen; j <= codelen; j++)
{
for (int i = 0; i < keylen; i++)
temp2[i] = rem2[i]; // rem2ainder of previous step is divident of current step
if (rem2[0] == '0') // if 1's bit of remainder is 0 then shift the rem by 1 bit
{
for (int i = 0; i < keylen - 1; i++)
rem2[i] = temp2[i + 1];
}
else // else exor the divident with generator key
{
for (int i = 0; i < keylen - 1; i++)
rem2[i] = exor(temp2[i + 1], key[i + 1]);
}
if (j != codelen)
rem2[keylen - 1] = data[j]; // appending next bit of data to rem2ainder obtain by
division
else
rem2[keylen - 1] = '\0';
}

16
20BCE0793 MEGHA BHATTACHARYA LAB 03

cout << "Our remainder is: " << rem2 << endl;
cout << "Data is a accepted" << endl;
}

int main()
{

cout<<"NAME: MEGHA BHATTACHARYA\n";


cout<<"REG NO: 20BCE0793\n";
cout<<"SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n";
cout<<"LAB ASSESSMENT: 03\n";
cout<<"CRC IMPLEMENTATION\n\n\n";

char key[20], data[20];


cout << "Enter the data:";
cin >> data;
cout << "Enter the key:";
cin >> key;
crc(data, key); // generate crc
return 0;
}

Screenshot of code in terminal:

17
20BCE0793 MEGHA BHATTACHARYA LAB 03

18
20BCE0793 MEGHA BHATTACHARYA LAB 03

Output:

19
20BCE0793 MEGHA BHATTACHARYA LAB 03

Observation:
In sender side, the binary data is first augmented by adding k-1 zeros in the end
of the data. Then we use modulo-2 binary division to divide binary data by the
key and store remainder of division. Append the remainder at the end of the
data to form the encoded data and send the same.
In receiver side, in each step, a copy of the divisor (or data) is XORed with the
k bits of the dividend (or key). The result of the XOR operation (remainder) is
(n-1) bits, which is used for the next step after 1 extra bit is pulled down to
make it n bits long. When there are no bits left to pull down, we have a result.
The (n-1)-bit remainder which is appended at the sender side.
In this way we have control errors by Cyclic Redundancy Check.

_____________________________________________
THE END
THANK YOU
_____________________________________________

20
20BCE0793 MEGHA BHATTACHARYA LAB 03

DONE BY:

Student Details:
Student Name: MEGHA BHATTACHARYA
Roll/Reg No: 20BCE0793
Email: megha.bhattacharya2020@vitstudent.ac.in
Mobile: 6382933694

21

You might also like