PDA

View Full Version : what should I give url?



sups
20th February 2011, 17:31
Hi,

I
have some query about qt programming in networking.
I am doing my project for writing the client side application for ftp
connection.
1)I dont get any error but after running it says "exited with code
1"....
2)I am not getting what url should I give to connect to the FTP server
which I have installed VSFTPD server for ubuntu in my computer..PLZ
someone help me..

Waiting for reply...
Thank You.

Below is the source code...

//client.cpp

#include "client.h"
#include <iostream>
using namespace std;
client::client(QWidget *parent)
: QWidget(parent)
{
}

client::~client()
{

}

Spider::Spider(QObject *parent)
: QObject(parent)
{
connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
connect(&ftp, SIGNAL(listInfo(const QUrlInfo &)),
this, SLOT(ftpListInfo(const QUrlInfo &)));
}

bool Spider::getDirectory(const QUrl &url)
{
if (!url.isValid()) {
cerr << "Error: Invalid URL" << endl;
return false;
}

if (url.scheme() != "ftp") {
cerr << "Error: URL must start with ’ftp:’" << endl;
return false;
}

ftp.connectToHost(url.host(), url.port(21));
ftp.login();
QString path = url.path();
if (path.isEmpty())
path = "/";
pendingDirs.append(path);
processNextDirectory();
return true;
}



void Spider::processNextDirectory()
{
if (!pendingDirs.isEmpty()) {
currentDir = pendingDirs.takeFirst();
currentLocalDir = "downloads/" + currentDir;
QDir(".").mkpath(currentLocalDir);
ftp.cd(currentDir);
ftp.list();
} else {
emit done();
}
}


void Spider::ftpListInfo(const QUrlInfo &urlInfo)
{
if (urlInfo.isFile()) {
if (urlInfo.isReadable()) {
QFile *file = new QFile(currentLocalDir + "/"+ urlInfo.name());
if (!file->open(QIODevice::WriteOnly)) {
cerr << "Warning: Cannot open file "
<< qPrintable(
QDir::convertSeparators(file->fileName()))
<< endl;
return;
}
ftp.get(urlInfo.name(), file);
openedFiles.append(file);
}
} else if (urlInfo.isDir() && !urlInfo.isSymLink()) {
pendingDirs.append(currentDir + "/" + urlInfo.name());
}
}
void Spider::ftpDone(bool error)
{

if (error) {
cerr << "Error: " << qPrintable(ftp.errorString()) <<
endl;
} else {
cout << "Downloaded " << qPrintable(currentDir) << "
to "<<

qPrintable(QDir::convertSeparators(QDir(currentLoc alDir).canonicalPath()));
}
qDeleteAll(openedFiles);
openedFiles.clear();
processNextDirectory();
}


//main.cpp

int
main(int argc, char *argv[])
{

QCoreApplication app(argc, argv);
QStringList args = app.arguments();
if (args.count() != 2) {
cerr << "Usage: spider url" << endl
<< "Example:" << endl
<< "spider ftp://ftp.trolltech.com/freebies/leafnode"<<
endl;
return 1;
}

Spider spider;
if (!spider.getDirectory(QUrl("localhost"))) // what should I give the
url???
return 1;
QObject::connect(&spider, SIGNAL(done()), &app, SLOT(quit()));
cout << "completing";
return app.exec();
}

//client.h

#ifndef CLIENT_H
#define CLIENT_H
#include <QUrl>
#include <QtGui/QWidget>
#include <QUrlInfo>
#include <QFtp>
#include <QFile>
#include <QDir>
class client : public QWidget
{
Q_OBJECT

public:
client(QWidget *parent = 0);
~client();
};

class Spider : public QObject
{
Q_OBJECT
public:
Spider(QObject *parent = 0);
bool getDirectory(const QUrl &url);
signals:
void done();
private slots:
void ftpDone(bool error);
void ftpListInfo(const QUrlInfo &urlInfo);
private:
void processNextDirectory();

QFtp ftp;
QList<QFile *> openedFiles;
QString currentDir;
QString currentLocalDir;
QStringList pendingDirs;
};

#endif // CLIENT_H

ChrisW67
20th February 2011, 23:22
Please edit your post and put your code in [code] tags.


1)I dont get any error but after running it says "exited with code
1"....
This is exactly what you tell C++ to return to the operating system when main is terminating.


2) I am not getting what url should I give to connect to the FTP server
which I have installed VSFTPD server for ubuntu in my computer..PLZ
someone help me..

An FTP URL is built exactly the same way as every other URL:
http://en.wikipedia.org/wiki/Uniform_Resource_Locator (http://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax)
Use the "ftp:" scheme and path.

QUrl is very useful when assembing/disassembling URLs.