0

How do i transfer a large file (video,audio) from my client to server in the local host using UDP sockets in python 3.5? I was able to send a small .txt file but not other file types. Please give me suggestions. Thank you!

Here is my code to transfer a text file.

CLIENT CODE:

import socket
import sys

s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

host = '127.0.0.1'
port=6000

msg="Trial msg"

msg=msg.encode('utf-8')

while 1:

    s.sendto(msg,(host,port))
    data, servaddr = s.recvfrom(1024)
    data=data.decode('utf-8')
    print("Server reply:", data)
    break
s.settimeout(5)   

filehandle=open("testing.txt","rb")

finalmsg=filehandle.read(1024)

s.sendto(finalmsg, (host,port))

SERVER CODE:

import socket

host='127.0.0.1'

port=6000

s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

s.bind(("",port))

print("waiting on port:", port)

while 1:

    data, clientaddr= s.recvfrom(1024)
    data=data.decode('utf-8')
    print(data)
    s.settimeout(4)
    break

reply="Got it thanks!"

reply=reply.encode('utf-8')

s.sendto(reply,clientaddr)

clientmsg, clientaddr=s.recvfrom(1024)
6
  • can you show us your code? Sep 11, 2016 at 21:52
  • Sure. I've added it to my original post
    – Rohan
    Sep 12, 2016 at 4:07
  • Since UDP is an unreliable protocol you have to add some reliability layer to it. This is not a trivial task and adding all of this to your trivial code example which just manages to send/receive a single packet makes the question too broad. But you might start with using multiple send and receive in order to transfer more data. Apart from that please learn how to use proper code formatting when asking questions at SO. Sep 12, 2016 at 4:12
  • Oh ok thank you! And sorry about the format.
    – Rohan
    Sep 13, 2016 at 1:26
  • How do i split the file and send it across? I don't mind if I lose few packets.
    – Rohan
    Sep 13, 2016 at 1:30

2 Answers 2

0

Don't use UDP for transferring large files, use TCP.

UDP does not garauntee all packets you send will arrive, or if they will arrive in order, they may even be duplicated. Furthermore UDP is not suited to large transfers because 1) it has no congestion control so you will just flood the network and the packets will be dropped, and, 2) you would have to break up your packets into smaller ones usually about 1400 bytes is recommended to keep under MTU otherwise if you rely on IP fragmentation and one fragment is lost your whole file is lost .. You would have to write custom code to fix all these issues with UDP since file transfers require everything to be sent reliably.

TCP on the other hand already does all this, it is reliable, has congestion control and is ubiquitous - you are viewing this web page over HTTP which is on top of TCP.

3
  • This does not answer the question which is how to do it with UDP. UDP can be used to transfer large files and is used to transfer large files although one needs to either add a reliability mechanism similar to TCP on top of UDP (like in the QUIC protocol) or accept potential data loss (like in real time video streaming). Sep 12, 2016 at 4:07
  • Ya, but I am supposed to do it using UDP, so I was wondering about how I could do it..
    – Rohan
    Sep 12, 2016 at 4:08
  • I know UDP can be used, but judging by the OP (starting from scratch and to transfer existing large files, i.e. not live streaming) it would crazy stupid to write your own protocol and I stand by my answer. Seriously, go back to whoever is mandating UDP and explain 6 months developing your own protocol to do something worse than TCP alread does just isn't worth it.
    – markmnl
    Sep 12, 2016 at 5:58
0

If you must use UDP instead of TCP or an application level protocol then, you should be able to 'generate redundant blocks' with a package like zfec so that you can reconstruct the original data even if not all of the packets are received.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.