Results 1 to 2 of 2

Thread: what should I give url?

  1. #1
    Join Date
    Feb 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question what should I give url?

    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:rocessNextDirectory()
    {
    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: what should I give url?

    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
    Use the "ftp:" scheme and path.

    QUrl is very useful when assembing/disassembling URLs.
    Last edited by ChrisW67; 20th February 2011 at 23:29.

Similar Threads

  1. How to give QextSerialPort example a Gui?
    By Jeroenbruinsma in forum Newbie
    Replies: 1
    Last Post: 5th March 2012, 01:40
  2. Replies: 2
    Last Post: 14th January 2011, 08:55
  3. Hi all. I give up (not really, but yes, kind of)
    By tachoknight in forum Newbie
    Replies: 1
    Last Post: 4th December 2010, 02:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.