0% found this document useful (0 votes)
2 views5 pages

Practical

The document outlines various simulations for error detection and data transmission protocols, including Checksum, CRC, Stop & Wait, Go-Back-N, and Selective Repeat. Each section describes the objective, theory, algorithm or working, and provides corresponding Python code for implementation. These protocols are essential for ensuring reliable communication in data transmission systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Practical

The document outlines various simulations for error detection and data transmission protocols, including Checksum, CRC, Stop & Wait, Go-Back-N, and Selective Repeat. Each section describes the objective, theory, algorithm or working, and provides corresponding Python code for implementation. These protocols are essential for ensuring reliable communication in data transmission systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

🔹 1.

Simulation of Checksum Algorithm


✳️Objective:

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:

1. Divide the message into equal segments (e.g., 8 bits).


2. Add all segments using 1's complement arithmetic.
3. Complement the final sum to obtain the checksum.
4. Send message + checksum.
5. At the receiver’s end, add all received segments (including checksum).
6. If the final result is all 1s, the message is considered error-free.

✳️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:

1. Represent message and generator as binary strings.


2. Append n-1 zeros to the message (n = length of generator).
3. Perform binary division (XOR operation) using the generator.
4. Append the remainder (CRC) to the original data.
5. Receiver performs the same division to detect error.

✳️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)

def crc(data, divisor):


n = len(divisor)
temp = data + '0'*(n-1)
while len(temp) >= n:
if temp[0] == '1':
temp = xor(divisor, temp[:n]) + temp[n:]
else:
temp = xor('0'*n, temp[:n]) + temp[n:]
return temp.zfill(n-1)

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

🔹 3. Simulation of Stop & Wait Protocol


✳️Objective:

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:

 Sender sends one frame.


 Waits until ACK is received.
 On timeout, resend the frame.
 Receiver sends ACK for every correct frame.

✳️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()

🔹 4. Simulation of Go-Back-N Protocol


✳️Objective:

To simulate Go-Back-N protocol with N=3 sliding window.


✳️Theory:

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

🔹 5. Simulation of Selective Repeat Protocol


✳️Objective:

To simulate Selective Repeat protocol using a sliding window.

✳️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}")

print("All frames successfully received.")

selective_repeat()

You might also like