0
votes

I'm trying to learn how to connect my app to a certain FTP server. For this purpose , Im using Apache Commons net which is quite good. So far I managed to connect my app to the FTP server while Im connected to the WiFi. While Im connected to the Wi-Fi, Its connecting and logging in smoothly, yet, when I try to connect my FTP server with my celluar network It gives me connection timeout error. First I thought this because of my cellular network speed. Therefore I increased the timeout for giving my cellular network much more time to connect but It didn't work. I mean It's basicly not working. Here is the code Im using. Its in a class which is extended by AsyncTask.

@Override
    protected String doInBackground(String[] params) {
        String temp = "Files : \n";
        FTPClient client = new FTPClient();
        client.setConnectTimeout(360 * 1000);
        client.setDefaultTimeout(360 * 1000);

        try {
            Log.d("FtpDebug", "Connecting ...");
            client.connect(myftpservername);
            client.enterLocalPassiveMode();
            Log.d("FtpDebug","Connected!");
        }catch (Exception e){
            Log.d("FtpError", e.toString());
        }

I said its not working because it gives me this error

D/FtpError: java.net.SocketTimeoutException: failed to connect to xxx.xx.xxxx.xx.xx/xxx.xxx.xx.x (port 21) after 360000ms: isConnected failed: ETIMEDOUT (Connection timed out)

In this error it says It failed to connect after trying 360.000ms=6 minutes. But actually , Im getting this error in 1 minute or less than 1 minute.So Its not even waiting for 6 minutes.

It is clear that Im doing something wrong. I would be really happy if someone point that out. Thanks.

2

2 Answers

0
votes

FTP is a protocol which uses a TCP connection to transfer the FTP commands (control connection), but for each data transfer it uses a new TCP connection (data connection). Information about the endpoint of the data connection (i.e. the port used) is exchanged inside the control connection.

This setup makes FTP a nightmare whenever firewalls or network address translations (NAT) are involved, which typically is the case when connecting from private networks but also from cellular networks. Sometimes it is possible to work around these problems by switching between passive and active mode, sometimes transparent helper applications are employed inside the network to deal with these problems and sometimes even these helper application will not help or will even make the situation worse.

You might try to play around with FTP active and passive mode, IPv4 vs. IPv6 and maybe you will be able to make the application work in this specific cellular network. But it might be still broken inside another network. I instead recommend that you spare you all this trouble and use a different protocol which has not the design issues, like HTTP/HTTPS or SFTP (but not FTPS which has the same problems).

0
votes

After doing some research and tests I find out that this problem occurs because of FTP server settings. I was using my university's FTP server for this but most probably because of security issues, they decreased the timeout time significantly which was my problem from the beginning. I am now using my friend's FTP server for my testing.

Solution : There is no direct solution for that If you have no permission to change your FTP server's settings. You might want to try your another FTP servers for avoiding or narrowing your problem. But I would like to give a quick advice to newbies like me, If you are trying to do something like a message app or so, don't start doing this with FTP but use MySQL instead. FTP servers, as Steffen pointed out already, is really unreliable and might cause you many troubles when think you are ready after finishing your code.