Hanzo
Hanzo

Reputation: 1899

Download files from server freezes app and loses connection

I'm triying to download some files from a FTP Server with FTPClient (commons-net-3.3).

When click in a button with a AsyncTask I get a list of FTPFile s with listFiles(path);. Then I download each file of list like this (All of work in the same AsyncTask)

FileOutputStream desFileStream = new FileOutputStream("localfile");
InputStream input = mFTPClient.retrieveFileStream("remotepath");
byte data[] = new byte[512];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
    total += count;                             
    Double porcentaje =  ((double) total / (double) tmpFiled.getSize()) * 100;
    publishProgress((int) Math.round(porcentaje));
    desFileStream.write(data, 0, count);
    }

    desFileStream.flush();
    desFileStream.close();
    input.close();
}}

In particular in input.read(data). This must be read 1024 bytes but in random iteration read less data than 1024, write it and in the next iteration input.read(data) freezes download return timedout exception

After that the internet connection of the dispositive not works

Logcat shows this exception

09-19 09:59:06.890: W/System.err(13215): java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
09-19 09:59:06.900: W/System.err(13215):    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
09-19 09:59:06.900: W/System.err(13215):    at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
09-19 09:59:06.900: W/System.err(13215):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
09-19 09:59:06.920: W/System.err(13215):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
09-19 09:59:06.920: W/System.err(13215):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
09-19 09:59:06.920: W/System.err(13215):    at java.io.FilterInputStream.read(FilterInputStream.java:118)
09-19 09:59:06.920: W/System.err(13215):    at java.io.InputStream.read(InputStream.java:162)
09-19 09:59:06.920: W/System.err(13215):    at com.asde.telemedicina.FTPHandler.ftpDownload(FTPHandler.java:405)
09-19 09:59:06.920: W/System.err(13215):    at com.asde.telemedicina.FTPHandler.doInBackground(FTPHandler.java:167)
09-19 09:59:06.920: W/System.err(13215):    at com.asde.telemedicina.FTPHandler.doInBackground(FTPHandler.java:1)
09-19 09:59:06.920: W/System.err(13215):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-19 09:59:06.920: W/System.err(13215):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-19 09:59:06.920: W/System.err(13215):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-19 09:59:06.930: W/System.err(13215):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-19 09:59:06.930: W/System.err(13215):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-19 09:59:06.930: W/System.err(13215):    at java.lang.Thread.run(Thread.java:841)
09-19 09:59:06.930: W/System.err(13215): Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
09-19 09:59:06.930: W/System.err(13215):    at libcore.io.Posix.recvfromBytes(Native Method)
09-19 09:59:06.930: W/System.err(13215):    at libcore.io.Posix.recvfrom(Posix.java:141)
09-19 09:59:06.930: W/System.err(13215):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
09-19 09:59:06.930: W/System.err(13215):    at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
09-19 09:59:06.930: W/System.err(13215):    ... 14 more

For example in Motorola Moto G never occurs however in other dispositives frecuently occurs. I',e tried two ftpservers.

Can cause the problems local file permissions?

Can anyone help me out?

Upvotes: 0

Views: 992

Answers (3)

Matteo Corti
Matteo Corti

Reputation: 494

I think that your error could be caused by multiple active downloads connection which saturate your network connection. And generate a timeout exception. It should explain the random characteristic of you error. To avoid it I think you have to limitate the number of simultaneous download.

After doing a little search on google I've also carried out (even if i think that isn't your case) that the "Connection Timeout" error In Ice Cream Sandwich and earlier could be related to DNS double caching. See this Issue

Upvotes: 1

mykolaj
mykolaj

Reputation: 974

In particular in input.read(data). This must be read 1024 bytes but in random iteration read less data than 1024

No it's not "must". Here is what Java doc sais:

int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b.

This function reads data up to buffer size, not exactly that size.

InputStream input = mFTPClient.retrieveFileStream("remotepath");

What is "remotepath"?
Is it a file, or is it a directory?

In case it's a file, problem is in the line input.close();, because after you've read few bytes on the first iteration you close your stream and then trying to read from the closed stream on the second iteration (like already been said in comment).

In case it's a directory, then you just can't download it like you're trying to. There's a LIST command in FTP protocol, which lists files in a current directory. You must list those files and download every of them separately.

Upvotes: 0

Batuhan Coşkun
Batuhan Coşkun

Reputation: 2969

I've done little search and try that;

FileOutputStream desFileStream = new FileOutputStream("localfile");
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); // File type can be changed
boolean status = mFTPClient.retrieveFile("remotepath",desFileStream);

Upvotes: 1

Related Questions