Getting connect failed: ETIMEDOUT (Connection timed out) in ftp connect

Devang picture Devang · Sep 17, 2014 · Viewed 17k times · Source

I have been using ftp to upload images on server in android application and I'm using the following code to connect with ftp. it's working fine in Wi-fi but if I switched to 3G or 2G connection, I am getting connection time out error. So would you please let me know how to take care of this situation. And my client is also facing this issue in Veriozon, Sprint, ATT network provider too. It's iPhone version is working fine in all network.

Code :

try {
                    ftpClient = new FTPClient();
                    ftpClient.setConnectTimeout(30);
                    ftpClient.connect(InetAddress.getByName(server));

                    boolean isLogin = ftpClient.login(username, password);
                    boolean workingDir = ftpClient
                            .changeWorkingDirectory(path);

                    if (ftpClient.getReplyString().contains("250")) {

                        ftpClient
                                .setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);

                        buffIn = new BufferedInputStream(
                                new FileInputStream(filePath));
                        ftpClient.enterLocalActiveMode();
                        // ftpClient.enterLocalPassiveMode();

                        ProgressInputStream progressInput = new ProgressInputStream(
                                buffIn, progressHandler);

                        isUploaded = ftpClient.storeFile(fileName,
                                progressInput);

                        buffIn.close();
                        ftpClient.logout();
                        ftpClient.disconnect();
                    }

                } catch (Exception e) {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    progressDialog.dismiss();
                                    Toast.makeText(RegisterActivity.this,
                                            R.string.postimage_uploaderror,
                                            Toast.LENGTH_LONG).show();
                                }
                            });

                        }
                    });
                }

Error :

java.net.ConnectException: failed to connect to Host (port 21): connect failed: ETIMEDOUT (Connection timed out)

I have imported "commons-net-ftp-2.0.jar" and commons-net-3.3.jar in my project.

Looking forward for your answer.

Best Regards,

Devang

Answer

Nadir Belhaj picture Nadir Belhaj · Sep 17, 2014

Normally a 3G or 2G connection is slower than wifi this is why you get a Connection Timeout error. to encounter this you need to set the timeout delay for your FTP client and you can do it by adding this line

ftpClient.setConnectTimeout(30); // 30 mSeconds increase it for more time 

so your code will become :

ftpClient.setConnectTimeout(30);
ftpClient.connect(InetAddress.getByName(server));
boolean isLogin = ftpClient.login(username, password);
boolean workingDir = ftpClient.changeWorkingDirectory(path);

EDIT

increase the timeout to 50s (50000)

ftpClient.setConnectTimeout(50000); // 50 Seconds increase it for more time