0

I have written the code for transferring an audio file from client to server using udp (python). Now I am required to introduce reliability in the codes of UDP. The instructions are given as:

"You will be required to implement following to make UDP reliable: (a) Sequence and acknowledge numbers (b) Re-transmission (selective repeat) (c) Window size of 5-10 UDP segments (stop n wait) (d) Re ordering on receiver side "

THE SENDER THAT IS CLIENT 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 RECEIVER THAT IS SERVER 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
Muhammad Ali
  • 1
  • 1
  • 4

1 Answers1

0

Before answer each request, you should know that we build a reliable UDP by adding some specific infomation before the real content, which you can think as a application layer head. We use them to do some control or collect infomation like TCP does in traffic layer by the head part. It may look like below:

struct Head {
    int seq;
    int size;
}

(a) Sequence and acknowledge numbers

If you're familar with TCP, it is not hard. You can set seq and when the other side receive it, the controller will judge it and to check if we need to do b/d.

(b) Re-transmission (selective repeat) & (d) Reordering on receiver side

They are familiar to realise, using GBN/ARQ/SACK algorithm to do retransmission, using some simple algorithm like sorting to do reording.

(c) Window size of 5-10 UDP segments (stop n wait)

This part need to do some thing like traffic control that TCP does. I don't how complex you want to do, it's can be really complex or simple, it depends on you.

tyChen
  • 1,404
  • 8
  • 27
  • Can you send me chunk of codes for each part by looking into my code that I have given above. I would be very grateful to you for that. Thank You – Muhammad Ali May 16 '20 at 06:45
  • Sorry but it's not the spirit of stackoverflow, nor is it the spirit of a programmer. I can give you some suggestion but you need to write yourself. – tyChen May 16 '20 at 06:52
  • ok I will try to write code myself. If I faced any issue I will ask here – Muhammad Ali May 16 '20 at 06:57
  • That's ok, good luck to you. After you write your own reliable UDP, it's time to look at some simple but effecitve reliable UDP open source projects like libutp. It'll be great to improve your knowledge and programming skill. – tyChen May 16 '20 at 07:01