Client

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.

Python3




from calc import mod2div
from error_gen import err_gen
from configuration import *
import socket
 
 
class Client:
 
    def __init__(self, ipadd, portn):
        self.socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket_.connect((ipadd, portn))
 
    def asciiToBin(self, data):
       
        # ascii to bin.
        return bin(int.from_bytes(data.encode(), 'big'))
 
    def appendZero(self, message):
       
        # append n - 1 0's.
        message = (message.ljust(len(CRC_GENERATOR) - 1 + len(message), '0'))
        return message
 
    def encode(self, data):
 
        # convert ascii to bin
        message = self.asciiToBin(data)
        dividend = self.appendZero(message)
 
        # generate and append crc
        crc = mod2div(dividend, CRC_GENERATOR)
        curr_frame = (message + crc)
 
        return curr_frame
 
    def send_file(self, filename='file.txt'):
 
        f = open(filename)
        data = f.read(FRAME_SIZE)
 
        while len(data) > 0:
 
            # encode data
            curr_frame = self.encode(data)
 
            # induce error
            curr_frame = err_gen().induce_err(curr_frame)
 
            # send frame
            self.socket_.send(curr_frame.encode())
 
            # receive acknowledgement
            if self.socket_.recv(BUFFER_SIZE).decode() == 'OK':
                data = f.read(FRAME_SIZE)
 
        # Terminate session
        self.socket_.send(END_OF_FILE.encode())
        self.socket_.close()
        f.close()
        print("File sent")
 
 
newclient = Client(ipadd="127.0.0.1", portn=3241)
newclient.send_file(filename="file.txt")


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.

Similar Reads

Sender side

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....

Receiver side

Divide the received data bits by the key. If the remainder is non-zero, then the data is corrupted....

At sender

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...

At receiver:

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....

At sender

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....

Error Generation

Error is introduced as follows :...

Modulo two Division

...

Configuration Data

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....

Client

...

Server

These are some constants that are shared by the receiver and the sender save this file as configuration.py....