Practical
Practical
To simulate the working of the Checksum algorithm for error detection in data transmission.
✳️Theory:
The Checksum is a basic method for detecting errors in a message. It works by dividing the
message into fixed-sized segments, adding them up using 1’s complement arithmetic, and
sending the result (checksum) along with the data. At the receiver’s end, the same operation is
performed, and the result is verified. If the result is all 1s, no error is assumed.
✳️Algorithm:
✳️Python Code:
def calculate_checksum(data):
sum = 0
for word in data:
sum += word
sum = (sum & 0xFF) + (sum >> 8) # 8-bit carry around
checksum = ~sum & 0xFF
return checksum
# Sender
data = [0x12, 0x34, 0x56] # example data
checksum = calculate_checksum(data)
transmitted = data + [checksum]
print("Transmitted Data with Checksum:", transmitted)
# Receiver
received = transmitted
recheck = calculate_checksum(received)
if recheck == 0:
print("No Error Detected.")
else:
print("Error Detected!")
🔹 2. Simulation of CRC (Cyclic Redundancy Check)
✳️Objective:
To simulate the CRC algorithm used for error checking during data transmission.
✳️Theory:
CRC is a strong method for detecting errors in digital data using polynomial division. The sender
appends a remainder to the message after dividing it with a generator polynomial. The receiver
divides the received message by the same polynomial to check for errors.
✳️Steps:
✳️Python Code:
def xor(a, b):
result = []
for i in range(1, len(b)):
result.append(str(int(a[i]) ^ int(b[i])))
return ''.join(result)
data = "1101011011"
divisor = "10011"
remainder = crc(data, divisor)
codeword = data + remainder
print("Transmitted Codeword:", codeword)
# Receiver Side
check = crc(codeword, divisor)
if int(check) == 0:
print("No Error Detected.")
else:
print("Error Detected.")
To simulate the Stop & Wait protocol for reliable data transmission.
✳️Theory:
Stop & Wait is a flow control protocol where the sender transmits one frame at a time and waits
for an acknowledgment before sending the next frame.
✳️Working:
✳️Python Code:
import time
import random
def stop_and_wait():
frames = ['F1', 'F2', 'F3', 'F4']
for frame in frames:
print(f"Sending {frame}")
time.sleep(1)
if random.randint(0, 1):
print(f"{frame} Acknowledged\n")
else:
print(f"ACK lost for {frame}. Resending...\n")
time.sleep(1)
print(f"Resending {frame}")
print(f"{frame} Acknowledged\n")
stop_and_wait()
In Go-Back-N, multiple frames (up to window size) can be sent before needing ACKs. If an
error occurs, all subsequent frames are resent from the error point.
✳️Python Code:
def go_back_n():
total_frames = 7
window_size = 3
i = 0
while i < total_frames:
print(f"Sending frames: {list(range(i, min(i+window_size,
total_frames)))}")
ack = input(f"Acknowledge frame {i}? (y/n): ").lower()
if ack == 'y':
i += 1
else:
print("ACK lost. Resending from frame", i)
go_back_n()
✳️Theory:
Selective Repeat allows multiple frames to be sent. Only erroneous or lost frames are
retransmitted, reducing unnecessary retransmission.
✳️Python Code:
def selective_repeat():
total_frames = 5
received = [False] * total_frames
for i in range(total_frames):
success = input(f"Did you receive Frame {i}? (y/n): ").lower()
if success == 'y':
received[i] = True
print(f"ACK sent for Frame {i}")
else:
print(f"Frame {i} lost. Will retransmit later.")
for i in range(total_frames):
if not received[i]:
print(f"Retransmitting Frame {i}")
received[i] = True
print(f"ACK sent for Frame {i}")
selective_repeat()