EC692 Codes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

EXP - 7  Stop & Wait ARQ

import random

i=1
frames = int(input("Enter the number of frames: "))
while(i<=frames):
print(f"Frame f{i} is sent")
ack = ["Acknowledgement Received", "No Acknowledgement Received"]
index = random.randint(0, len(ack) - 1)
if(index == 0):
print(f"Acknowledgement {i+1} received")
i += 1
else:
print("No Acknowledgement Received\nTime out...Resend")

EXP - 8  Cyclic Redundancy Check (CRC)

def modulo_2_division(dividend,divisor):
dividend = list(dividend)
divisor_length = len(divisor)
q = ''
# Perform modulo 2 division
for i in range(len(dividend) - divisor_length + 1):
q += dividend[i]
if dividend[i] == '1':
for j in range(divisor_length):
dividend[i + j] = '0' if dividend[i + j] == divisor[j] else '1'
# Return quotient & remainder
return q, ''.join(dividend[-divisor_length + 1:])

# TRANSMITTER SIDE
message = input("Enter the message input to be sent:- ")
gen_poly = input("Enter the key/ generator polynomial:- ")
k = len(gen_poly)
codeword = message + "0"*(k-1)
tx_quotient,tx_remainder = modulo_2_division(codeword,gen_poly)
print("Quotient:", tx_quotient)
print("Remainder:", tx_remainder)
tx_message = message + tx_remainder
print("Transmitted message:", tx_message,"\n")

# RECEIVER SIDE
rx_message = input("Enter the received message:- ")
rx_quotient,rx_remainder = modulo_2_division(rx_message,gen_poly)
print("Quotient:", rx_quotient)
print("Remainder:", rx_remainder)
print("Error Detected" if any(char == '1' for char in rx_remainder) or len(tx_message) != rx_message else "No
Error")

EXP – 4B  UDP Client


import socket
local_ip = '127.0.0.1'
local_port = 3000
udp = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
udp.bind((local_ip, local_port))
print('UDP Server Up and Listening...')
while(True):
print('Ready to Send...')
udp.sendto(bytes(input(), 'utf-8'), ('127.0.0.1', 5677)) # Unicode Transformation Format (8 bits) --> utf-8
print('Ready to Receive...')
data, address = udp.recvfrom(1024)
print(data.decode('utf-8') + ' from ' + str(address))

EXP – 4B  UDP Server

import socket
local_ip = '127.0.0.1'
local_port = 5677
udp = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
udp.bind((local_ip, local_port))
while(True):
print('Ready to Receive...')
data, address = udp.recvfrom(1024)
print(data.decode('utf-8') + ' from ' + str(address))
print('Ready to Send...')
udp.sendto(bytes(input(), 'utf-8'), ('127.0.0.1', 3000)) # Unicode Transformation Format (8 bits) --> utf-8

You might also like