【问题标题】:Apache Mina Java FTP Server Implementation - Client Stuck Waiting For Welcome MessageApache Mina Java FTP 服务器实现 - 客户端卡住等待欢迎消息
【发布时间】:2018-05-24 12:35:41
【问题描述】:

我正在为一个项目用 Java 实现一个 FTP 服务器。我可以启动服务器,但是当我尝试与客户端连接时,它卡在“等待欢迎消息”上。我看过几个例子,但我不确定我哪里出错了。这是我的课。我最终会将其中的一些分解为其他方法。

出于本文的目的,用户参数已被清除。

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();
}
}

这是创建服务器并启动和停止它的代码

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

【问题讨论】:

    标签: java ftp mina


    【解决方案1】:

    我会说FtpServer.Start 只开始侦听传入端口。它不会阻塞。之后您立即通过调用 .Stop 终止服务器。

    您必须在代码中明确等待以保持服务器运行。

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

    【讨论】:

    • 如果我在 StartServer() 设置了断点,是否还需要休眠?我跑到断点并启动服务器,然后尝试连接。最终会在 StartServer() 和 StopServer() 之间发生文件传输
    • 如果您的服务器在调试器中暂停,它几乎无法接受连接。
    • hmmm 我的意思是我在 StopServer() 处设置了一个断点,在调试器中我可以看到服务器正在运行。至少表明它是否正在运行的属性设置为 true。
    • 这就是我的意思。您已经停止了调试器中的进程(我的意思是这里的整个进程,而不是 FTP 服务器)。所以它不能接受请求。一旦你恢复这个过程,它就会调用StopServer。所以没有时刻,它可以接受传入的请求。你有没有按照我的建议尝试使用sleep
    • 还没有。我会试试的。我误解了断点的作用,只是试图理解这一点。感谢您的澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2012-11-10
    • 2023-03-04
    相关资源
    最近更新 更多