C++ GUI Programming with Qt4: Networking
15. Networking
- Writing FTP Clients
- Writing HTTP Clients
- Writing TCP Client–Server Applications
- Sending and Receiving UDP Datagrams
Qt provides the QFtp and QHttp classes for working with FTP and HTTP. These protocols are easy to use for downloading and uploading files and, in the case of HTTP, for sending requests to web servers and retrieving the results.
Qt also provides the lower-level QTcpSocket and QUdpSocket classes, which implement the TCP and UDP transport protocols. TCP is a reliable connection-oriented protocol that operates in terms of data streams transmitted between network nodes, and UDP is an unreliable connectionless protocol based on discrete packets sent between network nodes. Both can be used to create network client and server applications. For servers, we also need the QTcpServer class to handle incoming TCP connections. Secure SSL/TLS connections can be established by using QSslSocket instead of QTcpSocket.
Writing FTP Clients
The QFtp class implements the client side of the FTP protocol in Qt. It offers various functions to perform the most common FTP operations and lets us execute arbitrary FTP commands.
The QFtp class works asynchronously. When we call a function such as get() or put(), it returns immediately and the data transfer occurs when control passes back to Qt's event loop. This ensures that the user interface remains responsive while FTP commands are executed.
We will start with an example that shows how to retrieve a single file using get(). The example is a console application called ftpget that downloads the remote file specified on the command line. Let's begin with the main() function:
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QStringList args = QCoreApplication::arguments(); if (args.count() != 2) { std::cerr << "Usage: ftpget url" << std::endl << "Example:" << std::endl << " ftpget ftp://ftp.trolltech.com/mirrors" << std::endl; return 1; } FtpGet getter; if (!getter.getFile(QUrl(args[1]))) return 1; QObject::connect(&getter, SIGNAL(done()), &app, SLOT(quit())); return app.exec(); }
We create a QCoreApplication rather than its subclass QApplication to avoid linking in the QtGui library. The QCoreApplication::arguments() function returns the command-line arguments as a QStringList, with the first item being the name the program was invoked as, and any Qt-specific arguments such as -style removed. The heart of the main() function is the construction of the FtpGet object and the getFile() call. If the call succeeds, we let the event loop run until the download finishes.
All the work is done by the FtpGet subclass, which is defined as follows:
class FtpGet : public QObject { Q_OBJECT public: FtpGet(QObject *parent = 0); bool getFile(const QUrl &url); signals: void done(); private slots: void ftpDone(bool error); private: QFtp ftp; QFile file; };
The class has a public function, getFile(), that retrieves the file specified by a URL. The QUrl class provides a high-level interface for extracting the different parts of a URL, such as the file name, path, protocol, and port.
FtpGet has a private slot, ftpDone(), that is called when the file transfer is completed, and a done() signal that it emits when the file has been downloaded. The class also has two private variables: the ftp variable, of type QFtp, which encapsulates the connection to an FTP server, and the file variable that is used for writing the downloaded file to disk.
FtpGet::FtpGet(QObject *parent) : QObject(parent) { connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool))); }
In the constructor, we connect the QFtp::done(bool) signal to our ftpDone(bool) private slot. QFtp emits done(bool) when it has finished processing all requests. The bool parameter indicates whether an error occurred or not.
bool FtpGet::getFile(const QUrl &url) { if (!url.isValid()) { std::cerr << "Error: Invalid URL" << std::endl; return false; } if (url.scheme() != "ftp") { std::cerr << "Error: URL must start with 'ftp:'" << std::endl; return false; } if (url.path().isEmpty()) { std::cerr << "Error: URL has no path" << std::endl; return false; } QString localFileName = QFileInfo(url.path()).fileName(); if (localFileName.isEmpty()) localFileName = "ftpget.out"; file.setFileName(localFileName); if (!file.open(QIODevice::WriteOnly)) { std::cerr << "Error: Cannot write file " << qPrintable(file.fileName()) << ": " << qPrintable(file.errorString()) << std::endl; return false; } ftp.connectToHost(url.host(), url.port(21)); ftp.login(); ftp.get(url.path(), &file); ftp.close(); return true; }
The getFile() function begins by checking the URL that was passed in. If a problem is encountered, the function prints an error message to cerr and returns false to indicate that the download failed.
Instead of forcing the user to make up a local file name, we try to create a sensible name using the URL itself, with a fallback of ftpget.out. If we fail to open the file, we print an error message and return false.
Next, we execute a sequence of four FTP commands using our QFtp object. The url.port(21) call returns the port number specified in the URL, or port 21 if none is specified in the URL itself. Since no user name or password is given to the login() function, an anonymous login is attempted. The second argument to get() specifies the output I/O device.
The FTP commands are queued and executed in Qt's event loop. The completion of all the commands is indicated by QFtp's done(bool) signal, which we connected to ftpDone(bool) in the constructor.
void FtpGet::ftpDone(bool error) { if (error) { std::cerr << "Error: " << qPrintable(ftp.errorString()) << std::endl; } else { std::cerr << "File downloaded as " << qPrintable(file.fileName()) << std::endl; } file.close(); emit done(); }
Once the FTP commands have been executed, we close the file and emit our own done() signal. It may appear strange that we close the file here, rather than after the ftp.close() call at the end of the getFile() function, but remember that the FTP commands are executed asynchronously and may well be in progress after the getFile() function has returned. Only when the QFtp object's done() signal is emitted do we know that the download is finished and that it is safe to close the file.
QFtp provides several FTP commands, including connectToHost(), login(), close(), list(), cd(), get(), put(), remove(), mkdir(), rmdir(), and rename(). All of these functions schedule an FTP command and return an ID number that identifies the command. It is also possible to control the transfer mode (the default is passive) and the transfer type (the default is binary).
Arbitrary FTP commands can be executed using rawCommand(). For example, here's how to execute a SITE CHMOD command:
ftp.rawCommand("SITE CHMOD 755 fortune");
QFtp emits the commandStarted(int) signal when it starts executing a command, and it emits the commandFinished(int, bool) signal when the command is finished. The int parameter is the ID number that identifies the command. If we are interested in the fate of individual commands, we can store the ID numbers when we schedule the commands. Keeping track of the ID numbers allows us to provide detailed feedback to the user. For example:
bool FtpGet::getFile(const QUrl &url) { ... connectId = ftp.connectToHost(url.host(), url.port(21)); loginId = ftp.login(); getId = ftp.get(url.path(), &file); closeId = ftp.close(); return true; } void FtpGet::ftpCommandStarted(int id) { if (id == connectId) { std::cerr << "Connecting..." << std::endl; } else if (id == loginId) { std::cerr << "Logging in..." << std::endl; ... }
Another way to provide feedback is to connect to QFtp's stateChanged() signal, which is emitted whenever the connection enters a new state (QFtp::Connecting, QFtp::Connected, QFtp::LoggedIn, etc.).
In most applications, we are interested only in the fate of the sequence of commands as a whole rather than in particular commands. In such cases, we can simply connect to the done(bool) signal, which is emitted whenever the command queue becomes empty.
When an error occurs, QFtp automatically clears the command queue. This means that if the connection or the login fails, the commands that follow in the queue are never executed. If we schedule new commands after the error has occurred using the same QFtp object, these commands will be queued and executed.
In the application's .pro file, we need the following line to link against the QtNetwork library:
QT += network
We will now review a more advanced example. The spider command-line program downloads all the files located in an FTP directory, recursively downloading from all the directory's subdirectories. The network logic is located in the Spider class:
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; };
The starting directory is specified as a QUrl and is set using the getDirectory() function.
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 &))); }
In the constructor, we establish two signal–slot connections. The listInfo(const QUrlInfo &) signal is emitted by QFtp when we request a directory listing (in getDirectory()) for each file that it retrieves. This signal is connected to a slot called ftpListInfo(), which downloads the file associated with the URL it is given.
bool Spider::getDirectory(const QUrl &url) { if (!url.isValid()) { std::cerr << "Error: Invalid URL" << std::endl; return false; } if (url.scheme() != "ftp") { std::cerr << "Error: URL must start with 'ftp:'" << std::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; }
When the getDirectory() function is called, it begins by doing some sanity checks, and if all is well, it attempts to establish an FTP connection. It keeps track of the paths that it must process and calls processNextDirectory() to start downloading the root directory.
void Spider::processNextDirectory() { if (!pendingDirs.isEmpty()) { currentDir = pendingDirs.takeFirst(); currentLocalDir = "downloads/" + currentDir; QDir(".").mkpath(currentLocalDir); ftp.cd(currentDir); ftp.list(); } else { emit done(); } }
The processNextDirectory() function takes the first remote directory out of the pendingDirs list and creates a corresponding directory in the local file system. It then tells the QFtp object to change to the taken directory and to list its files. For every file that list() processes, it emits a listInfo() signal that causes the ftpListInfo() slot to be called.
If there are no more directories to process, the function emits the done() signal to indicate that the downloading is complete.
void Spider::ftpListInfo(const QUrlInfo &urlInfo) { if (urlInfo.isFile()) { if (urlInfo.isReadable()) { QFile *file = new QFile(currentLocalDir + "/" + urlInfo.name()); if (!file->open(QIODevice::WriteOnly)) { std::cerr << "Warning: Cannot write file " << qPrintable(QDir::toNativeSeparators( file->fileName())) << ": " << qPrintable(file->errorString()) << std::endl; return; } ftp.get(urlInfo.name(), file); openedFiles.append(file); } } else if (urlInfo.isDir() && !urlInfo.isSymLink()) { pendingDirs.append(currentDir + "/" + urlInfo.name()); } }
The ftpListInfo() slot's urlInfo parameter provides detailed information about a remote file. If the file is a normal file (not a directory) and is readable, we call get() to download it. The QFile object used for downloading is allocated using new and a pointer to it is stored in the openedFiles list.
If the QUrlInfo holds the details of a remote directory that is not a symbolic link, we add this directory to the pendingDirs list. We skip symbolic links because they can easily lead to infinite recursion.
void Spider::ftpDone(bool error) { if (error) { std::cerr << "Error: " << qPrintable(ftp.errorString()) << std::endl; } else { std::cout << "Downloaded " << qPrintable(currentDir) << " to " << qPrintable(QDir::toNativeSeparators( QDir(currentLocalDir).canonicalPath())); } qDeleteAll(openedFiles); openedFiles.clear(); processNextDirectory(); }
The ftpDone() slot is called when all the FTP commands have finished or if an error occurred. We delete the QFile objects to prevent memory leaks and to close each file. Finally, we call processNextDirectory(). If there are any directories left, the whole process begins again with the next directory in the list; otherwise, the downloading stops and done() is emitted.
If there are no errors, the sequence of FTP commands and signals is as follows:
connectToHost(host, port) login() cd(directory_1) list() emit listInfo(file_1_1) get(file_1_1) emit listInfo(file_1_2) get(file_1_2) ... emit done() ... cd(directory_N) list() emit listInfo(file_N_1) get(file_N_1) emit listInfo(file_N_2) get(file_N_2) ... emit done()
If a file is in fact a directory, it is added to the pendingDirs list, and when the last file of the current list() command has been downloaded, a new cd() command is issued, followed by a new list() command with the next pending directory, and the whole process begins again with the new directory. This is repeated, with new files being downloaded, and new directories added to the pendingDirs list, until every file has been downloaded from every directory, at which point the pendingDirs list will finally be empty.
If a network error occurs while downloading the fifth of, say, 20 files in a directory, the remaining files will not be downloaded. If we wanted to download as many files as possible, one solution would be to schedule the GET operations one at a time and to wait for the done(bool) signal before scheduling a new GET operation. In listInfo(), we would simply append the file name to a QStringList, instead of calling get() right away, and in done(bool) we would call get() on the next file to download in the QStringList. The sequence of execution would then look like this:
connectToHost(host, port) login() cd(directory_1) list() ... cd(directory_N) list() emit listInfo(file_1_1) emit listInfo(file_1_2) ... emit listInfo(file_N_1) emit listInfo(file_N_2) ... emit done() get(file_1_1) emit done() get(file_1_2) emit done() ... get(file_N_1) emit done() get(file_N_2) emit done() ...
Another solution would be to use one QFtp object per file. This would enable us to download the files in parallel, through separate FTP connections.
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QStringList args = QCoreApplication::arguments(); if (args.count() != 2) { std::cerr << "Usage: spider url" << std::endl << "Example:" << std::endl << " spider ftp://ftp.trolltech.com/freebies/" << "leafnode" << std::endl; return 1; } Spider spider; if (!spider.getDirectory(QUrl(args[1]))) return 1; QObject::connect(&spider, SIGNAL(done()), &app, SLOT(quit())); return app.exec(); }
The main() function completes the program. If the user does not specify a URL on the command line, we give an error message and terminate the program.
In both FTP examples, the data retrieved using get() was written to a QFile. This need not be the case. If we wanted the data in memory, we could use a QBuffer, the QIODevice subclass that wraps a QByteArray. For example:
QBuffer *buffer = new QBuffer; buffer->open(QIODevice::WriteOnly); ftp.get(urlInfo.name(), buffer);
We could also omit the I/O device argument to get() or pass a null pointer. The QFtp class then emits a readyRead() signal every time new data is available, and the data can be read using read() or readAll().