Important: Please read the Qt Code of Conduct -

Detecting when network connection is lost

  • I am writing a simple FTP downloader. Despite the recommendation of not using QFtp (because its deprecated), I am proceeding to use it anyway. Its a little annoying that Qt would deprecate this without providing a new version. But whatever.

    So far, I can log in. Get a list of files. Etc. Everything works pretty good. The one thing that is unclear is how do I detect if I've lost my connection with the ftp site?

    I have a QTimer that will timeout if the initial login fails within a few seconds. But suppose I log in. And I send a request to download a file. How can I check if the ftp connection is valid before sending the request? How about in the middle of the download? How do I detect if there is a lost connection?

    Any suggestions would be appreciated.

  • Moderators

    I don't think there's a way to tell if connection is valid before you use it (apart from the state() function).
    But you can just try to send a command (or sequence) anyway. Whenever a sequence finishes QFtp sends a done(bool error) signal. If there was interruption, connection lost or any other kind of error the error flag will be true.
    You can then proceed with error() or errorString() to see what kind of error occurred.

  • SIGNAL stateChanged(int state)

Log in to reply