Qt QFtp Get never发出commandFinished()命令
我目前面临一个关于QFtp的奇怪问题。我想从FTP服务器上下载一堆文件,但当我在y上下载x个文件后,Qt QFtp Get never发出commandFinished()命令,qt,ftp,qftp,Qt,Ftp,Qftp,我目前面临一个关于QFtp的奇怪问题。我想从FTP服务器上下载一堆文件,但当我在y上下载x个文件后,FTP->get()命令完成,文件被填充,但没有发出信号commandFinished(),因此它不会下载其他文件 这是我的密码: void Ftp::commandFinished(int i, bool error) { if(ftp->currentCommand() == QFtp::Get) { if(error) {
FTP->get()
命令完成,文件被填充,但没有发出信号
commandFinished()
,因此它不会下载其他文件
这是我的密码:
void Ftp::commandFinished(int i, bool error)
{
if(ftp->currentCommand() == QFtp::Get)
{
if(error)
{
//blablabla-ERROR-blablabla
}
currentFile->close();
filesToDownload.pop_front();
processFileList();
}
/**Gestion de la commande Login (authentification de l'utilisateur)
*/
if(ftp->currentCommand() == QFtp::Login)
{//not utile here}
/**Gestion de la commande ConnectToHost (connexion au serveur)
*/
if (ftp->currentCommand() == QFtp::ConnectToHost)
{//not utile here}
/**Gestion de la commande List (téléchargement d'un fichier)
*/
if(ftp->currentCommand() == QFtp::List)
{
if(error)
{
//Nananana-FAIL-nanana
}
//!Tri des fichiers à télécharger en fonction de leur dernière date de modification
if (!filesToDownload.isEmpty())
{
currentPeripheral->setLastDownloadDate(newLastModifiedDate) ;
std::sort(filesToDownload.begin(),filesToDownload.end(),compareQUrlInfos);
processFileList();
}
}
}
void Ftp::processFileList()
{
QUrlInfo info;
if (filesToDownload.isEmpty())
{
//!Suicide de l'instance de Ftp
ftp->close();
disconnect(this,0,0,0);
this->deleteLater();
return ;
}
info = filesToDownload.first();
QDir dlDir(QString::number(currentPeripheral->getId()));
//!Si un fichier a été téléchargé, on déclenche son traitement
if (currentFile != nullptr)
{
emit(oneDownloadFinished(currentFile->fileName(),currentPeripheral));
delete currentFile;
currentFile = nullptr;
}
//!On crée un répertoire de téléchargement si nécessaire
if (!dlDir.exists())
{
dlDir.mkdir(".");
}
//!on crée le fichier qui contiendra le téléchargement
currentFile = new QFile(dlDir.filePath(info.name()));
if(!currentFile->open(QIODevice::WriteOnly))
{
delete currentFile;
currentFile = nullptr;
emit(writeToMonitoringConsole(QString("Erreur lors de la creation du fichier "+info.name()),"Error"));
return;
}
//Here I start (sometimes) a never ending fail
ftp->get(info.name(), currentFile);
}
起初我认为这是因为我提出了太多的请求,因此我被拒绝了,但即使使用Sleep(2000)
它也会阻塞。阻塞出现得更快。我通常可以下载大约30个文件(幸运的是70岁的时候,我有200个文件!)。使用Sleep(2000)
我几乎没能成功下载2-3个文件
这是我的错吗?QFtp是否有我没有发现的限制?还是别的什么
EDIT:自从发布后,我测试了一些东西,当监控dataTransferProgress()信号时,引人注目的是问题文件已完全下载(qDebug称为“88928/88928”),但我从未输入commandFinished()
我的插槽commandFinished()通过以下方式链接到我的QFtp::commandFinished信号:
connect(ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(commandFinished(int,bool)));
我看到了几乎相同的情况,在一个
FTPWidget
类中,我使用从未接收过GET请求的commandFinished
。dataTransferProgress()信号可靠地报告了100%的下载及其全部内容
我使用了一个设置超时的方法,如下所示:
// Set up a single shot QTimer and connect it to a function to fix things
connect(m_lostFinishedTimer, SIGNAL(timeout()), this, SLOT(lostFinishedHack()));
// Start the timeout when the download data progress hits 100%
void FtpWidget::updateXferProgress(qint64 readBytes, qint64 totalBytes)
{
m_progressBar->setValue(progress_value(readBytes, totalBytes));
if (m_downloading && readBytes == totalBytes)
m_lostFinishedTimer->start();
}
// And don't forget to stop the timer if you do get a finish:
void FtpWidget::commandFinished(int id, bool error)
{
QFtp::Command cmd = m_ftp->currentCommand();
// ...
if (cmd == QFtp::Get)
{
m_lostFinishedTimer->stop();
//....
}
// ...
}
当计时器连接到修复例程时,基本上关闭下载文件,重新连接服务器,然后转到下一个文件
void FtpWidget::lostFinishedHack()
{
if (!m_downloading)
return;
if (m_downloadFile)
{
DOUT("FtpWidget::lostFinshedHack() -- file: " << DS(m_downloadFile->fileName()) << "\n");
m_downloadFile->close();
delete m_downloadFile;
m_downloadFile = 0;
}
// just reconnect the thing
Connect(m_ftpServerName->text());
downloadNextFile();
}
其中,FtpWidget::commandStarted
和commandFinished
是我的插槽,_q_piFtpReply()是QFtp对象的私有部分,我在其中钩住了跟踪断点
当它失败时,我得到:
FtpWidget::commandStarted(id: 256) -- Get
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 213, text "135896"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 150, text "Opening BINARY connection for 000-23Sep2014_103525.jpg (135896 bytes)"
FtpWidget::lostFinshedHack()
FtpWidget::lostFinshedHack() -- file: C:/Users/sean/Temp/Snapshots5/000-23Sep2014_103525.jpg
FtpWidget::connect("ftp://root@169.254.1.180/")
在200个回复代码之后获得150个回复代码似乎是个问题。因此,如果您查找FTP回复并查看qftp.cpp,它看起来是在150到达之后,导致qftp实现状态机进入永久等待。据我所知,是我的ftp服务器把事情搞砸了,而不是qftp.cpp(也许我应该试试WireShark来确保)
现在,我坚持解决这个问题的超时解决方法。我看到了几乎相同的情况,在使用
FTPWidget
类时,我从未收到GET请求的commandFinished
。dataTransferProgress()信号可靠地报告了100%的下载及其全部内容
我使用了一个设置超时的方法,如下所示:
// Set up a single shot QTimer and connect it to a function to fix things
connect(m_lostFinishedTimer, SIGNAL(timeout()), this, SLOT(lostFinishedHack()));
// Start the timeout when the download data progress hits 100%
void FtpWidget::updateXferProgress(qint64 readBytes, qint64 totalBytes)
{
m_progressBar->setValue(progress_value(readBytes, totalBytes));
if (m_downloading && readBytes == totalBytes)
m_lostFinishedTimer->start();
}
// And don't forget to stop the timer if you do get a finish:
void FtpWidget::commandFinished(int id, bool error)
{
QFtp::Command cmd = m_ftp->currentCommand();
// ...
if (cmd == QFtp::Get)
{
m_lostFinishedTimer->stop();
//....
}
// ...
}
当计时器连接到修复例程时,基本上关闭下载文件,重新连接服务器,然后转到下一个文件
void FtpWidget::lostFinishedHack()
{
if (!m_downloading)
return;
if (m_downloadFile)
{
DOUT("FtpWidget::lostFinshedHack() -- file: " << DS(m_downloadFile->fileName()) << "\n");
m_downloadFile->close();
delete m_downloadFile;
m_downloadFile = 0;
}
// just reconnect the thing
Connect(m_ftpServerName->text());
downloadNextFile();
}
其中,FtpWidget::commandStarted
和commandFinished
是我的插槽,_q_piFtpReply()是QFtp对象的私有部分,我在其中钩住了跟踪断点
当它失败时,我得到:
FtpWidget::commandStarted(id: 256) -- Get
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 213, text "135896"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 150, text "Opening BINARY connection for 000-23Sep2014_103525.jpg (135896 bytes)"
FtpWidget::lostFinshedHack()
FtpWidget::lostFinshedHack() -- file: C:/Users/sean/Temp/Snapshots5/000-23Sep2014_103525.jpg
FtpWidget::connect("ftp://root@169.254.1.180/")
在200个回复代码之后获得150个回复代码似乎是个问题。因此,如果您查找FTP回复并查看qftp.cpp,它看起来是在150到达之后,导致qftp实现状态机进入永久等待。据我所知,是我的ftp服务器把事情搞砸了,而不是qftp.cpp(也许我应该试试WireShark来确保)
目前,我仍然坚持解决这个问题的超时解决方案。我已经来不及回复了,但它仍然可以帮助其他人 在
void Ftp::commandFinished(int i,bool error)
中,最好同时检查QFtp::None
选项,
因为如果您的最后一个命令是get
,并且在调用commandFinished()
slot时
if(ftp->currentCommand()==QFtp::Get)
将无法识别最后一个命令,因为当前命令可能不是Get
,因为它已经完成
您也可以使用id进行检查
作为
if(ftp->currentCommand()==QFtp::Get | | 1==id)
我来不及回复了,但它仍然可以帮助其他人
在void Ftp::commandFinished(int i,bool error)
中,最好同时检查QFtp::None
选项,
因为如果您的最后一个命令是get
,并且在调用commandFinished()
slot时
if(ftp->currentCommand()==QFtp::Get)
将无法识别最后一个命令,因为当前命令可能不是Get
,因为它已经完成
您也可以使用id进行检查
作为
if(ftp->currentCommand()==QFtp::Get | 1==id)
我在您的代码中没有看到任何commandFinished信号?您能分享更多的代码吗?commandFinished()是QFtp发出的,当其中一个命令完成时,我通常不应该自己发送这个信号。这可能是因为我在代码中没有看到任何commandFinished信号?您能分享更多的代码吗?commandFinished()是QFtp发出的,当其中一个命令完成时,我通常不应该自己发送这个信号。这可能是我自己试图破解的,但我没有得到足够可靠的东西。失败时我尝试重新连接,但最终我在ftp服务器上同时连接了很多无法关闭的连接。所以我决定放弃使用Qt进行FTP操作。我用的是非常容易使用而且可靠的,我自己也试着破解,但是我没有得到足够可靠的东西。失败时我尝试重新连接,但最终我在ftp服务器上同时连接了很多无法关闭的连接。所以我决定放弃使用Qt进行FTP操作。我改用了它,它很容易使用,也很可靠。谢谢你的回复,我现在已经无法访问有问题的代码了,所以我无法测试你的建议,我希望它不会出现问题