| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 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
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \example network/fortuneserver
|
|---|
| 44 | \title Fortune Server Example
|
|---|
| 45 |
|
|---|
| 46 | The Fortune Server example shows how to create a server for a simple
|
|---|
| 47 | network service. It is intended to be run alongside the
|
|---|
| 48 | \l{network/fortuneclient}{Fortune Client} example or the
|
|---|
| 49 | \l{network/blockingfortuneclient}{Blocking Fortune Client} example.
|
|---|
| 50 |
|
|---|
| 51 | \image fortuneserver-example.png Screenshot of the Fortune Server example
|
|---|
| 52 |
|
|---|
| 53 | This example uses QTcpServer to accept incoming TCP connections, and a
|
|---|
| 54 | simple QDataStream based data transfer protocol to write a fortune to the
|
|---|
| 55 | connecting client (from the \l{network/fortuneclient}{Fortune Client}
|
|---|
| 56 | example), before closing the connection.
|
|---|
| 57 |
|
|---|
| 58 | \snippet examples/network/fortuneserver/server.h 0
|
|---|
| 59 |
|
|---|
| 60 | The server is implemented using a simple class with only one slot, for
|
|---|
| 61 | handling incoming connections.
|
|---|
| 62 |
|
|---|
| 63 | \snippet examples/network/fortuneserver/server.cpp 1
|
|---|
| 64 |
|
|---|
| 65 | In its constructor, our Server object calls QTcpServer::listen() to set up
|
|---|
| 66 | a QTcpServer to listen on all addresses, on an arbitrary port. In then
|
|---|
| 67 | displays the port QTcpServer picked in a label, so that user knows which
|
|---|
| 68 | port the fortune client should connect to.
|
|---|
| 69 |
|
|---|
| 70 | \snippet examples/network/fortuneserver/server.cpp 2
|
|---|
| 71 |
|
|---|
| 72 | Our server generates a list of random fortunes that is can send to
|
|---|
| 73 | connecting clients.
|
|---|
| 74 |
|
|---|
| 75 | \snippet examples/network/fortuneserver/server.cpp 3
|
|---|
| 76 |
|
|---|
| 77 | When a client connects to our server, QTcpServer will emit
|
|---|
| 78 | QTcpServer::newConnection(). In turn, this will invoke our
|
|---|
| 79 | sendFortune() slot:
|
|---|
| 80 |
|
|---|
| 81 | \snippet examples/network/fortuneserver/server.cpp 4
|
|---|
| 82 |
|
|---|
| 83 | The purpose of this slot is to select a random line from our list of
|
|---|
| 84 | fortunes, encode it into a QByteArray using QDataStream, and then write it
|
|---|
| 85 | to the connecting socket. This is a common way to transfer binary data
|
|---|
| 86 | using QTcpSocket. First we create a QByteArray and a QDataStream object,
|
|---|
| 87 | passing the bytearray to QDataStream's constructor. We then explicitly set
|
|---|
| 88 | the protocol version of QDataStream to QDataStream::Qt_4_0 to ensure that
|
|---|
| 89 | we can communicate with clients from future versions of Qt. (See
|
|---|
| 90 | QDataStream::setVersion().)
|
|---|
| 91 |
|
|---|
| 92 | \snippet examples/network/fortuneserver/server.cpp 6
|
|---|
| 93 |
|
|---|
| 94 | At the start of our QByteArray, we reserve space for a 16 bit integer that
|
|---|
| 95 | will contain the total size of the data block we are sending. We continue
|
|---|
| 96 | by streaming in a random fortune. Then we seek back to the beginning of
|
|---|
| 97 | the QByteArray, and overwrite the reserved 16 bit integer value with the
|
|---|
| 98 | total size of the array. By doing this, we provide a way for clients to
|
|---|
| 99 | verify how much data they can expect before reading the whole packet.
|
|---|
| 100 |
|
|---|
| 101 | \snippet examples/network/fortuneserver/server.cpp 7
|
|---|
| 102 |
|
|---|
| 103 | We then call QTcpServer::newPendingConnection(), which returns the
|
|---|
| 104 | QTcpSocket representing the server side of the connection. By connecting
|
|---|
| 105 | QTcpSocket::disconnected() to QObject::deleteLater(), we ensure that the
|
|---|
| 106 | socket will be deleted after disconnecting.
|
|---|
| 107 |
|
|---|
| 108 | \snippet examples/network/fortuneserver/server.cpp 8
|
|---|
| 109 |
|
|---|
| 110 | The encoded fortune is written using QTcpSocket::write(), and we finally
|
|---|
| 111 | call QTcpSocket::disconnectFromHost(), which will close the connection
|
|---|
| 112 | after QTcpSocket has finished writing the fortune to the network. Because
|
|---|
| 113 | QTcpSocket works asynchronously, the data will be written after this
|
|---|
| 114 | function returns, and control goes back to Qt's event loop. The socket
|
|---|
| 115 | will then close, which in turn will cause QObject::deleteLater() to delete
|
|---|
| 116 | it.
|
|---|
| 117 |
|
|---|
| 118 | \sa {Fortune Client Example}, {Threaded Fortune Server Example}
|
|---|
| 119 | */
|
|---|