0

I'm trying to create a ftp client for multiple user accounts. While adding a user account, I need to check whether the host and user details are valid or not. For that I created a method.

bool FtpConnect::checkConnectivity(QString remoteIP, QString username, QString password)
{
    QUrl server;
    server.setScheme("ftp");
    server.setHost(remoteIP);
    if (!server.isValid()) {
        QMessageBox::warning(0, "Error", "Invalid Host");
        return false;
    }
    server.setPort(21);
    server.setUserName(username);
    server.setPassword(password);
    QFtp *ftp = new QFtp(this);
    ftp->connectToHost(server.host(), server.port());
    int status = ftp->login(server.userName(), server.password());
    QMessageBox::information(0, "Updated", QString::number(status));
    return (ftp->list() == 4) ? true : false;
}

I need the code to return true if the connection is ok and false while the connection could not complete. Noe the variable status returns 4 which is the QFtp::Connecting signal. please somebody help me.

4

1 回答 1

2

QFtp methods do not block execution. The command is scheduled, and its execution is performed asynchronously.
You can't check status() right after you call connectToHost() or login(), because at this moment state is still Unconnected. You should wait for a signal to be emitted instead.
ftp->list() and other methods return the command id, so there's no point in checking return values. What you can do is remember the id and wait for commandFinished() signal with this id, but there is another option.
Use QFtp::stateChanged and check for QFtp::LoggedIn.

于 2013-03-05T08:44:49.750 回答