Need for Byte-Stuffing
In variable-size framing at the data link layer, we need to define a way to separate one frame from the next. Byte stuffing is employed to accomplish the task. In byte stuffing an 8-bit flag (‘F’) is added at the beginning and at the end of the frame, thereby distinguishing one frame from the next. Therefore, every time a flag sequence (‘F’) is encountered, it signifies the beginning or end of a frame. This, ingenious scheme, however would give rise to a discrepancy, if the flag pattern (‘F’) would occur within the data carried by the frame itself. Byte stuffing comes to the rescue here, by stuffing the original data with an extra 8-bit escape sequence (‘E’) before the flag pattern, whenever it occurred within the data carried by a frame. The receiver would then have to de-stuff the escape sequence, in order to obtain the original data.
A simple question that might arise at this juncture is, what if the escape sequence (‘E’) formed a part of the data to be sent! This scenario is handled in exactly the same way as described above, i.e an extra 8-bit escape sequence (‘E’) is added to the original data before the escape sequence that formed part of the data.
In character-oriented protocols, where data to be carried are 8-bit characters, byte stuffing is employed to handle the problems discussed above.
To make things simpler we will consider only three types of byte sequences in the sent data, as :
F : Flag Sequence
E : Escape Sequence
D : Any other Data Sequence
For Example :
At Sender Side Enter the Message to be Sent : DDEDFFDE The data being sent (with byte stuffed) is : FDDEEDEFEFDEEF Seding Message.... Thanks for the Feedback Server!! At Receiver Side Message Recevied...Successfully!!! The Stuffed Message is : FDDEEDEFEFDEEF The Destuffed Message is : DDEDFFDE Messaging is over.....EXITING
From the above example we can see how the original data is recovered at the receiver end.
At Sender (Client) Side
- Data of each frame at the sender side, is first stuffed with 8-bit flag sequence (‘F’) at the beginning and end of each frame.
- Next, the data is scanned to see if any similar flag sequence (‘F’) forms a part of it or not. If yes, then before each such flag sequence, an extra escape sequence (‘E’) is stuffed.
- Now, if any similar escape sequence (‘E’) is found to form a part of the data to be sent, then an extra escape sequence (‘E’) is stuffed before the occurrence of each such escape sequence.
- Finally, this stuffed data is sent by the sender.
- The receiver skips over the first and last bytes of data received, as they are merely for signalling the beginning and end of one frame, respectively and does not carry any useful data.
- From the next byte on wards data is scanned and if two escape sequences (‘E’) are found in succession, the first one is de-stuffed. Similarly, if an escape sequence is followed by a flag sequence (‘F’), the former is de-stuffed.
- This strategy helps the receiver recover the actual sent data, accurately.
- Difference between Byte stuffing and Bit stuffing
- Bit Stuffing error detection technique using Java
- Implementing Checksum Using Java
- Implementing Generic Graph in Java
- Implementing a Linked List in Java using Class
- Bit Stuffing in Computer Network
- Java Applet | Implementing Flood Fill algorithm
- Byte byteValue() method in Java with examples
- How to Convert a Byte value to String value in Java with Examples
- How to Convert a String value to Byte value in Java with Examples
- Byte shortValue() method in Java with examples
- Implementing Salting
- Output of Java Programs | Set 55 (Java Collections Framework)
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Sorting in Java
- Min Heap in Java
- MD5 hash in Java
- Hashing in Java
At Receiver (Server) Side
At Sender (Client) Side
At Receiver (Server) Side
The Server (Receiver) should start running prior to the Client (Sender). This is essential because the client needs an open connection (socket) to send the data and this connection is opened by the server.
At Client (Sender) Side Enter the Message to be Sent : DFEDDFED The data being sent (with byte stuffed) is : FDEFEEDDEFEEDF Seding Message.... Thanks for the Feedback Server!! At Server (Receiver) Side Message Recevied...Successfully!!! The Stuffed Message is : FDEFEEDDEFEEDF The Destuffed Message is : DFEDDFED Messaging is over.....EXITING
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.