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/fortuneserver
|
---|
30 | \title Fortune Server Example
|
---|
31 |
|
---|
32 | The Fortune Server example shows how to create a server for a simple
|
---|
33 | network service. It is intended to be run alongside the
|
---|
34 | \l{network/fortuneclient}{Fortune Client} example or the
|
---|
35 | \l{network/blockingfortuneclient}{Blocking Fortune Client} example.
|
---|
36 |
|
---|
37 | \image fortuneserver-example.png Screenshot of the Fortune Server example
|
---|
38 |
|
---|
39 | This example uses QTcpServer to accept incoming TCP connections, and a
|
---|
40 | simple QDataStream based data transfer protocol to write a fortune to the
|
---|
41 | connecting client (from the \l{network/fortuneclient}{Fortune Client}
|
---|
42 | example), before closing the connection.
|
---|
43 |
|
---|
44 | \snippet examples/network/fortuneserver/server.h 0
|
---|
45 |
|
---|
46 | The server is implemented using a simple class with only one slot, for
|
---|
47 | handling incoming connections.
|
---|
48 |
|
---|
49 | \snippet examples/network/fortuneserver/server.cpp 1
|
---|
50 |
|
---|
51 | In its constructor, our Server object calls QTcpServer::listen() to set up
|
---|
52 | a QTcpServer to listen on all addresses, on an arbitrary port. In then
|
---|
53 | displays the port QTcpServer picked in a label, so that user knows which
|
---|
54 | port the fortune client should connect to.
|
---|
55 |
|
---|
56 | \snippet examples/network/fortuneserver/server.cpp 2
|
---|
57 |
|
---|
58 | Our server generates a list of random fortunes that is can send to
|
---|
59 | connecting clients.
|
---|
60 |
|
---|
61 | \snippet examples/network/fortuneserver/server.cpp 3
|
---|
62 |
|
---|
63 | When a client connects to our server, QTcpServer will emit
|
---|
64 | QTcpServer::newConnection(). In turn, this will invoke our
|
---|
65 | sendFortune() slot:
|
---|
66 |
|
---|
67 | \snippet examples/network/fortuneserver/server.cpp 4
|
---|
68 |
|
---|
69 | The purpose of this slot is to select a random line from our list of
|
---|
70 | fortunes, encode it into a QByteArray using QDataStream, and then write it
|
---|
71 | to the connecting socket. This is a common way to transfer binary data
|
---|
72 | using QTcpSocket. First we create a QByteArray and a QDataStream object,
|
---|
73 | passing the bytearray to QDataStream's constructor. We then explicitly set
|
---|
74 | the protocol version of QDataStream to QDataStream::Qt_4_0 to ensure that
|
---|
75 | we can communicate with clients from future versions of Qt. (See
|
---|
76 | QDataStream::setVersion().)
|
---|
77 |
|
---|
78 | \snippet examples/network/fortuneserver/server.cpp 6
|
---|
79 |
|
---|
80 | At the start of our QByteArray, we reserve space for a 16 bit integer that
|
---|
81 | will contain the total size of the data block we are sending. We continue
|
---|
82 | by streaming in a random fortune. Then we seek back to the beginning of
|
---|
83 | the QByteArray, and overwrite the reserved 16 bit integer value with the
|
---|
84 | total size of the array. By doing this, we provide a way for clients to
|
---|
85 | verify how much data they can expect before reading the whole packet.
|
---|
86 |
|
---|
87 | \snippet examples/network/fortuneserver/server.cpp 7
|
---|
88 |
|
---|
89 | We then call QTcpServer::newPendingConnection(), which returns the
|
---|
90 | QTcpSocket representing the server side of the connection. By connecting
|
---|
91 | QTcpSocket::disconnected() to QObject::deleteLater(), we ensure that the
|
---|
92 | socket will be deleted after disconnecting.
|
---|
93 |
|
---|
94 | \snippet examples/network/fortuneserver/server.cpp 8
|
---|
95 |
|
---|
96 | The encoded fortune is written using QTcpSocket::write(), and we finally
|
---|
97 | call QTcpSocket::disconnectFromHost(), which will close the connection
|
---|
98 | after QTcpSocket has finished writing the fortune to the network. Because
|
---|
99 | QTcpSocket works asynchronously, the data will be written after this
|
---|
100 | function returns, and control goes back to Qt's event loop. The socket
|
---|
101 | will then close, which in turn will cause QObject::deleteLater() to delete
|
---|
102 | it.
|
---|
103 |
|
---|
104 | \sa {Fortune Client Example}, {Threaded Fortune Server Example}
|
---|
105 | */
|
---|