Search code examples
javaftpapache-commons-net

Java ftp commons.net network coding


I'm currently trying to do some network coding for an android (java) application and I'm facing some problems. I use the Apache library commons.net in order to establish an ftp connection to a server I'm hosting for file transfer to the android unit. this is my code:

public class Server {
public static void main(String[] args)
{
    String username = "Username";
    String password = "Password";
    String host = "AddressString";
    FTPSClient ftps;
    ftps = new FTPSClient();
    System.out.println("trying to connect...");
    try{
        System.out.println("trying to connect...");
        ftps.connect(host, 21);
        System.out.println("Connected");
        System.out.println("logging in...");
        ftps.login(username, password);
        System.out.println("logged in!");
        ftps.enterLocalPassiveMode();

                catch (IOException ex) {
                    System.out.println("Error: " + ex.getMessage());
                    ex.printStackTrace();
                } finally {
                try {
                    if (ftps.isConnected()) {
                    System.out.print("LOggin out");
                        ftps.logout();
                        ftps.disconnect();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

}
    System.out.println("Terminated");
}

}

The program never gets passed the line "ftps.connect(host, 21);", with the error "Connection closed without indication", I do belive I have configured my server correctly since I can connect to it via "Putty" from another network etc. What am I missing here?

Note: I am not trying to connect through an Android device, I'm currently using eclipse for testing.


Solution

  • "I do belive I have configured my server correctly since I can connect to it via "Putty" from another network etc. What am I missing here?"

    The "putty" utility talking to an SSH server on port 22 not to an FTP server on port 21. You can say that basic network connectivity / routing appears to be working, but that's not necessarily enough.

    Possible problems you may be having include:

    • the FTP port being blocked by firewalling on the client, the server or somewhere in between, or

    • the FTP server may be configured incorrectly.

    I noticed that the client is trying to use FTP/S but it it using the default port for FTP (21) not FTP/S (991). This is not necessarily wrong (see https://serverfault.com/questions/10807/what-firewall-ports-do-i-need-to-open-when-using-ftps for details) but maybe you should check that your server is configured to support explicit FTP/S.

    I would advise:

    • look at the server and client side logs for clues
    • temporarily turn on "debug level" logging (client & server side)
    • see if you can establish a raw TCP connection on port 21; e.g. using telnet <server-host> 21
    • try changing the client to use FTP rather than FTP/S
    • if all else fails, try using a packet sniffer to capture the network traffic.

    UPDATE

    ... it seems i confused sftp with ftps ...

    Yes, they are very different. SFTP is in effect FTP tunneled over an SSH connection. It requires a client-side library that understands SSH.

    So if you are trying to use FTP / FTPS client-side libraries and settings to talk an SSH (SFTP) service, you are probably failing because port 21 is blocked (which it should be if there is no FTP service) or because no FTP service is running (which should be the default for a typical out-of-the-box Linux server).