2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2013-01-02 11:13:29 +00:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-09-19 12:28:29 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the documentation of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:FDL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Free Documentation License Usage
|
2011-04-27 10:05:43 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Free
|
|
|
|
** Documentation License version 1.3 as published by the Free Software
|
2011-05-24 09:34:08 +00:00
|
|
|
** Foundation and appearing in the file included in the packaging of
|
2012-09-19 12:28:29 +00:00
|
|
|
** this file. Please review the following information to ensure
|
|
|
|
** the GNU Free Documentation License version 1.3 requirements
|
|
|
|
** will be met: http://www.gnu.org/copyleft/fdl.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
2012-09-18 18:32:53 +00:00
|
|
|
\example threadedfortuneserver
|
2011-04-27 10:05:43 +00:00
|
|
|
\title Threaded Fortune Server Example
|
2012-12-04 13:06:35 +00:00
|
|
|
\ingroup examples-network
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-11-28 16:04:12 +00:00
|
|
|
\brief The Threaded Fortune Server example shows how to create a server for a
|
2011-04-27 10:05:43 +00:00
|
|
|
simple network service that uses threads to handle requests from different
|
|
|
|
clients. It is intended to be run alongside the Fortune Client example.
|
|
|
|
|
|
|
|
\image threadedfortuneserver-example.png
|
|
|
|
|
|
|
|
The implementation of this example is similar to that of the
|
2012-09-18 18:32:53 +00:00
|
|
|
\l{fortuneserver}{Fortune Server} example, but here we will
|
2011-04-27 10:05:43 +00:00
|
|
|
implement a subclass of QTcpServer that starts each connection in a
|
|
|
|
different thread.
|
|
|
|
|
|
|
|
For this we need two classes: FortuneServer, a QTcpServer subclass, and
|
|
|
|
FortuneThread, which inherits QThread.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortuneserver.h 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
FortuneServer inherits QTcpServer and reimplements
|
|
|
|
QTcpServer::incomingConnection(). We also use it for storing the list of
|
2013-03-14 23:42:15 +00:00
|
|
|
random fortunes.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortuneserver.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
We use FortuneServer's constructor to simply generate the list of
|
|
|
|
fortunes.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortuneserver.cpp 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Our implementation of QTcpServer::incomingConnection() creates a
|
|
|
|
FortuneThread object, passing the incoming socket descriptor and a random
|
|
|
|
fortune to FortuneThread's constructor. By connecting FortuneThread's
|
|
|
|
finished() signal to QObject::deleteLater(), we ensure that the thread
|
|
|
|
gets deleted once it has finished. We can then call QThread::start(),
|
|
|
|
which starts the thread.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortunethread.h 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Moving on to the FortuneThread class, this is a QThread subclass whose job
|
|
|
|
is to write the fortune to the connected socket. The class reimplements
|
|
|
|
QThread::run(), and it has a signal for reporting errors.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortunethread.cpp 0
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
FortuneThread's constructor simply stores the socket descriptor and
|
|
|
|
fortune text, so that they are available for run() later on.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortunethread.cpp 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The first thing our run() function does is to create a QTcpSocket object
|
|
|
|
on the stack. What's worth noticing is that we are creating this object
|
|
|
|
inside the thread, which automatically associates the socket to the
|
|
|
|
thread's event loop. This ensures that Qt will not try to deliver events
|
|
|
|
to our socket from the main thread while we are accessing it from
|
|
|
|
FortuneThread::run().
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortunethread.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The socket is initialized by calling QTcpSocket::setSocketDescriptor(),
|
|
|
|
passing our socket descriptor as an argument. We expect this to succeed,
|
|
|
|
but just to be sure, (although unlikely, the system may run out of
|
|
|
|
resources,) we catch the return value and report any error.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortunethread.cpp 3
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
As with the \l{fortuneserver}{Fortune Server} example, we encode
|
2011-04-27 10:05:43 +00:00
|
|
|
the fortune into a QByteArray using QDataStream.
|
|
|
|
|
2012-09-18 18:32:53 +00:00
|
|
|
\snippet threadedfortuneserver/fortunethread.cpp 4
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
But unlike the previous example, we finish off by calling
|
|
|
|
QTcpSocket::waitForDisconnected(), which blocks the calling thread until
|
|
|
|
the socket has disconnected. Because we are running in a separate thread,
|
2013-03-14 23:42:15 +00:00
|
|
|
the GUI will remain responsive.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa {Fortune Server Example}, {Fortune Client Example}, {Blocking Fortune
|
|
|
|
Client Example}
|
|
|
|
*/
|