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

Shanshak CN

The document outlines various experiments related to data communication protocols, including Bit Stuffing, Byte Stuffing, CRC, Stop-and-Wait ARQ, Go Back N ARQ, and Selective Repeat ARQ. Each experiment includes an aim, theoretical background, and code implementations demonstrating the respective techniques for ensuring reliable data transmission. The results section for each experiment details the expected inputs and outputs, focusing on error detection and frame acknowledgment mechanisms.

Uploaded by

zerotwo02x02x
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 views18 pages

Shanshak CN

The document outlines various experiments related to data communication protocols, including Bit Stuffing, Byte Stuffing, CRC, Stop-and-Wait ARQ, Go Back N ARQ, and Selective Repeat ARQ. Each experiment includes an aim, theoretical background, and code implementations demonstrating the respective techniques for ensuring reliable data transmission. The results section for each experiment details the expected inputs and outputs, focusing on error detection and frame acknowledgment mechanisms.

Uploaded by

zerotwo02x02x
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/ 18

EXPERIMENT -1

AIM

Study and implement Bit, Byte and Character stuffing


frames.techniques used in data communication to delineate frames.

THEORY

1. Bit Stuffing

Bit stuffing is a technique used in data communication to


ensure that special bit sequences—typically used as
delimiters or control characters—do not appear
unintentionally in the data being transmitted. This method
is commonly used in protocols such as HDLC (High-Level
Data Link Control) and CAN (Controller Area Network).

Sender Side:
Before transmitting the data, the sender scans the bit stream for predefined patterns (e.g., 0111110)
that could be misinterpreted as control characters. To avoid this, the sender inserts an extra bit—
usually a 0—after every five consecutive 1 bits. This insertion disrupts the pattern, preventing any
misinterpretation.

Receiver Side:
The receiver, while reading the incoming bit stream, monitors it for these stuffed bits. Whenever five
consecutive 1s are followed by a 0, it identifies the 0 as a stuffed bit and removes it. This process
restores the original data stream, which is then processed normally.

2. Byte Stuffing (Character Stuffing)

Byte stuffing is a similar technique to bit stuffing but


operates at the byte level. It is used in protocols such as
PPP (Point-to-Point Protocol) and XMODEM.

Sender Side:
The sender inspects the byte stream for any occurrence of
control characters or delimiters. If such a byte is found, an
additional byte known as an escape character is inserted
before it. This escape character signals that the next byte
should be interpreted as data, not as a control character.

Receiver Side:
While reading the byte stream, the receiver looks for escape characters. Upon encountering one, it
understands that the following byte is actual data—even if it resembles a control character—and
processes it accordingly. The escape character itself is removed during this process.
CODE

#include <bits/stdc++.h>

using namespace std;

// Function to perform Bit Stuffing

string bitStuffing(const string& data) {

cout << "Input string data: \t" << data << endl;

const int threshold = 5;

string flag = "0";

// Create flag sequence: 01111110

for (int i = 0; i < threshold; i++)

flag += "1";

flag += "0";

string stuffedData;

int consecutiveOnes = 0;

// Traverse input data to apply bit stuffing

for (char bit : data) {

stuffedData += bit;

if (bit == '1') {

consecutiveOnes++;

// Insert a '0' after 5 consecutive 1s

if (consecutiveOnes == threshold) {

stuffedData += '0';

consecutiveOnes = 0;

} else {
consecutiveOnes = 0;

// Add flag at beginning and end

stuffedData = flag + stuffedData + flag;

return stuffedData;

// Function to perform Character (Byte) Stuffing

string characterStuffing(const string& data, char delimiter, char escape) {

cout << "Input string data: \t" << data << endl;

string stuffedData;

stuffedData += delimiter; // Starting delimiter

// Traverse input string to apply character stuffing

for (char character : data) {

// If the character is delimiter or escape, prefix it with escape

if (character == delimiter || character == escape) {

stuffedData += escape;

stuffedData += character;

stuffedData += delimiter; // Ending delimiter

return stuffedData;

}
RESULT

Input:

• Data to be framed (Binary sequence).

Output:

• Framed data according to the selected technique (e.g., bit stuffed, byte stuffed, or character
stuffed).
EXPERIMENT 2
AIM

Study and implement the CRC protocol.

THEORY
Cyclic Redundancy Check (CRC) is a method used for detecting errors in data transmission. It
involves appending a check code generated by a special algorithm to the data being sent. At
the receiver's end, the same algorithm is applied to verify the integrity of the received data.
CRC works by detecting changes in bits due to transmission faults, ensuring data accuracy. It
is widely used in digital networks and storage devices to enhance data reliability.

CODE
#include <bits/stdc++.h>

using namespace std;

#define lli long long int

string toBin(lli num) {

string bin = "";

while (num) {

bin = ((num & 1) ? "1" : "0") + bin;

num >>= 1;

return bin;

lli toDec(string bin) {

lli num = 0;

for (int i = 0; i < bin.length(); i++)

if (bin[i] == '1')

num += 1LL << (bin.length() - i - 1);

return num;
}

void CRCCheck(string codeword, string generator) {

int n = generator.length();

lli gen = toDec(generator), dword = toDec(codeword), rem;

lli dividend = dword << (n - 1);

int shft = ceil(log2(dividend + 1)) - n;

while ((dividend >= gen) || (shft >= 0)) {

rem = (dividend >> shft) ^ gen;

dividend = (dividend & ((1LL << shft) - 1)) | (rem << shft);

shft = ceil(log2(dividend + 1)) - n;

cout << "Remainder: " << dividend << endl;

cout << "Remainder " << dividend

<< ((dividend == 0) ? " = 0 , hence no error is detected."

: " != 0 , hence an error is detected during transmission.")

<< endl << endl;

void CRCSender(string dataword, string generator) {

int n = generator.length();

lli gen = toDec(generator), dword = toDec(dataword), rem;

lli dividend = dword << (n - 1);

int shft = ceil(log2(dividend + 1)) - n;

while ((dividend >= gen) || (shft >= 0)) {

rem = (dividend >> shft) ^ gen;

dividend = (dividend & ((1LL << shft) - 1)) | (rem << shft);

shft = ceil(log2(dividend + 1)) - n;

lli codeword = (dword << (n - 1)) | dividend;

cout << "Transmitted Codeword : " << toBin(codeword) << endl << endl;
}

void CRC(string dataword, string generator) {

cout << "Sender Side:" << endl;

CRCSender(dataword, generator);

cout << "Receiver Side:" << endl;

string codeword = toBin((toDec(dataword) << (generator.length() - 1)));

CRCCheck(codeword, generator);

// Modify one bit in the codeword

int bitToModify = rand() % codeword.length();

codeword[bitToModify] ^= '0' ^ '1'; // Flip the bit

cout << "Modified Codeword: " << codeword << endl;

CRCCheck(codeword, generator);

int main() {

string dataword, generator;

cout << "Enter the dataword: ";

cin >> dataword;

cout << "Enter the generator: ";

cin >> generator;

CRC(dataword, generator);

return 0;

}
RESULT
Input:

• Dataword: Binary Sequence

• Generator: Binary sequence or Polynomial equation

• Codeword: Codeword generated by the sender

• Verification Result:

o If dataword is correct: Display the codeword released by the sender.

o If error detected: Display "Invalid" along with the original codeword and the
corrupted codeword.
EXPERIMENT 3
AIM

Study and implement Stop and Wait protocol.

THEORY
Stop-and-Wait Automatic Repeat reQuest (ARQ) protocol
is a type of error-control protocol used in data
transmission over unreliable communication channels,
typically in computer networks. It ensures reliable
delivery of data by employing a simple mechanism of
sending a frame and waiting for an acknowledgment
(ACK) from the receiver before sending the next frame.

1. Successful Transmission: Sender sends frame,


receiver acknowledges within timeout.

2. Frame Lost: Sender sends frame, no


acknowledgment received within timeout,
sender retransmits.

3. ACK Lost: Sender sends frame, receiver receives but ACK is lost, sender retransmits due to
timeout.

4. Late ACK: Sender sends frame, receiver acknowledges late, sender retransmits assuming
frame loss.

CODE
#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <unistd.h> // Added for sleep() function

int main() {

int i, j, packet[30];

int fsize = 5;

printf("\n\t Frame size: %d\n", fsize);

printf("\n\t Timeout Time: 3 \n");


printf("\n\t ---- DATA LOG \n");

printf("\n FRAME\t DATA\tWAITING\tACKNOW\tRESEND");

for (i = 0; i < fsize; i++) {

packet[i] = rand() % 1000;

printf("\n %d \t %d", i + 1, packet[i]);

while (j == 0 || rand() % 2 == 0) {

sleep(1);

printf("\t1 ");

sleep(1);

for (j = 2; rand() % 2 == 0 && j < 4; j++) {

printf("%d ", j);

sleep(1);

if (j == 4)

printf("\t No \tRESENDING...\n %d \t %d", i + 1, packet[i]);

else

break;

if (j == 0) {

sleep(1);

printf("\t0 ");

printf("\t YES \t No");

printf("\n\n ALL DATA PACKETS SENT------\n");


return 0;

RESULT

Handle three cases: when the frame and acknowledgement are delivered successfully, when the
frame is lost, and when the acknowledgement is lost. Show the role of the timeout timer in the
protocol.

Input:

• Data frames to be transmitted.

• Timeout period for the timer.

Output:

• Success message for successful transmission.

• Resend the frame if the frame is lost.

• Timeout message
EXPERIMENT 4

AIM
Study and implement Go Back N ARQ.

THEORY

Go Back N Automatic Repeat Request (ARQ) is a sliding window protocol used in the data
link layer for flow control purposes. It allows for the transmission of multiple frames from
the sender to the receiver before receiving acknowledgments, enabling efficient pipelining
of data. Its components are:

• Sender Window: Maintains a fixed-sized window (N) for consecutive frame transmission.
• Receiver Window: Accepts one frame at a time.
• Operation: Frames sent without waiting for acknowledgments; sender slides window upon
acknowledgment reception.
• Retransmission: If acknowledgment not received or frames arrive out of order, sender
retransmits from corrupted frame.
• Pipelining: Allows transmission before acknowledgment reception, enhancing efficiency.

CODE

#include<bits/stdc++.h>
#include<ctime>
#define ll long long int
using namespace std;

void transmission(ll & i, ll & N, ll & tf, ll & tt) {


while (i <= tf) {
int z = 0;

for (int k = i; k < i + N && k <= tf; k++) {


cout << "Sending Frame " << k << "..." << endl;
tt++;
}
for (int k = i; k < i + N && k <= tf; k++) {
int f = rand() % 2;
if (!f) {

cout << "Acknowledgment for Frame " << k << "..." << endl;
z++;
} else {
cout << "Timeout!! Frame Number : " << k << " Not Received" << endl;

cout << "Retransmitting Window..." << endl;


break;
}
}

cout << "\n";


i = i + z;
}
}

int main() {
ll tf, N, tt = 0;
srand(time(NULL));
cout << "Enter the Total number of frames: ";

cin >> tf;


cout << "Enter the Window Size: ";
cin >> N;
ll i = 1;

transmission(i, N, tf, tt);


cout << "Total number of frames which were sent and resent are: " << tt << endl;
return 0;
}
RESULT
Input:
• Data frames to be transmitted.

• Window size.
• Timeout period for the timer.
Output:
• Success message for successful transmission.

• Resend the message if the frame is lost.


• Timeout message if acknowledgement is lost.
EXPERIMENT 5
AIM
Study and implement Selective Repeat ARQ
THEORY
Selective Repeat ARQ is a Sliding Window Protocol used for reliable, in-order delivery over
noisy channels. It resends only damaged or lost frames, identified by NACKs from the
receiver. Frames are buffered for future use. Sender sets timers for each frame and
retransmits on timer expiry or NACK receipt. ACK and NACK include frame sequence
numbers. Receiver sorts frames by sequence numbers. Sender searches for lost frames
identified by NACKs.

Components of Selective Repeat ARQ include:


1. Sender: Initiates data transmission, sets timers for each frame, and responds to
NACKs by retransmitting specific frames.
2. Receiver: Receives frames, buffers correctly received frames, sends ACKs for correct
frames, and sends NACKs for lost or damaged frames.
3. ACK (Acknowledgment): Sent by the receiver to confirm correct receipt of a frame.
4. NACK (Negative Acknowledgment): Sent by the receiver to indicate a lost or damaged
frame, prompting the sender to retransmit it.

5. Timer: Set by the sender for each transmitted frame to detect timeouts and trigger
retransmissions.
6. Sequence Numbers: Assigned to each frame to enable identification of lost frames
and facilitate sorting at the receiver's end.

7. Buffer: Used by the receiver to store received frames for sorting and eventual
processing.

CODE
#include<iostream>

int tmp1, tmp2, tmp3, tmp4, tmp5, i, windowsize, noofPacket, morePacket;

using namespace std;

int receiver(int);

int simulate(int);

int negack(int);
int main() {

cout << "Number of frames: ";

cin >> noofPacket;

cout << "Window Size: ";

cin >> windowsize;

for (int i = 0; i < 10; i++)

rand();

noofPacket = rand() % 10;

morePacket = noofPacket;

while (morePacket >= 0) {

tmp1 = simulate(windowsize);

windowsize -= tmp1;

tmp4 += tmp1;

if (tmp4 > noofPacket)

tmp4 = noofPacket;

for (i = noofPacket - morePacket; i <= tmp4; i++)

cout << "\nSending Frame " << i;

tmp2 = receiver(tmp1);

tmp3 += tmp2;

if (tmp3 > noofPacket)

tmp3 = noofPacket;

tmp2 = negack(tmp1);

tmp5 += tmp2;

if (tmp5 != 0) {

cout << "\nNo acknowledgement for the frame " << tmp5;

cout << "\nRetransmitting frame " << tmp5;

morePacket -= tmp1;

if (windowsize <= 0)

windowsize = 4;

cout << "\n Selective Repeat Protocol Ends. All packets are successfully transmitted.";

}
int receiver(int tmp1) {

int i;

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

rand();

i = rand() % tmp1;

return i;

int negack(int tmp1) {

int i;

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

rand();

i = rand() % tmp1;

return i;

int simulate(int windowsize) {

int tmp1, i;

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

tmp1 = rand();

if (tmp1 == 0)

tmp1 = simulate(windowsize);

i = tmp1 % windowsize;

if (i == 0)

return windowsize;

else

return tmp1 % windowsize;

}
RESULT
Input:
• Data frames to be transmitted.

• Window size.
• Timeout period for the timer.
Output:
• Success message for successful
transmission.
• Resend the message if the frame is lost.
• Timeout message if acknowledgement is
lost.

You might also like