Simple ftp file transfer reconnect problem?
-
Hello,
i write an small prg that transfer an file via ftp.
i test the prg and get following error,
if the username wrong first transfer get an user or password error 204,
thats okay, if i retry the transfer with the same settings i get an timeout.why?
@#include "include/Protocol/Ftp.h"
#include "include/ConfigFile/ConfigFile.h"
#include "include/Defines/Global.h"#include <QUrl>
#include <QNetworkAccessManager>
#include <QFile>
#include <QApplication>Ftp::Ftp(ConfigFile &config, QWidget *parent) :
QWidget(parent), cf(config), reply(NULL)
{
manager = new QNetworkAccessManager(0);
connecttimer.setInterval(FTP_CONNECT_TIMEOUT);
connect(&connecttimer, SIGNAL(timeout()), this, SLOT(CheckConnection()));}
void Ftp::GetFile(void)
{
if(!reply)
{
reply = manager->get(QNetworkRequest(QUrl("ftp://" +cf.GetftpUsername() +":" +cf.GetftpPassword() +"@" +cf.GetServerAddress().toString() +":" +QString::number(cf.GetftpPort()) +"/" +cf.GetProgramName() +OS_PRG_EXT)));
qDebug() << "#########" << "reply == NULL" << "#########";
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadinProgress(qint64,qint64)));
connect(reply, SIGNAL(finished()), this, SLOT(replyfinished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
}connecttimer.start(); #if DEBUG_LEVEL_FTP > LOG_ERRORS qDebug() << this->metaObject()->className() << "FTP: GetFile "; #endif
}
void Ftp::CheckConnection(void)
{
#if DEBUG_LEVEL_FTP > LOG_ERRORS
qDebug() << this->metaObject()->className() << "CheckConnection";
#endifconnecttimer.stop(); reply->disconnect(); if(reply) { if(reply->isFinished()) { replyfinished(); emit DownloadFinished(reply->error()); emit StatusUpdate("No error"); } else { qDebug() << "reply->isFinished() " << reply->isFinished(); emit DownloadFinished(QNetworkReply::TimeoutError); emit StatusUpdate("the connection to the remote server timed out"); } } reply->deleteLater(); reply = NULL;
}
void Ftp::downloadinProgress(qint64 bytesReceived, qint64 bytesTotal)
{
#if DEBUG_LEVEL_FTP > LOG_ERRORS
qDebug() << this->metaObject()->className() << "downloadinProgress " << "bytesReceived " << bytesReceived << "bytesTotal " << bytesTotal;
#endifconnecttimer.start();
}
void Ftp::slotReadyRead(void)
{
#if DEBUG_LEVEL_FTP > LOG_ERRORS
qDebug() << this->metaObject()->className() << "slotReadyRead";
#endifconnecttimer.start();
}
void Ftp::replyfinished()
{
connecttimer.stop();#if DEBUG_LEVEL_FTP > LOG_ERRORS qDebug() << this->metaObject()->className() <<"replyfinished "; #endif if(reply) { #if DEBUG_LEVEL_FTP > LOG_ERRORS qDebug() << this->metaObject()->className() << "if reply "; #endif if(reply->bytesAvailable()) { QFile file(QApplication::applicationDirPath() +"/" + cf.GetProgramName() + TEMP_FILENAME_EXT); file.open(QIODevice::WriteOnly); #if DEBUG_LEVEL_FTP > LOG_ERRORS qDebug() << this->metaObject()->className() << "file write: " << file.write(reply->readAll()); #endif file.close(); } emit DownloadFinished(reply->error()); qDebug() << reply->errorString()<< " reply->error() " << reply->error(); if(reply->error() == 0) emit StatusUpdate("No error"); else emit StatusUpdate(reply->errorString()); } else { emit DownloadFinished(QNetworkReply::TimeoutError); emit StatusUpdate("the connection to the remote server timed out"); } reply->deleteLater(); reply = NULL;
}
void Ftp::slotError(QNetworkReply::NetworkError error)
{#if DEBUG_LEVEL_FTP > LOG_NOTHING qDebug() << this->metaObject()->className() <<"slotError: " << error; #endif if(error) { connecttimer.stop(); emit DownloadFinished(reply->error()); emit StatusUpdate(reply->errorString()); reply->deleteLater(); reply = NULL; }
}
@