Apache Mina Java FTP Server Implementation - Client Stuck Waiting For Welcome Message

593 views Asked by At

I am implementing an FTP server in Java for a project. I can start the server but when I try to connect with a client it is stuck on "waiting for welcome message". I've looked at several examples but I'm not sure where I'm going wrong. Here is the class I have. I will eventually break some of this out into other methods.

The user parameters have been cleared for the purposes of this post.

public class FTPServer {


final int PORT = 2221;
String userfile = "";
String username="";
String password = ""
String homedir ="";

private FtpServer server=null;
public FTPServer() {}

public FTPServer(final String ipaddress, final int port){   


FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory listenerfactory = new ListenerFactory();

    listenerfactory.setDataConnectionConfiguration(
    new DataConnectionConfigurationFactory().createDataConnectionConfiguration());

    ConnectionConfigFactory connection = new ConnectionConfigFactory();
    connection.setMaxLoginFailures(10);
    connection.setLoginFailureDelay(5);
    connection.setAnonymousLoginEnabled(false);

// set the ip address of the listener
listenerfactory.setServerAddress(ipaddress);

// set the port of the listener
if (port == 0)
{ listenerfactory.setPort(PORT);}

else {listenerfactory.setPort(port);
// replace the default listener
serverFactory.addListener("default", listenerfactory.createListener());
     serverFactory.setConnectionConfig(connection.createConnectionConfig());

}

PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File(userfile));
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
UserManager um = userManagerFactory.createUserManager();
BaseUser user = new BaseUser();

user.setName(username);
user.setPassword(password);
user.setHomeDirectory(homedir);
try {
    um.save(user);
} catch (FtpException e1) {
    // TODO Auto-generated catch block
    this.StopServer();
    e1.printStackTrace();
}

serverFactory.setUserManager(um);
    server = serverFactory.createServer();

}

public void  StopServer(){ this.server.stop(); }

public void StartServer()
{
try {
    server.start();
} catch (FtpException e) {
    // handle this eventually, good enough for testing now
    e.printStackTrace();
}
}

Here is the code that creates the server and starts and stops it

final int port = 0;
final String ipaddress = "";
FTPServer server = new FTPServer(ipaddress,port);
server.StartServer();
 server.StopServer();
1

There are 1 answers

6
Martin Prikryl On

I'd say that FtpServer.Start only starts listening on the incoming port. It does not block. You kill the server immediately afterwards by calling .Stop.

You have to wait in your code explicitly to keep the server running.

server.StartServer();
Thread.sleep(Long.MAX_VALUE);