📜  使用Java的 Bit Stuffing 错误检测技术

📅  最后修改于: 2022-05-13 01:55:00.479000             🧑  作者: Mango

使用Java的 Bit Stuffing 错误检测技术

先决条件:
一、 Java中的Socket编程
2. 位填充
3. 数据链路层的成帧

数据被封装在数据链路层的帧中并通过网络发送。位填充是一种错误检测技术。

使用的想法非常简单。每个帧都以一个特殊的位模式“01111110”开始和结束,这是标志字节。每当发送方的数据链路层在数据中遇到五个连续的 1 时,它会自动在传出的比特流中填充一个 0 位。位填充类似于字节填充,其中在数据中的标志字节之前将转义字节填充到正在进行的字符流中。

当接收器看到五个连续的 1 位,然后是一个 0 位时,它会自动去填充(删除)0 位。位填充对于发送方和接收方计算机中的网络层是完全透明的。

通过位填充,可以通过标志模式明确识别两个帧之间的边界。因此,如果接收器忘记了它的位置,它所要做的就是扫描输入中的标志序列。因为它们只能出现在帧边界,而永远不会出现在数据中。

Illustrative Examples

Sender Side(Client):
User enters a binary data as input.
Enter data: 
0000001
Data is stuffed and sent to the receiver 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
Receiver has to unstuff the input data from sender and get the original data which 
was given as input by the user.
Unstuffed data: 
0000001

下面给出上述逻辑的代码实现。
在发送方(客户端):

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);
    }
}

在接收方(服务器端):

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();
    }
}

输入输出如上图所示。