File Send by UDP Sockets
<p>I am sending file using python' UDP socket. At receiver side(file_receiver.py), you need to interrupt (ctrl-c) the program in order to receive the file at the end. Therefore I put settimeout for 2 second, to program to quit automatically, once data is received completely. If I know well, you can not set non-blocking socket in UDP. what is the best way to overcome this problem.</p>

<p>file_sender.py</p>

<pre><code>#!/usr/bin/env python

from socket import *
import sys

s = socket(AF_INET,SOCK_DGRAM)
host ="localhost"
port = 9999
buf =1024
addr = (host,port)

f=open (sys.argv[1], "rb") 
data = f.read(buf)
while (data):
    if(s.sendto(data,addr)):
        print "sending ..."
        data = f.read(buf)
s.close()
f.close()
</code></pre>

<p>file_receiver.py</p>

<pre><code>#!/usr/bin/env python

from socket import *
import sys
import select

host="0.0.0.0"
port = 9999
s = socket(AF_INET,SOCK_DGRAM)
s.bind((host,port))

addr = (host,port)
buf=1024


f = open("op.pdf",'wb')
data,addr = s.recvfrom(buf)


while(data):
    f.write(data)
    s.settimeout(2)
    data,addr = s.recvfrom(buf)

f.close()
s.close()
</code></pre>

<p>Thanks.</p>
