Feb 12 2014
Byte Stuffing Sender Receiver
Byte stuffing is framing technique in which we add special characters (called flag) to distinguish beginning and end of a frame. This technique only works with text data.
In above technique there is problem when flag encounter as data. To overcome this problem we again introduce new special character (called Escape). The escape characters have a predefined pattern. The receiver removes the escape character and keeps the data part. It cause to another problem, if the text contains escape characters as part of data. To deal with this, an escape character is prefixed with another escape character.
Byte Stuffing Sender Receiver Example
bytestuffsen.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 | //Named Piped Byte Stuff 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 #define FLAG '$' //Flag character for starting and ending of frame #define ESC '#' //Escape character for escaping flag int main() { char *myfifo = "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 stored in this variable int i=0,j=0; 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); //Byte Stuff Start smsg[j++] = FLAG; //Put FLAG in starting of stuff message for(i=0;msg[i]!='\0';i++) { if(msg[i] == FLAG || msg[i] == ESC) //If message character is FLAG or ESC { smsg[j++] = ESC;//Escape it } smsg[j++] = msg[i]; } smsg[j++] = FLAG;//Put Flag in ending of stuff message smsg[j] = '\0'; //End ths Stuffed message //Byte Stuff 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; } |
bytestufrec.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 | //Named Piped Byte Stuff 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 #define FLAG '$' #define ESC '#' int main() { char *myfifo = "myfifo"; //FIFO File Path int fp; //Open Pipe Return Value char msg[MAX]; //Stuffed Message received from Sender char dmsg[MAX]; //Original Message after De-Stuff int i=0,j=0; //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 : %s",msg); //Byte De-Stuff Start for(i=1;strlen(msg)-1;i++)//We start loop with 1 because want to ignore first FLAG { if(msg[i] == FLAG || msg[i] == ESC) { i++; } dmsg[j++] = msg[i]; } dmsg[j] = '\0'; //Byte De-Stuff End printf("\nDestuffed Message : %s",dmsg); //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; } |