使用Qt开发FTPClient时遇到的问题

使用Qt开发FTPClient时遇到的问题,qt,qtnetwork,Qt,Qtnetwork,我正在尝试使用QT网络实现FTPClient 我如何处理特殊情况,如在下载过程中网络电缆被拔下,而不是互联网连接消失等 我的FTPClient如何知道此类事件,是否有此类通知 我曾尝试使用像done(bool)、ommandfished(int-id、bool-error)这样的信号,但我没有得到任何类型的信号。您似乎使用了QFtp,这已经过时了。您应该使用QNetworkReply(和QNetworkAccessManager),它具有finish()和error()信号: . 要在使用QF

我正在尝试使用QT网络实现FTPClient

我如何处理特殊情况,如在下载过程中网络电缆被拔下,而不是互联网连接消失等

我的FTPClient如何知道此类事件,是否有此类通知


我曾尝试使用像done(bool)、ommandfished(int-id、bool-error)这样的信号,但我没有得到任何类型的信号。

您似乎使用了QFtp,这已经过时了。您应该使用QNetworkReply(和QNetworkAccessManager),它具有finish()和error()信号:
.

要在使用QFtp时处理网络异常,可以监听stateChanged()信号。如果状态变为关闭或未连接,可以检查错误()是什么


关于QNAM vs QFtp:QNAM是更干净、更新的api,但这两种api都非常适合工作并得到官方支持。就API而言,QFtp使用旧的命令id模式(每个命令返回一个命令id),这要求我们跟踪命令(例如:找出发出信号的命令)。我发现QNAM的api模式要好得多,因为它的命令返回一个QNetworkReply对象,该对象反过来发出信号。但是,QNAM的api似乎不像处理http/s(比如)那样针对ftp进行了优化,所以现在您最好还是坚持使用QFtp。

QNetworkAccessManager,正如我无法回答的评论中所述,它是基本的网络实用工具,用于满足一般需求,而不是用于低级别访问

您可以做的选择很少:

1) 使用QTcpSocket和服务器自行实现FTP协议和所有需要的功能

2) 使用QNetworkAccessManager,希望您能解决所有问题


每种方法的好处应该很清楚,但请记住,Qt不仅仅是创建FTP客户端的工具包。

您是否尝试过创建自定义插槽并将其连接到QNetworkReply错误信号

然后,您可以检查参数以确定错误并决定如何处理它

QNetworkReply::NoError  0   no error condition. Note: When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with the QNetworkRequest::RedirectionTargetAttribute attribute.
QNetworkReply::ConnectionRefusedError   1   the remote server refused the connection (the server is not accepting requests)
QNetworkReply::RemoteHostClosedError    2   the remote server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::HostNotFoundError    3   the remote host name was not found (invalid hostname)
QNetworkReply::TimeoutError 4   the connection to the remote server timed out
QNetworkReply::OperationCanceledError   5   the operation was canceled via calls to abort() or close() before it was finished.
QNetworkReply::SslHandshakeFailedError  6   the SSL/TLS handshake failed and the encrypted channel could not be established. The sslErrors() signal should have been emitted.
QNetworkReply::TemporaryNetworkFailureError 7   the connection was broken due to disconnection from the network, however the system has initiated roaming to another access point. The request should be resubmitted and will be processed as soon as the connection is re-established.
QNetworkReply::ProxyConnectionRefusedError  101 the connection to the proxy server was refused (the proxy server is not accepting requests)
QNetworkReply::ProxyConnectionClosedError   102 the proxy server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::ProxyNotFoundError   103 the proxy host name was not found (invalid proxy hostname)
QNetworkReply::ProxyTimeoutError    104 the connection to the proxy timed out or the proxy did not reply in time to the request sent
QNetworkReply::ProxyAuthenticationRequiredError 105 the proxy requires authentication in order to honour the request but did not accept any credentials offered (if any)
QNetworkReply::ContentAccessDenied  201 the access to the remote content was denied (similar to HTTP error 401)
QNetworkReply::ContentOperationNotPermittedError    202 the operation requested on the remote content is not permitted
QNetworkReply::ContentNotFoundError 203 the remote content was not found at the server (similar to HTTP error 404)
QNetworkReply::AuthenticationRequiredError  204 the remote server requires authentication to serve the content but the credentials provided were not accepted (if any)
QNetworkReply::ContentReSendError   205 the request needed to be sent again, but this failed for example because the upload data could not be read a second time.
QNetworkReply::ProtocolUnknownError 301 the Network Access API cannot honor the request because the protocol is not known
QNetworkReply::ProtocolInvalidOperationError    302 the requested operation is invalid for this protocol
QNetworkReply::UnknownNetworkError  99  an unknown network-related error was detected
QNetworkReply::UnknownProxyError    199 an unknown proxy-related error was detected
QNetworkReply::UnknownContentError  299 an unknown error related to the remote content was detected
QNetworkReply::ProtocolFailure  399 a breakdown in protocol was detected (parsing error, invalid or unexpected responses, etc.)
其中一些错误代码是HTTP特有的,但其他错误代码更为通用。

以下是一个错误代码,以及文档。我建议你在房间里用他们的包装纸

下载时处理错误的摘录:

 if (ftp->currentCommand() == QFtp::Get) {
     if (error) {
         statusLabel->setText(tr("Canceled download of %1.")
                              .arg(file->fileName()));
         file->close();
         file->remove();
     } else {
         statusLabel->setText(tr("Downloaded %1 to current directory.")
                              .arg(file->fileName()));
         file->close();
     }
     delete file;
     enableDownloadButton();
     progressDialog->hide();
这也是一个完整的演示。以下是一个屏幕截图:


我正在使用Qt 4.7库。它确实下载了文件,但我没有收到这样的事件。尽管如此,QFtp已经过时,可能有缺陷。你试过QNetworkReply吗?我认为说服Ashish将QNAM用于FTP客户端是不对的。QNAM充当需要处理不同协议的应用程序的抽象层。相反,FTP客户端在设计上只能使用FTP协议,它可能需要比通用QNAM接口中公开的更复杂的功能。QFtp apidocs:“此类提供了FTP的直接接口,允许您对请求进行更多控制。但是,对于新的应用程序,建议使用QNetworkAccessManager和QNetworkReply,因为这些类拥有一个更简单、更强大的API。“它没有明确地标记为过时,尽管QHttp(我认为QFtp也是如此),就目前而言,你是对的。不过,我希望QNAM这些天会得到更好的支持。不过,我自己也没有在FTP中大量使用它。是的,我知道这是可行的,但当网络被禁用时,它不会给出任何事件。当网络断开连接时,客户端应该相信下载“完成”“然后这个密码就会被激活。这不是正在发生的事情吗?