使用Qt开发FTPClient的问题

发布于 2024-10-05 22:53:28 字数 215 浏览 1 评论 0原文

我正在尝试使用 QT Network 来实现 FTPClient 。

我如何处理特殊情况,例如在下载过程中网络电缆被拔掉,互联网连接没有消失等?

我的 FTPClient 如何才能知道此类事件以及是否有此类通知可用?

我尝试过使用 did(bool) 、 ommandFinished ( int id, bool error ) 等信号,但我没有收到任何类型的信号。

I am trying to implement FTPClient in using QT Network .

How can i handle exceptional cases like during downloading network cable is unplugged , not internet connection gone etc.. ?

How can my FTPClient can come to know about such event and is there such kind of notification available ?

I have tried to use signals like done(bool) , ommandFinished ( int id, bool error ) but i m not getting any sort of signal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假情假意假温柔 2024-10-12 22:53:28

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

You seem to use QFtp, which is obsolete. You should use QNetworkReply (and QNetworkAccessManager), which has finished() and error() signals:
QNetworkReply documentation.

冰雪梦之恋 2024-10-12 22:53:28

您是否尝试过创建自定义插槽并将其连接到 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,但其他错误代码则更为通用。

Have you tried creating a custom SLOT and connecting it to the QNetworkReply error SIGNAL?

You can then inspect the argument to determine the error and decide how you want to deal with it.

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.)

Some of this error codes are specific to HTTP but others are more generic.

枕梦 2024-10-12 22:53:28

要在使用 QFtp 时处理网络异常,可以监听 stateChanged() 信号。如果状态变为 Closing 或 Unconnected,您可以检查 error() 是什么。

关于 QNAM 与 QFtp:QNAM 是更干净、更新的 API,但两者都非常适合工作并得到官方支持。 API 方面,QFtp 使用旧的命令 ID 模式(每个命令返回一个命令 ID),这要求我们跟踪命令(例如:找出发出信号的命令)。我发现 QNAM 的 api 模式要好得多,因为它的命令返回一个 QNetworkReply 对象,该对象又会发出信号。但是,QNAM 的 api 似乎并没有针对 ftp 进行调整,也没有处理 http/s(例如 不通过 ftp 删除文件),所以也许您现在最好坚持使用 QFtp。

To handle network exceptions when using QFtp, you can listen to the stateChanged() signal. If the state becomes Closing or Unconnected, you can check what the error() is.

About QNAM vs QFtp: QNAM is the cleaner and newer api, but both are very much meant to work and officially supported. API-wise, QFtp uses the old command-id pattern (each command returns a command-id), which require us to keep track of the commands (for eg.: to figure out for what command a signal was raised). I find QNAM's api pattern to be much better because its commands return a QNetworkReply object that in turn emits signals. But then, QNAM's api doesn't seem tuned for ftp as well as it handles http/s (like no deletion of files over ftp), so maybe you're good sticking to QFtp for now.

喜爱纠缠 2024-10-12 22:53:28

这是QT FTP 客户端的完整示例以及文档。我建议在 QFTP 类 周围使用他们的包装器。

下载时处理错误的摘录:

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

这也是一个完全正常工作的演示。这是屏幕截图:

alt text

Here is a complete example of an QT FTP client, along with documentation. I would recommend using their wrappers around the QFTP class.

Excerpt on handling errors when downloading:

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

It's a fully working demo, too. Here's a screenshot:

alt text

樱花落人离去 2024-10-12 22:53:28

正如我无法回答的评论中所述,QNetworkAccessManager 是满足常见需求而不是低级别访问的基本网络实用程序。

您可以选择几个选项:

1) 使用 QTcpSocket 和服务器自己实现 FTP 协议以及您想要的所有功能。

2)使用QNetworkAccessManager并希望你能解决它的所有问题。

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

QNetworkAccessManager, as stated in comments I’m not able to answer, is basic network utility for common needs and not for low level access.

There are few options you can do:

1) Implement FTP protocol yourself and all features you want using QTcpSocket and server.

2) Use QNetworkAccessManager and hope you can work around all issues with it.

Benefits from each approach should be clear, but remember, that Qt is not just a toolkit for FTP client creation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文