1

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();
David Green
  • 1,210
  • 22
  • 38

1 Answers1

0

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);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Do I still need the sleep if I have a breakpoint set at StartServer()? I run to the breakpoint and start the server, then try the connection. Eventually there will be file transfers happening between StartServer() and StopServer() – David Green May 24 '18 at 14:14
  • If your server is paused in debugger, it can hardly accept connections. – Martin Prikryl May 24 '18 at 14:15
  • hmmm what I meant was I set a break point at StopServer(), and in the debugger I can see that the server is running. at least the property that says whether it's running is set to true. – David Green May 24 '18 at 17:09
  • That's what I mean. You have stopped the process in the debugger (I mean whole process here, not the FTP server). So it cannot accept requests. And once you resume the process, it calls `StopServer`. So there's no moment, when it can accept incoming requests. Did you even tried to use the `sleep` as I've suggested? – Martin Prikryl May 24 '18 at 17:16
  • Not yet. I will try that. I misunderstood what the breakpoint does and was just trying to understand that. thanks for clarifying. – David Green May 24 '18 at 17:46
  • I tried adding the Thread.Sleep(). I had the same problem and after it resumed from sleep it threw in IncomatibleClassChange exception. I'm going to look around and see if I can find some more sample code that implements the server. I'm trying to embed a server in another larger project. – David Green May 24 '18 at 23:41