Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Python by asffas12 ( 3 years ago )
def dotted_decimal_to_binary(dotted_decimal):
    # Split the dotted decimal address into its four octets
    octets = dotted_decimal.split('.')
    
    # Convert each octet to its binary representation and pad with zeros
    binary_octets = [bin(int(octet))[2:].zfill(8) for octet in octets]
    
    # Concatenate the binary octets and return the result
    binary_address = ''.join(binary_octets)
    return binary_address

def binary_to_dotted_decimal(binary_address):
    # Split the binary address into its four octets
    octets = [binary_address[i:i+8] for i in range(0, len(binary_address), 8)]
    
    # Convert each octet to its decimal representation
    decimal_octets = [str(int(octet, 2)) for octet in octets]
    
    # Join the decimal octets with dots and return the result
    dotted_decimal = '.'.join(decimal_octets)
    return dotted_decimal
# Convert a dotted decimal address to binary
dotted_decimal = '192.168.1.1'
binary_address = dotted_decimal_to_binary(dotted_decimal)
print(binary_address)  # Output: 11000000101010000000000100000001

# Convert a binary address to dotted decimal
binary_address = '11000000101010000000000100000001'
dotted_decimal = binary_to_dotted_decimal(binary_address)
print(dotted_decimal)  # Output: 192.168.1.1



import time
import random
# Sender function
def sender(data):
    # Initialize variables
    seq_num = 0
    ack_num = 0

    # Loop through each data packet
    for packet in data:
        # Send packet and wait for acknowledgement
        print(f"Sending packet {seq_num}: {packet}")
        ack_received = False
        while not ack_received:
            time.sleep(1)  # Simulate delay
            print(f"Waiting for acknowledgement from receiver for packet {seq_num}")
            ack_received = receiver(seq_num)
        seq_num = 1 - seq_num  # Flip sequence number

    # Send end-of-transmission packet
    print("Sending end-of-transmission packet")
    ack_received = False
    while not ack_received:
        time.sleep(1)  # Simulate delay
        print("Waiting for acknowledgement from receiver for end-of-transmission packet")
        ack_received = receiver(seq_num)

# Receiver function
def receiver(expected_seq_num):
    # Simulate random acknowledgement errors
    if random.random() < 0.2:
        print("Error: acknowledgement lost")
        return False

    # Simulate packet loss
    if random.random() < 0.1:
        print("Error: packet lost")
        return False

    # Simulate delay
    time.sleep(1)

    # Simulate out-of-order packets
    if random.random() < 0.1:
        print("Warning: packet received out of order")

    # Simulate corrupted packets
    if random.random() < 0.1:
        print("Error: packet corrupted")
        return False

    # Simulate correct acknowledgement
    if expected_seq_num == 0:
        print("Sending acknowledgement for packet 0")
    else:
        print("Sending acknowledgement for packet 1")
    return True

# Test the implementation
data = ["hello", "world", "how", "are", "you"]
sender(data)

 

Revise this Paste

Your Name: Code Language: