Important: Please read the Qt Code of Conduct -

How to Test Ftp server availability

  • Hi
    I have a small requirement as i need to test Ftp availability using QNetworkAccessManager.

    I was able to transfer file to server.But i need to test availability first and then transfer file..
    How do i do that..Can anyone share a part of code or hints

    Thanks in Advance
    Bala Beemaneni

  • Thats good...But i want to login in to FTP if it is available.How do i do that with QNetworkAccessManager?

    Bala Beemaneni

  • Try something like this:

    @ QUrl url("ftp://server/files/jgevas_FTPTest.txt");

    data = new QFile(file, this);

    if (data->open(QIODevice::ReadOnly))

    reply = manager.put(QNetworkRequest(url), data);@

    Maybe, you should change the port number.

  • The above example sends the file to an ftp server.My requirement is different.If You are awre of QFtp ,we do connect to host and login in to ftp .I want similar functionality using QNAM.I should just be able to test with the given credentials whether it is accessible or not? How do i that? Hope u understand the requirement now..

    Bala Beemaneni

  • The above example does everything you want, as you get
    QNetworkReply reply .

    In the next step you can do:


    which yields .

  • Moderators


    Did you check "authenticationRequired": ?
    Until valid credentials are given the signal is emitted again and again. If I understood you correctly this can be used in your case.

  • Hi sirop,
    The above examples does everything ..i get error from QNetworkReply as "NO ERROR". But i do not want to send that text file and verify the accessibility..
    Here we use
    @ reply = manager.put(QNetworkRequest(url), data);@
    i want to send only the ftp address as
    reply = manager.put or get (QNetworkRequest(url));

    I am not sure what function to use whether it is get or put or something else..?
    Let me give an example like i.e..the way we ping and find the accessibility of a network just with address

    Bala Beemaneni

  • I would then use:
    @QNetworkReply * QNetworkAccessManager::get(const QNetworkRequest & request)@

  • Hi p3c0
    That signal emits only if authentication either username or password set mismatches.What i need is i want is on button click need to just login in to ftp server and on another button click logout from ftp server.

  • hi sirop,
    hi i did test that but of no use.Here is my code...
    @ QUrl testFtp("ftp://"+server);

    // reply = manager.put(request,testData);
    reply = manager.get(request);
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));@

    i get the error in Network Error says "202".
    i.e: QNetworkReply::ContentOperationNotPermittedError

  • Moderators

    Well then another way would be to try to download file from ftp server. If it exists then on finished signal you should get NoError

    void MainWindow::replyFinished(QNetworkReply *reply)
    qDebug() << "Reply: " << reply->error(); //will show 0

  • hi dude...
    thats not the way i guess.We just have an ip address,user,password.Just we need to ensure whether we are reachable to the server or not..We do not know what are the contents in ftp.So we cant download a file.
    So cant we just login or connect to ftp server using QNAM?
    What about the QNetworkSession? I could not understand this..can u suggest something else

    Thanks for ur interest p3c0

  • Moderators


  • Moderators

    Another way would be to try to connect the server using QTcpSocket as
    QTcpSocket *socket = new QTcpSocket;
    if(!socket->waitForConnected(2000)) {
    qDebug() << "Not connected";

    If it succeeds then onConnected will be called immediately. But not sure if this is the recommended way.

  • hi ,
    we may not have hostnames for all the ftp servers.So may not help much.
    yeah using TCP will work.Bt right now i am googling for the QNAM API's which can do that.

    Let me know if u get any from QNAM or supporting ones

  • Hii guys

    Any updates on the previous ???
    And i would like to add one more to this as..

    1. When the network we try to connect is not in reach it does not give error on time..It takes a lot of time and some times it never returns any error..How do i handle this?

       @    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));@

    i am catching above signal which takes a lot of time...
    Are there any best methods?


  • Try to use QTimer ?

  • Hi,
    i feel there will be a straight way to do it with QNetworkAccessManager or with QNetworkReply.Probably we are missing that..


Log in to reply