Server
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
Python3
import socket from calc import mod2div from configuration import * class Server: def __init__( self , ipaddr, portn): self .socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self .socket_.bind((ipaddr, portn)) self .socket_.listen( 5 ) def iszero( self , data): for x in data: if x ! = '0' : return False return True def isCurrupted( self , message): return not self .iszero(mod2div(message, CRC_GENERATOR)) def decode( self , message): message = message[: 1 - len (CRC_GENERATOR)] n = int (message, 2 ) return n.to_bytes((n.bit_length() + 7 ) / / 8 , 'big' ).decode() def log( self , loghandle, itr, received_frame, retrie_count): loghandle.write( "Frame Number : " + str (itr) + "\n" ) loghandle.write( "Frame Content : \"" + self .decode(received_frame) + "\"\n" ) loghandle.write( "Retries : " + str (retrie_count) + "\n\n" ) def receive_file( self , filepath, logpath): received_socket, addr = self .socket_.accept() f = open (filepath, 'w' ) l = open (logpath, 'w' ) itr = 1 retrie_count = 0 while 1 : received_frame = received_socket.recv(BUFFER_SIZE).decode() if received_frame = = END_OF_FILE: f.close() l.close() self .socket_.close() print ( "File Received" ) return if self .isCurrupted(received_frame): retrie_count + = 1 received_socket.send(REJECT.encode()) else : # Received file f.write( self .decode(received_frame)) # Log self .log(l, itr, received_frame, retrie_count) retrie_count = 0 received_socket.send(ACCEPT.encode()) newServer = Server(ipaddr = "127.0.0.1" , portn = 3241 ) newServer.receive_file(filepath = "received_data.txt" , logpath = "logfile.txt" ) |
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.
Execution:
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.