Jan 25 2014
Bit Stuffing Sender Receiver
In a bit-oriented protocol, the data to send is a series of bits. In order to distinguish frames, most protocols use a bit pattern of 8-bit length (01111110) as flag at the beginning and end of each frame. Here also cause the problem of appearance of flag in the data part to deal with this an extra bit added. This method is called bit stuffing. In bit stuffing, if a 0 and five successive 1 bits are encountered, an extra 0 is added. The receiver node removes the extra-added zero.
Simply, Bit stuffing is the process of adding one extra 0 whenever five consecutive 1s follow a 0 in the data. Byte stuffing is the method of adding 1 extra byte if there is a flag or escape character in the text.
Bit Stuffing Sender Receiver Example
bitstuffsen.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | //Named Piped BitStuffing Sender Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions #define MAX 100 //Buffer Size for Writing Message int main() { char *myfifo = "/tmp/myfifo"; //FIFO File Path int fp; //Open Pipe Return Value char msg[MAX]; //Original Message to be sent char smsg[MAX]; //Stuffed Message to be sent to Pipe int i=0,j=0; //Loop variable for traversal of text int cnt=0; //Five Consicutive 1 counter printf("Creating Pipe...Waiting for receiver for Process...\n"); //Creating Named Pipe //mkfifo(<filepath>,<permission>) if(mkfifo(myfifo,0666) < 0) { perror("FIFO (Named Pipe) could not be created.\n"); exit(-1); } else printf("FIFO (Named Pipe) Created...\n"); //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_WRONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened.\n"); exit(-2); } else printf("FIFO (Named Pipe) Opened...\n"); printf("Enter Message : "); gets(msg); /* BITSTUFF LOGIC START */ for(i=0;msg[i] != '\0';i++) { smsg[j++] = msg[i]; //Store every character into Stuffed Message if(msg[i] == '1') { cnt++; } else cnt = 0; //If 0 encounter then reset 1's counter if(cnt == 5) //If Five consicutive 1's encounter Stuf Extra Bit(0) { smsg[j++] = '0'; cnt = 0; //Again Set Counter to 0 } } smsg[j] = '\0'; /* BITSTUFF LOGIC END */ //Writing to Named Pipe //write(<open_pipe_return_value>,<message>,<msgsize>); if(write(fp,smsg,sizeof(smsg)) < 0) { perror("Error writing to FIFO (Named Pipe)\n"); exit(-3); } else printf("Message has been sent to FIFO (Named Pipe)\n"); //Closing Named Pipe //close(<open_pipe_return_value>); if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\n"); exit(-4); } return 0; } |
bitstuffrec.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | //Named Piped BitStuffing Receiver Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions #define MAX 100 //Buffer Size for Writing Message int main() { char *myfifo = "/tmp/myfifo"; //FIFO File Path int fp; //Open Pipe Return Value char msg[MAX]; //Stuffed Message Received From Sender char dmsg[MAX]; //Orignal Message After De-Stuff int i=0,j=0; //Loop Variable for trafersal of text int cnt=0; //Counter variable for Five Consicutive 1 //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_RDONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened for reading.\n"); exit(-1); } else printf("FIFO (Named Pipe) Opened... Trying to read from\n"); //Reading From Named Pipe //read(<open_pipe_return_value>,<message>,<msgsize>); if(read(fp,msg,sizeof(msg)) < 0) { perror("Error reading from FIFO (Named Pipe)\n"); exit(-2); } else printf("Message Received from FIFO (Named Pipe)\n"); printf("\nStuffed Message : "); puts(msg); /* * BIT DE-STUF LOGIC START */ for(i=0;i<strlen(msg);i++) { if(cnt == 5) //If Five consicutive 1's encounter Remove Bit(0) { cnt = 0; //Reset 1's counter continue; } if(msg[i] == '1') cnt++; else cnt=0;//If 0 encounter then reset 1's counter dmsg[j++] = msg[i];//Store character into De-Stuffed Message } dmsg[j] = '\0'; printf("\nOriginal Message : "); puts(dmsg); /* * BIT DE-STUF LOGIC END */ //Closing Named Pipe //close(<open_pipe_return_value>); if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\b"); exit(-3); } if(unlink(myfifo) < 0) { perror("Error deleting FIFO (Named Pipe)\b"); exit(-4); } return 0; } |