Character Count: In the character count method, the sender includes a count of the characters in each frame as a header field. The receiver knows the frame length by reading this count and can extract the data accordingly. For example, if a frame has the data "HELLO," the sender will add a header field with the value "5" to indicate the frame length. The receiver reads this count and knows that it needs to extract the next five characters as the frame.
#include stdio.hCharacter Stuffing: Character stuffing is a framing method that allows data frames to contain special characters by adding a delimiter before and after the special characters. The delimiter is a unique character sequence that is not present in the data itself. When the sender encounters a special character in the data, it adds the delimiter before and after the special character. The receiver looks for the delimiter to identify the start and end of each frame and removes the delimiter while extracting the data. This ensures that the special characters within the data do not interfere with the framing process.#include string.h #define MAX_FRAME_SIZE 100 void sendFrame(const char* data) { int length = strlen(data); printf("Sending frame: %d%s\n", length, data); } void receiveFrame(const char* frame) { int length = 0; sscanf(frame, "%d", <length); const char* data = frame + sizeof(int); printf("Received frame: %d%s\n", length, data); } int main() { const char* message = "HELLO"; sendFrame(message); char frame[MAX_FRAME_SIZE]; sprintf(frame, "%d%s", strlen(message), message); receiveFrame(frame); return 0; }
#include <stdio.h>
#include <string.h>
#define MAX_FRAME_SIZE 100
const char FLAG = '#'; // Delimiter character
void sendFrame(const char* data) {
char stuffedFrame[MAX_FRAME_SIZE * 2]; // Buffer for stuffed frame
int index = 0;
stuffedFrame[index++] = FLAG; // Start the frame with the delimiter
for (int i = 0; i < strlen(data); i++) {
if (data[i] == FLAG) { // If the character is the delimiter
stuffedFrame[index++] = FLAG; // Add an extra delimiter before it
}
stuffedFrame[index++] = data[i]; // Add the character to the frame
}
stuffedFrame[index++] = FLAG; // End the frame with the delimiter
stuffedFrame[index] = '\0'; // Add null terminator
printf("Sending frame: %s\n", stuffedFrame);
}
void receiveFrame(const char* frame) {
char unstuffedFrame[MAX_FRAME_SIZE];
int index = 0;
// Skip the first delimiter
for (int i = 1; i < strlen(frame) - 1; i++) {
if (frame[i] == FLAG && frame[i + 1] == FLAG) {
i++; // Skip the extra delimiter
}
unstuffedFrame[index++] = frame[i];
}
unstuffedFrame[index] = '\0'; // Add null terminator
printf("Received frame: %s\n", unstuffedFrame);
}
int main() {
const char* message = "H#ELLO#"; // Message with special characters
sendFrame(message);
receiveFrame("#H##ELLO##");
return 0;
}
Bit stuffing is a framing method that operates at the bit level. It
involves adding extra bits to the data to ensure that a specific bit
pattern does not appear within the frame, except as a delimiter. The
most common use of bit stuffing is the HDLC (High-Level Data Link
Control) protocol. In bit stuffing, a predefined bit pattern (known as
the flag) is used as the frame delimiter. Whenever the sender encounters
the flag pattern in the data, it adds an extra bit (0) after five
consecutive 1s. The receiver reads the data and removes the extra 0
after five consecutive 1s while extracting the frame. This ensures that
the flag pattern appears only as a delimiter and not within the data.
#include <stdio.h>
#include <string.h>
#define MAX_FRAME_SIZE 100
const char FLAG = '0'; // Flag bit pattern
const int MAX_CONSECUTIVE_ONES = 5; // Maximum consecutive ones before stuffing
void sendFrame(const char* data) {
char stuffedFrame[MAX_FRAME_SIZE * 2]; // Buffer for stuffed frame
int index = 0;
int consecutiveOnes = 0;
stuffedFrame[index++] = FLAG; // Start the frame with the flag pattern
for (int i = 0; i < strlen(data); i++) {
if (data[i] == '1') {
consecutiveOnes++; // Count consecutive ones
}
else {
consecutiveOnes = 0; // Reset consecutive ones counter
}
stuffedFrame[index++] = data[i]; // Add the bit to the frame
if (consecutiveOnes == MAX_CONSECUTIVE_ONES) {
stuffedFrame[index++] = '0'; // Stuff a zero bit
consecutiveOnes = 0; // Reset consecutive ones counter
}
}
stuffedFrame[index++] = FLAG; // End the frame with the flag pattern
stuffedFrame[index] = '\0'; // Add null terminator
printf("Sending frame: %s\n", stuffedFrame);
}
void receiveFrame(const char* frame) {
char unstuffedFrame[MAX_FRAME_SIZE];
int index = 0;
int consecutiveOnes = 0;
// Skip the first flag pattern
for (int i = 1; i < strlen(frame) - 1; i++) {
unstuffedFrame[index++] = frame[i]; // Add the bit to the unstuffed frame
if (frame[i] == '1') {
consecutiveOnes++; // Count consecutive ones
}
else {
consecutiveOnes = 0; // Reset consecutive ones counter
}
if (consecutiveOnes == MAX_CONSECUTIVE_ONES && frame[i + 1] == '0') {
i++; // Skip the stuffed zero bit
consecutiveOnes = 0; // Reset consecutive ones counter
}
}
unstuffedFrame[index] = '\0'; // Add null terminator
printf("Received frame: %s\n", unstuffedFrame);
}
int main() {
const char* message = "110111110"; // Message with bit stuffing
sendFrame(message);
receiveFrame("011011110100111101110"); // Received frame with stuffed zero bit
return 0;
}
If you get any error in above programs, please comment below