Cn fat lab: Crc: def calculate_crc(binary_input, divisor): binary_input = binary_input + "0" * (len(divisor) - 1) # Append zeros to the input dividend = list(binary_input) # Convert input to a list of characters divisor = list(divisor) # Convert divisor to a list of characters for i in range(len(binary_input) - len(divisor) + 1): if dividend[i] == "0": continue # Skip the iteration if the dividend bit is 0 for j in range(len(divisor)): dividend[i + j] = str((int(dividend[i + j]) ^ int(divisor[j]))) # Perform XOR # Return the CRC value crc = "".join(dividend)[-len(divisor) + 1:] return crc binary_input = "101110" divisor = "1101" crc = calculate_crc(binary_input, divisor) print("CRC:", crc) ``` In this code, the calculate_crc function takes two arguments: binary_input (the binary input string) and divisor (the divisor string). It first appends zeros to the input to accommodate for the length of the divisor. Then, it converts the input and divisor to lists of characters to facilitate manipulation. The function then performs the division using the CRC algorithm. It iterates through the binary input, skipping iterations when the dividend bit is 0. For each iteration, it performs the XOR operation between the corresponding bits of the dividend and the divisor. Finally, the function returns the CRC value by taking the remaining characters of the dividend after the division. In the example usage, the code demonstrates how to calculate the CRC for a given binary input string (101110) with a divisor string (1101). The calculated CRC value is then printed to the console. def calculate_crc(data): crc = 0xFFFFFFFF # initial value # CRC-32 polynomial (normal form) polynomial = 0xEDB88320 for byte in data: crc ^= byte for _ in range(8): if crc & 1: crc = (crc >> 1) ^ polynomial else: crc >>= 1 crc ^= 0xFFFFFFFF return crc data = b"Hello, world!" crc_value = calculate_crc(data) print("CRC-32 value:", hex(crc_value)) class: import ipaddress def classful_addressing(ip_address): try: network = ipaddress.ip_network(ip_address) network_class = network.network_address if network_class.is_private: print(f"The IP address {ip_address} belongs to a private network.") else: if network_class.is_multicast: print(f"The IP address {ip_address} belongs to a multicast address range.") else: if network_class.is_reserved: print(f"The IP address {ip_address} belongs to a reserved address range.") else: print(f"The IP address {ip_address} belongs to a public network.") if network_class.is_global: print(f"The IP address {ip_address} is a globally routable address.") else: print(f"The IP address {ip_address} is a non-globally routable address.") except ValueError: print(f"The IP address {ip_address} is not a valid IPv4 address.") def classless_addressing(ip_address, subnet_mask): try: network = ipaddress.ip_network(f"{ip_address}/{subnet_mask}", strict=False) print(f"The IP address {ip_address} with subnet mask {subnet_mask} belongs to the network {network.network_address}/{network.prefixlen}") if network.is_private: print(f"This is a private network.") else: if network.is_multicast: print(f"This is a multicast address range.") else: if network.is_reserved: print(f"This is a reserved address range.") else: print(f"This is a public network.") if network.is_global: print(f"This is a globally routable address.") else: print(f"This is a non-globally routable address.") except ValueError: print(f"The IP address {ip_address} or subnet mask {subnet_mask} is not valid.") classful_addressing('192.168.1.1') classful_addressing('10.0.0.1') classful_addressing('172.16.0.1') classful_addressing('224.0.0.1') classful_addressing('192.0.2.1') classless_addressing('192.168.1.1', '255.255.255.0') classless_addressing('10.0.0.1', '255.0.0.0') classless_addressing('172.16.0.1', '255.255.0.0') classless_addressing('224.0.0.1', '255.0.0.0') classless_addressing('192.0.2.1', '255.255.255.0') checksum: def calculate_checksum(data): checksum = 0 # Convert the input data to a string representation data_str = str(data) for char in data_str: checksum += ord(char) return checksum data = "Hello, world!" checksum_value = calculate_checksum(data) print("Checksum value:", checksum_value) binary: def calculate_checksum(data): checksum = sum(data) & 0xFF return checksum data = [0b11011001, 0b10101010, 0b11110000, 0b00001111] checksum_value = calculate_checksum(data) print("sender Checksum value:",checksum_value) data1 = [0b11011001, 0b10101010, 0b11110000, 0b00001111] def reciever_check_msg(data1): checksum1 = sum(data1) & 0xFF checksum_val = calculate_checksum(data) print("reciever Checksum value:",checksum1) chck=checksum1-checksum_val if(chck==0): print("valid checksum") else: print("invalid checksum") reciever_check_msg(data1) Hamming code: def hamming_encode(data): num_parity_bits = 4 num_data_bits = 4 parity_bit_positions = [1, 2, 4, 8] encoded_data = [] for i in range(num_data_bits): bit = data[i] encoded_data.append(bit) if (i+1) in parity_bit_positions: encoded_data.append(0) for i in range(num_parity_bits): parity_bit = parity_bit_positions[i] parity = 0 for j in range(len(encoded_data)): if (j+1) & parity_bit: parity ^= encoded_data[j] encoded_data[parity_bit - 1] = parity return encoded_data def hamming_decode(encoded_data): Calculate the number of parity bits used num_parity_bits = 4 num_data_bits = 4 parity_bit_positions = [1, 2, 4, 8] error_bit = 0 for i in range(num_parity_bits): parity_bit = parity_bit_positions[i] parity = 0 for j in range(len(encoded_data)): if (j+1) & parity_bit: parity ^= encoded_data[j] if parity != 0: error_bit += parity_bit if error_bit != 0: encoded_data[error_bit - 1] ^= 1 decoded_data = [] for i in range(num_data_bits): decoded_data.append(encoded_data[2**i - 1]) return decoded_data data = [1, 0, 1, 1] # Input data to be encoded encoded_data = hamming_encode(data) print("Encoded data:", encoded_data) decoded_data = hamming_decode(encoded_data) print("Decoded data:", decoded_data) tcp udp :java tcp_client: import java.io.; import java.net.; public class tcp_client { public static void main(String[] args) { String serverHost = "localhost"; int serverPort = 12345; try { // Create a socket connection to the server Socket clientSocket = new Socket(serverHost, serverPort); // Set up input and output streams BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true); // Read input from user BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Client: "); String message = inFromUser.readLine(); // Send message to server outToServer.println(message); // Receive response from server String response = inFromServer.readLine(); System.out.println("Server: " + response); // Close the socket clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } Tcp_server: import java.io.; import java.net.; public class tcp_server { public static void main(String[] args) { int serverPort = 12345; try { // Create a server socket ServerSocket serverSocket = new ServerSocket(serverPort); // Wait for client connection System.out.println("Waiting for client connection..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected!"); // Set up input and output streams BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true); // Receive message from client String message = inFromClient.readLine(); System.out.println("Client: " + message); // Read input from server BufferedReader inFromServer = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Server: "); String response = inFromServer.readLine(); // Send response to client outToClient.println(response); // Close the socket serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } UDPSERVER: import java.io.; import java.net.; public class UDPSender { public static void main(String[] args) { DatagramSocket socket = null; try { // Create a UDP socket socket = new DatagramSocket(); String message = "Hello, receiver!"; InetAddress receiverAddress = InetAddress.getLocalHost(); int receiverPort = 12345; // Convert the message to bytes byte[] buffer = message.getBytes(); // Create a UDP packet with the message and destination address DatagramPacket packet = new DatagramPacket(buffer, buffer.length, receiverAddress, receiverPort); // Send the packet socket.send(packet); System.out.println("Message sent to receiver: " + message); } catch (IOException e) { e.printStackTrace(); } finally { // Close the socket if (socket != null) { socket.close(); } } } } UDPRECEIVER: import java.io.; import java.net.; public class UDPReceiver { public static void main(String[] args) { DatagramSocket socket = null; try { // Create a UDP socket and bind it to a specific port socket = new DatagramSocket(12345); byte[] buffer = new byte[1024]; // Create a UDP packet to receive incoming messages DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // Receive the packet socket.receive(packet); String message = new String(packet.getData(), 0, packet.getLength()); System.out.println("Message received from sender: " + message); } catch (IOException e) { e.printStackTrace(); } finally { // Close the socket if (socket != null) { socket.close(); } } } } FLOWCONTROL: STOP AND WAIT import time def simulate_network_delay(): time.sleep(1) # Sleep for 1 second def sender(message): sequence_number = 0 ack_received = False while not ack_received: print("Sender: Sending frame with sequence number", sequence_number) simulate_network_delay() received_ack = True if (sequence_number % 2 == 0) else False if received_ack: print("Sender: ACK received for frame", sequence_number) ack_received = True else: print("Sender: ACK not received for frame", sequence_number) print("Sender: Resending frame with sequence number", sequence_number) simulate_network_delay() print("Sender: Message sent successfully!") def receiver(): sequence_number = 0 while True: received_frame = True if (sequence_number % 2 == 0) else False if received_frame: print("Receiver: Frame", sequence_number, "received") simulate_network_delay() print("Receiver: Sending ACK for frame", sequence_number) simulate_network_delay() sequence_number += 1 else: print("Receiver: Frame", sequence_number, "not received") print("Receiver: Resending ACK for frame", sequence_number - 1) simulate_network_delay() sender(message) receiver() SLIDING WINDOW : GO BACK import time def simulate_network_delay(): time.sleep(1) # Sleep for 1 second def sender(message): window_size = 3 sequence_number = 0 window_base = 0 packets = [] acknowledgements = [] for i in range(0, len(message), window_size): packets.append(message[i:i + window_size]) while window_base < len(packets): for i in range(window_base, min(window_base + window_size, len(packets))): print("Sender: Sending packet with sequence number", sequence_number, "and data", packets[i]) simulate_network_delay() received_ack = True if (sequence_number % 2 == 0) else False if received_ack: print("Sender: ACK received for packet with sequence number", sequence_number) acknowledgements.append(sequence_number) sequence_number += 1 window_base += 1 else: print("Sender: ACK not received for packet with sequence number", sequence_number) print("Sender: Resending packets in the current window") simulate_network_delay() print("Sender: Message sent successfully!") print("Sender: Acknowledgements received:", acknowledgements) def receiver(): window_size = 3 sequence_number = 0 window_base = 0 acknowledgements = [] while True: received_packet = True if (sequence_number % 2 == 0) else False if received_packet: print("Receiver: Packet with sequence number", sequence_number, "received") acknowledgements.append(sequence_number) simulate_network_delay() send_ack = True if (sequence_number % 2 == 0) else False if send_ack: print("Receiver: Sending ACK for packet with sequence number", sequence_number) simulate_network_delay() sequence_number += 1 else: print("Receiver: Packet with sequence number", sequence_number, "not received") print("Receiver: Resending ACK for packet with sequence number", window_base - 1) simulate_network_delay() if len(acknowledgements) == window_size: window_base += window_size acknowledgements.clear() message = "Hello, receiver!" sender(message) receiver() SLIDING WINDOW : SELECT REPEAT import time import random def simulate_network_delay(): time.sleep(1) # Sleep for 1 second def sender(message): window_size = 3 sequence_number = 0 window_base = 0 packets = [] acknowledgements = {} for i in range(0, len(message), window_size): packets.append(message[i:i + window_size]) while window_base < len(packets): for i in range(window_base, min(window_base + window_size, len(packets))): packet = packets[i] print("Sender: Sending packet with sequence number", sequence_number, "and data", packet) simulate_network_delay() if random.random() < 0.3: print("Sender: Packet with sequence number", sequence_number, "lost") continue acknowledgements[sequence_number] = False sequence_number += 1 received_ack = True if random.random() >= 0.3 else False if received_ack: ack_number = int(input("Sender: Enter the ACK number: ")) if ack_number in acknowledgements: acknowledgements[ack_number] = True print("Sender: ACK received for packet with sequence number", ack_number) simulate_network_delay() else: print("Sender: Invalid ACK number received:", ack_number) if all(acknowledgements.get(seq, False) for seq in range(window_base, window_base + window_size)): window_base += window_size acknowledgements.clear() print("Sender: Message sent successfully!") def receiver(): window_size = 3 sequence_number = 0 window_base = 0 acknowledgements = {} while True: received_packet = True if random.random() >= 0.3 else False if received_packet: packet = input("Receiver: Enter the packet with sequence number " + str(sequence_number) + ": ") if random.random() < 0.3: print("Receiver: Packet with sequence number", sequence_number, "received, but ACK not sent") else: print("Receiver: Packet with sequence number", sequence_number, "received") acknowledgements[sequence_number] = True simulate_network_delay() while window_base in acknowledgements: print("Receiver: Sending ACK for packet with sequence number", window_base) simulate_network_delay() window_base += 1 sequence_number += 1 else: print("Receiver: Packet with sequence number", sequence_number, "not received") print("Receiver: Resending ACK for packet with sequence number", window_base - 1) simulate_network_delay() message = "Hello, receiver!" sender(message)