Flag byte with byte stuffing framing

Byte stuffing framing

      Flag byte with byte stuffing is character-oriented framing. Therefore, the data to be carried is considered as 8-bit characters. While framing, the data link layer takes data from the network layer which is in the form of packets , and adds some control information in the form of headers and trailers.  Headers usually contain the source and destination’s address, and the trailer includes the control information for error detection and correction. The data link layer inserts frame delimiters between each frame i.e., start indicator and stop indicator for each frame.

 

     The important thing is what will be the indicator that will indicate the start of the frame and end of the frame?  Please note that as this method is a character-oriented framing method data link layer will choose the 8-bit character that is not usually used for text encoding to indicate the start of the frame and end of the frame. This indicator or this character has chosen to be used as a start indicator and the stop indicator is called a flag.  So, the flag is an 8-bit character chosen to be used as a frame delimiter which will indicate the start of the frame and end of the frame.

     To help you understand better let me use the above diagram to explain the concept better.  Assume that the data link layer received data in the form of a packet.  For simplicity, we assume that the data is ‘ABC’.  Now the data link layer puts this data in the wrapper, we call it a frame and add some control information in the form of header and trailer.  

Using Flag as Start and Stop indicator.

     While framing, the important thing is to insert a frame delimiter so that the receiver will correctly identify the boundaries of received frames.  The data link layer inserts 8-bit character code:  a flag at the beginning and at the end of each frame.  

     The first flag byte will be considered as the start of the frame then ABC as data and when another flag occurs it will be considered as the end of the frame.  The receivers’ data link layer always looks for a flag, when it occurs for the first time that will be considered as a beginning of a frame.  After that, if it occurs that will be treated as the end of the frame. If it occurs, one more time that will be considered as the beginning of the next frame. This is the way framing is done using a flag byte with the byte stuffing method.

What if Flag occurs in data?

     The flag byte is chosen such that it is not used for text encoding. But what if this framing method is used for transmitting multimedia files; there is always the possibility that this pattern which is used for representing the start indicator and stop indicator may be present in the data part.  It is because the flag byte a is an 8-bit sequence that may be representing some character.  When flag byte is present in the data part there will be a conflict.

     Suppose the data received from the network layer is a ‘flag’.

 

we can see flag which is chosen to be used as a delimiter occurs in the data.  Using this method data will be framed like this:

     The data link layer will insert the header and trailer and inserts a delimiter that is flag at the beginning of the frame and at the end of the frame.  Then this frame is transmitted by the sender’s data link layer. On the receiver side, the data link layer is always looking for a flag pattern. When it sees the first flag it knows it must be the start of the frame and reads character A.  But when ‘flag’ which is a data byte, occurs it will be considered as the end of the frame, and character C will not be accepted by the receiver’s data link layer. When another flag encounters it will be considered as the beginning of another frame, which is incorrect. So, this method fails to detect frame boundaries because there is confusion between the flag byte and data byte same as flag bye.

     To overcome this problem; the byte stuffing mechanism is used. In this mechanism, an indicator is inserted or stuffed in just before the data byte which is the same as the flag byte.  This stuffed in byte indicate that the data byte which follows is the same as the flag byte, do not consider it as a flag byte.  So, in the byte stuffing mechanism, a special character called ESC (escape) character is inserted or stuffed in just before every byte which is the same as a flag byte.

 

For example, if the data to be sent is A Flag B,  

     while framing one Flag at the beginning and one Flag at the end is inserted. Control information in the form of a header and trailer is added.

   Now, just before the data byte which is the same as the flag byte and escape (ESC) is stuffed in to indicate that the byte follows is the data byte and not the Flag byte.

     When this frame arrives at the receivers’ data link layer, the first flag indicates the beginning of the frame. The ESC i.e., escape character indicates that the byte follows is a data byte and not the flag byte. Therefore, the flag byte which follows ESC will be accepted as a data byte. Finally, the last flag will indicate the end of the frame.

What if ESC occurs in data?

     Now the question arises, what if ESC occurs in the data part or message part.  The answer is if the ESC (i.e., escape character) is found in the data part, one another ESC is inserted just before the escape character.

This inserted ESC will indicate the data byte, which follows is like an escape character but not an escape character. Whenever the receiver encounters an escape character, it removes it from the data and considers the next byte as a data byte and not the escape character or delimiting flag.  

Drawback:

The problem with this method is it adds too much overhead to the message by inserting too many escape characters. These stuffed-in characters will increase the size of the message.  Another problem is that the encoding systems used in recent times have 16-bit or 32-bit characters which conflicts with the 8-bit characters.  

error: Content is protected !!