1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example network/threadedfortuneserver
|
---|
30 | \title Threaded Fortune Server Example
|
---|
31 |
|
---|
32 | The Threaded Fortune Server example shows how to create a server for a
|
---|
33 | simple network service that uses threads to handle requests from different
|
---|
34 | clients. It is intended to be run alongside the Fortune Client example.
|
---|
35 |
|
---|
36 | \image threadedfortuneserver-example.png
|
---|
37 |
|
---|
38 | The implementation of this example is similar to that of the
|
---|
39 | \l{network/fortuneserver}{Fortune Server} example, but here we will
|
---|
40 | implement a subclass of QTcpServer that starts each connection in a
|
---|
41 | different thread.
|
---|
42 |
|
---|
43 | For this we need two classes: FortuneServer, a QTcpServer subclass, and
|
---|
44 | FortuneThread, which inherits QThread.
|
---|
45 |
|
---|
46 | \snippet examples/network/threadedfortuneserver/fortuneserver.h 0
|
---|
47 |
|
---|
48 | FortuneServer inherits QTcpServer and reimplements
|
---|
49 | QTcpServer::incomingConnection(). We also use it for storing the list of
|
---|
50 | random fortunes.
|
---|
51 |
|
---|
52 | \snippet examples/network/threadedfortuneserver/fortuneserver.cpp 0
|
---|
53 |
|
---|
54 | We use FortuneServer's constructor to simply generate the list of
|
---|
55 | fortunes.
|
---|
56 |
|
---|
57 | \snippet examples/network/threadedfortuneserver/fortuneserver.cpp 1
|
---|
58 |
|
---|
59 | Our implementation of QTcpServer::incomingConnection() creates a
|
---|
60 | FortuneThread object, passing the incoming socket descriptor and a random
|
---|
61 | fortune to FortuneThread's constructor. By connecting FortuneThread's
|
---|
62 | finished() signal to QObject::deleteLater(), we ensure that the thread
|
---|
63 | gets deleted once it has finished. We can then call QThread::start(),
|
---|
64 | which starts the thread.
|
---|
65 |
|
---|
66 | \snippet examples/network/threadedfortuneserver/fortunethread.h 0
|
---|
67 |
|
---|
68 | Moving on to the FortuneThread class, this is a QThread subclass whose job
|
---|
69 | is to write the fortune to the connected socket. The class reimplements
|
---|
70 | QThread::run(), and it has a signal for reporting errors.
|
---|
71 |
|
---|
72 | \snippet examples/network/threadedfortuneserver/fortunethread.cpp 0
|
---|
73 |
|
---|
74 | FortuneThread's constructor simply stores the socket descriptor and
|
---|
75 | fortune text, so that they are available for run() later on.
|
---|
76 |
|
---|
77 | \snippet examples/network/threadedfortuneserver/fortunethread.cpp 1
|
---|
78 |
|
---|
79 | The first thing our run() function does is to create a QTcpSocket object
|
---|
80 | on the stack. What's worth noticing is that we are creating this object
|
---|
81 | inside the thread, which automatically associates the socket to the
|
---|
82 | thread's event loop. This ensures that Qt will not try to deliver events
|
---|
83 | to our socket from the main thread while we are accessing it from
|
---|
84 | FortuneThread::run().
|
---|
85 |
|
---|
86 | \snippet examples/network/threadedfortuneserver/fortunethread.cpp 2
|
---|
87 |
|
---|
88 | The socket is initialized by calling QTcpSocket::setSocketDescriptor(),
|
---|
89 | passing our socket descriptor as an argument. We expect this to succeed,
|
---|
90 | but just to be sure, (although unlikely, the system may run out of
|
---|
91 | resources,) we catch the return value and report any error.
|
---|
92 |
|
---|
93 | \snippet examples/network/threadedfortuneserver/fortunethread.cpp 3
|
---|
94 |
|
---|
95 | As with the \l{network/fortuneserver}{Fortune Server} example, we encode
|
---|
96 | the fortune into a QByteArray using QDataStream.
|
---|
97 |
|
---|
98 | \snippet examples/network/threadedfortuneserver/fortunethread.cpp 4
|
---|
99 |
|
---|
100 | But unlike the previous example, we finish off by calling
|
---|
101 | QTcpSocket::waitForDisconnected(), which blocks the calling thread until
|
---|
102 | the socket has disconnected. Because we are running in a separate thread,
|
---|
103 | the GUI will remain responsive.
|
---|
104 |
|
---|
105 | \sa {Fortune Server Example}, {Fortune Client Example}, {Blocking Fortune
|
---|
106 | Client Example}
|
---|
107 | */
|
---|