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.
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.