import sys import time from defective_socket import DefectiveTxSocket # Define constants BIT_ERROR_RATE = 0.1 # in percentage BUFFER_SIZE = 1024 # Check input arguments if len(sys.argv) != 2: print("ERROR: Invalid input(s).") print("Usage:") print("\tpython fserver.py ") sys.exit(1) try: bindPort = int(sys.argv[1]) except ValueError: print("ERROR: server port must be between 1024 and 65535.") sys.exit(1) # Define socket serverSocket = DefectiveTxSocket(bindPort, BIT_ERROR_RATE) print("Server listening on {}:{}".format("0.0.0.0", bindPort)) # Start running the server """ SAMPLE CODE This sample code waits for a request packet from the client, and on receipt, continuously sends out packets containing the string "hello" You may find this useful when you start writing your own code. """ data, clientAddr = serverSocket.recvfrom(BUFFER_SIZE) print("Received request from {}:\n{}".format(clientAddr, data)) serverSocket.settimeout(1) # allows for CTRL+C break out while True: serverSocket.sendto(b"hello", clientAddr) time.sleep(1)