0% found this document useful (0 votes)
27 views17 pages

WCN PBL Codes

The document contains a series of Python programs that implement various error detection and correction methods, including CRC, checksum, Hamming code, bit stuffing, and stop-and-wait protocols. Each program provides user-friendly input options and demonstrates how to generate codes, detect errors, and correct them. The document also includes implementations of ARQ protocols such as stop-and-wait and selective repeat.

Uploaded by

ssk276793
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)
27 views17 pages

WCN PBL Codes

The document contains a series of Python programs that implement various error detection and correction methods, including CRC, checksum, Hamming code, bit stuffing, and stop-and-wait protocols. Each program provides user-friendly input options and demonstrates how to generate codes, detect errors, and correct them. The document also includes implementations of ARQ protocols such as stop-and-wait and selective repeat.

Uploaded by

ssk276793
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/ 17

Question 1: Write a program to generate a code using CRC method with user-defined

data input (Python)


def generate_crc_code(data, divisor):
# Append zeros to the data to match the length of the divisor
data += '0' * (len(divisor) - 1)
data = list(data) # Convert data to a list for easy manipulation

for i in range(len(data) - len(divisor) + 1):


if data[i] == '1':
for j in range(len(divisor)):
data[i + j] = str(int(data[i + j]) ^ int(divisor[j]))

# The CRC code is the remainder after division


crc_code = ''.join(data[-(len(divisor) - 1):])
return crc_code

# Get user input for data and divisor


data_input = input("Enter the data (binary): ")
divisor_input = input("Enter the divisor (binary): ")

# Generate the CRC code


crc_code = generate_crc_code(data_input, divisor_input)
print("CRC Code:", crc_code)

Question 2: Write a program to detect an error at the receiver end using CRC method
(Python)
def detect_crc_error(received_data, divisor):
data = received_data

1
# Append zeros to the received data to match the length of the divisor
data += '0' * (len(divisor) - 1)
data = list(data) # Convert data to a list for easy manipulation

for i in range(len(data) - len(divisor) + 1):


if data[i] == '1':
for j in range(len(divisor)):
data[i + j] = str(int(data[i + j]) ^ int(divisor[j]))

# Check if there are any remaining '1's in the data


if '1' in data[-(len(divisor) - 1):]:
return "Error detected."
else:
return "No error detected."

# Get user input for the received data and the same divisor used for encoding
received_data_input = input("Enter the received data (binary): ")
divisor_input = input("Enter the same divisor used for encoding (binary): ")

# Detect errors using the CRC method


result = detect_crc_error(received_data_input, divisor_input)
print(result)

Q3 write a program to identifying the types of error like single bit error, multiple bit error and
burst error with user defined input. also what will be the output
def identify_error_type(received_data):
# Count the number of '1's in the received data

2
ones_count = received_data.count('1')

if ones_count == 1:
return "Single Bit Error"
elif ones_count > 1:
return "Multiple Bit Error"
else:
return "No Error (or Burst Error)"

# Get user input for the received data


received_data_input = input("Enter the received data (binary): ")

# Identify the type of error


error_type = identify_error_type(received_data_input)
print("Error Type:", error_type)

Question 4: Write a program to generate a code using the checksum method with user-
friendly input and user-defined data input (Python)
def generate_checksum(data):
# Calculate the checksum as the sum of ASCII values of characters in the data
checksum = sum(ord(char) for char in data)
return checksum

# Get user input for data


data_input = input("Enter the data: ")

# Generate the checksum


checksum = generate_checksum(data_input)

3
print("Checksum:", checksum)

Question 5: Write a program to detect an error at the receiver end using the checksum
method (Python)
def detect_checksum_error(received_data, received_checksum):
# Calculate the checksum from the received data
calculated_checksum = sum(ord(char) for char in received_data)

if calculated_checksum == received_checksum:
return "No error detected."
else:
return "Error detected."

# Get user input for the received data and received checksum
received_data_input = input("Enter the received data: ")
received_checksum_input = int(input("Enter the received checksum: "))

# Detect errors using the checksum method


result = detect_checksum_error(received_data_input, received_checksum_input)
print(result)

QUES6.>write a program to generate a code using Hamming method with user friendly input
with user defined input.
def generate_hamming_code(data):

4
# Calculate the number of parity bits needed (r)
r=0
while 2 ** r < len(data) + r + 1:
r += 1

# Initialize the Hamming code with parity bits as '?' (to be filled)
hamming_code = list(data) + ['?'] * r

# Calculate the values of parity bits (position at 2^k) using XOR


for i in range(r):
position = 2 ** i
parity = 0
for j in range(1, len(hamming_code) + 1):
if (j & position) == position and hamming_code[j - 1] == '1':
parity ^= 1
hamming_code[position - 1] = str(parity)

return ''.join(hamming_code)

# Get user input for data


data_input = input("Enter the data (binary): ")

# Generate the Hamming code


hamming_code = generate_hamming_code(data_input)
print("Hamming Code:", hamming_code)

QUES6.>write a program to generate a code using Hamming method with user friendly input
with user defined input. what will be the output
def generate_hamming_code(data):

5
# Calculate the number of parity bits needed (r)
r=0
while 2**r < len(data) + r + 1:
r += 1

# Initialize the Hamming code with parity bits as '0'


hamming_code = ['0'] * (len(data) + r)

# Fill in the data bits


data_index = 0
for i in range(len(hamming_code)):
if (i + 1) & i != 0: # Skip positions that are powers of 2 (parity bits)
hamming_code[i] = data[data_index]
data_index += 1

# Calculate the values of parity bits


for i in range(r):
position = 2**i
parity = 0
for j in range(position - 1, len(hamming_code), 2 * position):
for k in range(j, j + position):
if k < len(hamming_code) and hamming_code[k] == '1':
parity ^= 1
hamming_code[position - 1] = str(parity)

return ''.join(hamming_code)

# Get user-friendly input for data


data_input = input("Enter the data: ")

6
# Generate the Hamming code
hamming_code = generate_hamming_code(data_input)
print("Hamming Code:", hamming_code)

QUES7.> write a program to detect and correct an error at receiver end using Hamming
method. what will be the output
def detect_correct_hamming_error(received_data):
# Convert received data to a list
received_code = list(received_data)

# Calculate the number of parity bits (r)


r=0
while 2**r < len(received_code):
r += 1

# Initialize error positions


error_positions = []

# Calculate the values of parity bits


for i in range(r):
position = 2**i
parity = 0
for j in range(position - 1, len(received_code), 2 * position):
for k in range(j, j + position):
if k < len(received_code) and received_code[k] == '1':
parity ^= 1
if parity != 0:
error_positions.append(position)

7
# Correct the error
for position in error_positions:
if received_code[position - 1] == '0':
received_code[position - 1] = '1'
else:
received_code[position - 1] = '0'

# Remove parity bits and return the corrected data


corrected_data = ''.join([bit for i, bit in enumerate(received_code) if (i + 1) & i != 0])

return corrected_data

# Get user input for received data


received_data_input = input("Enter the received data (Hamming code): ")

# Detect and correct errors using the Hamming method


corrected_data = detect_correct_hamming_error(received_data_input)
print("Corrected Data:", corrected_data)

Question 8: Write a program for stop-and-wait protocol, generating a code using the
Hamming method with user-friendly input and user-defined input.
import random

# Function to generate a Hamming code


def generate_hamming_code(data):
# Implementation of Hamming encoding

8
# This is a simplified example. In practice, you'd need to implement the Hamming
encoding algorithm.
hamming_code = data # Placeholder for simplicity
return hamming_code

# Function to simulate a receiver


def receiver(received_data):
# Simulate error by randomly flipping a bit
if random.random() < 0.1:
position = random.randint(0, len(received_data) - 1)
received_data = received_data[:position] + ('1' if received_data[position] == '0' else '0')
+ received_data[position + 1:]

# In practice, you would implement Hamming decoding here

return received_data

# Main function to simulate a sender and receiver using stop-and-wait


def main():
data = input("Enter data (binary): ")
sender_hamming_code = generate_hamming_code(data)

print(f"Sender: Sending data with Hamming code: {sender_hamming_code}")

# Simulate transmission (in a real network, you'd send data over a network)
received_data = sender_hamming_code

print(f"Receiver: Received data: {received_data}")

received_data = receiver(received_data)

9
print(f"Receiver: After error simulation: {received_data}")

print("Receiver: Acknowledgment sent back to sender")

if __name__ == "__main":
main()

Question 9: Implementation of data link layer framing method for bit stuffing in any
language.

def sender_bit_stuffing(data):
start_delimiter = '01111110' # Start-of-frame delimiter
end_delimiter = '01111110' # End-of-frame delimiter
stuffed_data = start_delimiter
stuffed_data += data.replace('011111', '0111110') # Bit stuffing: Replace '011111' with
'0111110'
stuffed_data += end_delimiter
return stuffed_data

def receiver_bit_unstuffing(stuffed_data):
# Remove start delimiter and end delimiter
data = stuffed_data[8:-8]
# Bit unstuffing: Replace '0111110' with '011111'
data = data.replace('0111110', '011111')
return data

10
# Main function to demonstrate bit stuffing and unstuffing
def main():
data = input("Enter data to be transmitted: ")
stuffed_data = sender_bit_stuffing(data)
print("Stuffed Data:", stuffed_data)

received_data = receiver_bit_unstuffing(stuffed_data)
print("Received Data:", received_data)

if __name__ == "__main__":
main()

Question 10: Implementation of stop-and-wait protocol in any language.

import time

# Simulated receiver

def receiver(received_frame):

# Simulate successful or failed reception

if received_frame:

return "ACK"

else:

return "NAK"

11
# Simulated sender

def sender(data):

frame_to_send = data

while frame_to_send:

print("Sender: Sending frame:", frame_to_send)

acknowledgment = receiver(frame_to_send)

if acknowledgment == "ACK":

print("Sender: Received ACK for frame:", frame_to_send)

frame_to_send = None # Move to the next frame or end the transmission

else:

print("Sender: Received NAK for frame:", frame_to_send)

print("Sender: Resending frame:", frame_to_send)

time.sleep(1) # Simulate a delay before resending

# Main function

def main():

data_to_send = input("Enter data to be transmitted: ")

sender(data_to_send)

if __name__ == "__main__":

main()

12
Question 11: Implementation of stop-and-wait ARQ protocol in any language.

import random

import time

# Simulated receiver

def receiver(expected_sequence_number, error_probability):

# Simulate the reception of frames with a chance of error

received_frame = random.choice([expected_sequence_number, None])

if received_frame == expected_sequence_number:

# Frame received successfully

return "ACK"

else:

# Frame received with an error or not received

if random.random() < error_probability:

# Simulate frame received with error

return "NAK"

else:

# Simulate frame not received

return None

# Simulated sender

def sender(data, error_probability):

13
sequence_number = 0

while sequence_number < len(data):

frame_to_send = (sequence_number, data[sequence_number])

print(f"Sender: Sending frame {sequence_number}: {frame_to_send[1]}")

acknowledgment = receiver(frame_to_send[0], error_probability)

if acknowledgment == "ACK":

print(f"Sender: Received ACK for frame {sequence_number}")

sequence_number += 1

elif acknowledgment == "NAK":

print(f"Sender: Received NAK for frame {sequence_number}")

print(f"Sender: Resending frame {sequence_number}: {frame_to_send[1]}")

else:

print(f"Sender: Frame {sequence_number} not acknowledged, resending:


{frame_to_send[1]}")

time.sleep(1) # Simulate a delay before resending

# Main function

def main():

data_to_send = input("Enter data to be transmitted: ")

error_probability = float(input("Enter probability of frame errors (0.0-1.0): "))

sender(data_to_send, error_probability)

if __name__ == "__main__":

main()

14
Question 12: Implementation of Selective Repeat ARQ protocol in any language.

import random

import time

WINDOW_SIZE = 3 # Set the window size for selective repeat

# Simulated receiver

def receiver(expected_sequence_number, error_probability):

# Simulate the reception of frames with a chance of error

received_frame = random.choice([expected_sequence_number, None])

if received_frame == expected_sequence_number:

# Frame received successfully

return "ACK"

else:

15
# Frame received with an error or not received

if random.random() < error_probability:

# Simulate frame received with error

return "NAK"

else:

# Simulate frame not received

return None

# Simulated sender

def sender(data, error_probability):

next_sequence_number = 0

while next_sequence_number < len(data):

# Initialize the window

window = [(i, data[i]) for i in range(next_sequence_number,


min(next_sequence_number + WINDOW_SIZE, len(data))]

for sequence_number, frame in window:

print(f"Sender: Sending frame {sequence_number}: {frame}")

acknowledgment = receiver(sequence_number, error_probability)

if acknowledgment == "ACK":

print(f"Sender: Received ACK for frame {sequence_number}")

elif acknowledgment == "NAK":

print(f"Sender: Received NAK for frame {sequence_number}")

else:

print(f"Sender: Frame {sequence_number} not acknowledged, resending:


{frame}")

16
time.sleep(1) # Simulate a delay before resending

next_sequence_number += 1

# Main function

def main():

data_to_send = input("Enter data to be transmitted: ")

error_probability = float(input("Enter probability of frame errors (0.0-1.0): "))

sender(data_to_send, error_probability)

if __name__ == "__main__":

main()

17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy