Python – Stop & Wait Implementation using CRC
Stop and wait protocol is an error control protocol, in this protocol the sender sends data packets one at a time and waits for positive acknowledgment from the receiver’s side, if acknowledgment is received then the sender sends the next data packet else it’ll resend the previous packet until a positive acknowledgment is not received.
Note: To get more info on what is stop and wait protocol, refer Stop and Wait ARQ article.
CRC aka Cyclic redundancy check is an error detection mechanism, its procedure is as follows.
- Choose a generator polynomial mutually agreed upon by the sender and receiver, let k be the number of bits in the key obtained from this polynomial.
- Append (k – 1) 0’s to the right of the actual binary data.
- Divide the data obtained in step 2 by the key, and store the remainder.
- Append the remainder to the actual binary data, and send the data packet.
- Divide the received data bits by the key.
- If the remainder is non-zero, then the data is corrupted.
In this article, we will implement stop and wait algorithm such that the sender computes error detecting code using CRC and sends the data along with error detecting code to the receiver using socket. The receiver, after ensuring an error-free frame, saves the data.
The procedure will be as follows:
- Read input from a file, n characters at a time.
- Convert it into binary
- Generate its CRC.
- Send data + CRC bits to the receiver, but before sending, randomly introduce error
- Receive data packet.
- Determine if it is error-free. If yes extract data bits, convert them into character form, and save them into the output file. Send Ack as OK. If not, send NAK.
- If OK is received, proceed with the next n characters. Otherwise, if NAK is received, send the data + CRC bits again to the receiver after randomly introducing the error.
Final outcome: Input and output files should match. A log file should show how many frames were in error and how many retries were done for each frame.
Error is introduced as follows :
- Generate a random number, say r1. Perform r1 % 2. If you get a 0 do not introduce error and send original bits. If you get a 1, introduce error.
- To decide which bit will be in error, generate another random number, say r2. Perform r2 %(size of received frame). Assume you get a value i as the outcome. Flip the i-th bit. Now send it to the receiver.
Save this file as error_gen.py.
Initial : 1001010 Final : 0001010
Modulo two Division
Modulo two-division is required to calculate the CRC. Explaining the working of this code is beyond the scope of this article, the curious ones can refer Modulo two division.
Save this file as calc.py.
10010101 % 011010 = 01001
These are some constants that are shared by the receiver and the sender save this file as configuration.py.
The client class will have five methods.
- Constructor: To connect to the server using a socket at the given IP address and port number.
- asciiToBin: To convert ASCII string to binary string.
- appendZero: To append (k – 1) 0’s to the end of binary data.
- encode: To generate and append CRC at the end of actual data bits.
- sendfile :
- This method reads n characters from the input file at a time &
- Creates the data packet to be sent by calling the encode method.
- Calls induce_error method to randomly introduce an error in the data packet.
- Sends the data packet and waits for the acknowledgment.
- If the acknowledgment received is positive, then move on to the next n bits,
- else resend the current data packet.
- When the file is completely read, then send a flag to tell the receiver to stop waiting for the next frame.
- Terminate the session.
Save this file as CLIENT.py.
The server class will have six methods.
- Constructor: To listen for client request at the given IP address and port number.
- iszero : To determine if a string represents 0.
- isCurrupted : To determine if the received data is corrupted by performing modulo 2 division by the CRC_GENERATOR.
- decode : To extract the data bits from the received data packet and convert them to their ASCII values.
- log : To log the entry of each frame in the logfile.
- receive_file :
- This method receives the data packet from the sender &
- Checks its validity by calling the isCurrupted function.
- If the data packet is valid, then it decodes it and copies it in a file at server’s end and sends a positive acknowledgement to the sender and logs the entry of the data packet in the logfile.
- else, it sends a negative acknowledgement to the sender.
- If the received data packet is the end of file, then it terminates all the connections and returns.
Save this file as SERVER.py
To see the execution of the code, keep all the files in a single directory, and execute the server file before executing the client’s file. Also, make sure that you have the input text file and its correct path.