Transfering File from python server to android client
<p>i have a python 2.7 server and an android application. what I do is: send a URL as a string to the server that convert it into a PDF file, then I want to send this file to my android app. The problem is in android app i think, I send file without problem from server but when I try to open it on android it is empty (0 byte file)...</p>

<p>Python Server code:</p>

<pre><code>class ThreadedServer(object):
def __init__(self, host, port):
    self.host = host
    self.port = port
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind((self.host, self.port))

def listen(self):
    self.sock.listen(5)
    while True:
        client, address = self.sock.accept()
        client.settimeout(60)
        threading.Thread(target = self.listenToClient,args = (client,address)).start()

def listenToClient(self, client, address):
    size = 1024

    try:
        data=client.recv(size)

        /*code to convert URL into PDF*/
        /*output file to send is out.pdf*/

        fl=open("out.pdf")
        print fl.name

        while True:
            chunk = file.read(fl)
            if not chunk:
                break

        client.sendall(chunk)            

    except Exception, e:
        print str(e)
        client.close()

if __name__ == "__main__":
    ThreadedServer('',4095).listen()
</code></pre>

<p>Android Class transfer file:</p>

<pre><code>public class Conversione extends AsyncTask&lt;String, String, File&gt; {

@Override
protected File doInBackground(String... params) {
    String s=params[0],fl=null;
    BufferedOutputStream scrivi=null;
    Socket ss;
    try {
        ss = new Socket(InetAddress.getByName("192.168.1.8"),4095);
        scrivi = new BufferedOutputStream(ss.getOutputStream());
        scrivi.write(s.getBytes());
        scrivi.flush();

        File file = new File(Environment.getExternalStorageDirectory(),"/MyReader");
        if(!file.exists())
            file.mkdir();

        fl= Environment.getExternalStorageDirectory().toString()+"/MyReader/out.pdf";
        byte[] buffer = new byte[1024];
        int lenght;
        InputStream is = ss.getInputStream();
        OutputStream os = new FileOutputStream(fl);

        while((lenght=is.read(buffer))&gt;0){
            os.write(buffer,0,lenght);
        }

        os.close();
        ss.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
    File f=new File(fl);
    return f;
}
}
</code></pre>

<p>I don't have idea why this doesn't work, someone can help me?</p>