Bit stuffing using Java | Computer Networks

Prerequisites:
1. Socket programming in Java
2. Bit Stuffing
3. Framing in data Link Layer

Data is encapsulated in frames in the data link layer and sent over the network. Bit Stuffing is a error detection technique.

The idea used is very simple. Each frame begins and ends with a special bit pattern “01111110” which is the flag byte. Whenever the sender’s data link layer encounters five consecutive 1s in the data, it automatically stuffs a 0 bit in the outgoing bit stream. The bit stuffing is analogous to byte stuffing, in which an escape byte is stuffed into the ongoing character stream before a flag byte in the data.

When the receiver sees five consecutive 1 bits, followed by a 0 bit, it automatically destuffs(deletes) the 0 bit. Bit stuffing is completely transparent to the network layer in both sender and receiver computers.

With bit stuffing, the boundary between the two frames can be unambiguously recognized by the flag pattern. Thus, if the receiver loses track of where it is, all it has to do is scan the input for flag sequences., since they can only occur at frame boundaries and never within the data.



Illustrative Examples

Sender Side(Client):
User enters a binary data as input.
Enter data: 
0000001
Data is stuffed and sent to the reciever for unstuffing. 
Here server is the receiver.
Data stuffed in client: 01111110000000101111110
Sending to server for unstuffing

Receiver Side(Server):
Receiver receives the stuffed data. 
Stuffed data from client: 01111110000000101111110
Reciever has to unstuff the input data from sender and get the original data which 
was given as input by the user.
Unstuffed data: 
0000001

The code implementation of the above logic is given below.
At Sender side(client side):

package bitstuffing;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class BitStuffingClient {
    public static void main(String[] args) throws IOException
    {
        // Opens a socket for connection
        Socket socket = new Socket("localhost", 6789);

        DataInputStream dis = new DataInputStream(socket.getInputStream());
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

        // Scanner class object to take input
        Scanner sc = new Scanner(System.in);

        // Takes input of unstuffed data from user
        System.out.println("Enter data: ");
        String data = sc.nextLine();

        int cnt = 0;
        String s = "";
        for (int i = 0; i < data.length(); i++) {
            char ch = data.charAt(i);
            if (ch == '1') {

                // count number of consecutive 1's
                // in user's data
                cnt++;

                if (cnt < 5)
                    s += ch;
                else {

                    // add one '0' after 5 consecutive 1's
                    s = s + ch + '0';
                    cnt = 0;
                }
            }
            else {
                s += ch;
                cnt = 0;
            }
        }

        // add flag byte in the beginning
        // and end of stuffed data
        s = "01111110" + s + "01111110";

        System.out.println("Data stuffed in client: " + s);
        System.out.println("Sending to server for unstuffing");
        dos.writeUTF(s);
    }
}

At Receiver side(server side):

package bitstuffing;
import java.io.*;
import java.net.*;
public class BitStuffingServer {
    public static void main(String[] args) throws IOException
    {
        ServerSocket skt = new ServerSocket(6789);

        // Used to block until a client connects to the server
        Socket socket = skt.accept();

        DataInputStream dis = new DataInputStream(socket.getInputStream());
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

        // Receiving the string from the client which
        // needs to be stuffed
        String s = dis.readUTF();
        System.out.println("Stuffed data from client: " + s);

        System.out.println("Unstuffed data: ");
        int cnt = 0;

        // removal of stuffed bits:
        // start from 9th bit because the first 8
        //  bits are of the special pattern.
        for (int i = 8; i < s.length() - 8; i++) {
            char ch = s.charAt(i);
            if (ch == '1') {
                cnt++;
                System.out.print(ch);

                // After 5 consecutive 1's one stuffed bit
                //'0' is added. We need to remove that.
                if (cnt == 5) {
                    i++;
                    cnt = 0;
                }
            }
            else {

                // print unstuffed data
                System.out.print(ch);

                // we only need to maintain count 
                // of consecutive 1's
                cnt = 0;
            }
        }
        System.out.println();
    }
}

The input and output are as shown above.




Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.