May-15-2020, 03:01 PM
I am required to send an audio file form client to server using UDP protocol (python) in packets of 500 bytes each. Then problem statement for sender is given as: "The sender should run as follows: The sender will read the file specified by the filename and transfer it using UDP sockets. On completing the transfer, the sender should terminate and exit. The sender should bind to listen port to receive acknowledgments and other signaling from the receiver. You should only use a single UDP socket for both sending and receiving data. Note that although UDP will allow you to send large packets using IP fragmentation, but make sure that you restrict your packets to 500 bytes (in payload). " The problem statement for receiver is given as: "The receiver should run as follows: The receiver will need to bind to the UDP port specified on the command line and receive a file from the sender sent over the port. The file should be saved to a different filename. Note that you should make sure in your testing that the filename used by the receiver is not the same as the one used by the sender. The receiver should exit once the transfer is complete."
THE CLIENT THAT IS SENDER CODE IS GIVEN BELOW
THE CLIENT THAT IS SENDER CODE IS GIVEN BELOW
from socket import * import time # Assigning server IP and server port serverName = "127.0.0.1" serverPort = 5000 # Setting buffer length buffer_length = 500 # Assigning the audio file a name my_audio_file = r"C:\Users\mali.bee17seecs\PycharmProjects\TestProject\Aye_Rah-e-Haq_Ke_Shaheedo.mp3" clientSocket = socket(AF_INET, SOCK_DGRAM) # Opening the audio file f = open(my_audio_file, "rb") # Reading the buffer length in data data = f.read(buffer_length) # While loop for the transfer of file while data: if clientSocket.sendto(data, (serverName, serverPort)): data = f.read(buffer_length) time.sleep(0.02) # waiting for 0.02 seconds clientSocket.close() f.close() print("File has been Transferred")THE SERVER THAT IS RECEIVER CODE IS GIVEN BELOW
from socket import * import select # Assigning server IP and server port serverName = "127.0.0.1" serverPort = 5000 # Setting timeout timeout = 3 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind((serverName, serverPort)) # While loop for the receiving of file while True: data, serverAddress = serverSocket.recvfrom(1024) if data: file = open(r"C:\Users\mali.bee17seecs\PycharmProjects\TestProject\Aye_Rah-e-Haq_Ke_Shaheedo.mp3", "wb") while True: ready = select.select([serverSocket], [], [], timeout) if ready[0]: data, serverAddress = serverSocket.recvfrom(500) file.write(data) else: file.close() print("File has been Received") break