examples/network/fortuneserver/server.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 #include <QtNetwork>
       
    44 
       
    45 #include <stdlib.h>
       
    46 
       
    47 #include "server.h"
       
    48 
       
    49 Server::Server(QWidget *parent)
       
    50     : QDialog(parent)
       
    51 {
       
    52     statusLabel = new QLabel;
       
    53     quitButton = new QPushButton(tr("Quit"));
       
    54     quitButton->setAutoDefault(false);
       
    55 
       
    56 //! [0] //! [1]
       
    57     tcpServer = new QTcpServer(this);
       
    58     if (!tcpServer->listen()) {
       
    59         QMessageBox::critical(this, tr("Fortune Server"),
       
    60                               tr("Unable to start the server: %1.")
       
    61                               .arg(tcpServer->errorString()));
       
    62         close();
       
    63         return;
       
    64     }
       
    65 //! [0]
       
    66     QString ipAddress;
       
    67     QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
       
    68     // use the first non-localhost IPv4 address
       
    69     for (int i = 0; i < ipAddressesList.size(); ++i) {
       
    70         if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
       
    71             ipAddressesList.at(i).toIPv4Address())
       
    72             ipAddress = ipAddressesList.at(i).toString();
       
    73     }
       
    74     // if we did not find one, use IPv4 localhost
       
    75     if (ipAddress.isEmpty())
       
    76         ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
       
    77     statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
       
    78                             "Run the Fortune Client example now.")
       
    79                          .arg(ipAddress).arg(tcpServer->serverPort()));
       
    80 //! [1]
       
    81 
       
    82 //! [2]
       
    83     fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
       
    84              << tr("You've got to think about tomorrow.")
       
    85              << tr("You will be surprised by a loud noise.")
       
    86              << tr("You will feel hungry again in another hour.")
       
    87              << tr("You might have mail.")
       
    88              << tr("You cannot kill time without injuring eternity.")
       
    89              << tr("Computers are not intelligent. They only think they are.");
       
    90 //! [2]
       
    91 
       
    92     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
       
    93 //! [3]
       
    94     connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
       
    95 //! [3]
       
    96 
       
    97     QHBoxLayout *buttonLayout = new QHBoxLayout;
       
    98     buttonLayout->addStretch(1);
       
    99     buttonLayout->addWidget(quitButton);
       
   100     buttonLayout->addStretch(1);
       
   101 
       
   102     QVBoxLayout *mainLayout = new QVBoxLayout;
       
   103     mainLayout->addWidget(statusLabel);
       
   104     mainLayout->addLayout(buttonLayout);
       
   105     setLayout(mainLayout);
       
   106 
       
   107     setWindowTitle(tr("Fortune Server"));
       
   108 }
       
   109 
       
   110 //! [4]
       
   111 void Server::sendFortune()
       
   112 {
       
   113 //! [5]
       
   114     QByteArray block;
       
   115     QDataStream out(&block, QIODevice::WriteOnly);
       
   116     out.setVersion(QDataStream::Qt_4_0);
       
   117 //! [4] //! [6]
       
   118     out << (quint16)0;
       
   119     out << fortunes.at(qrand() % fortunes.size());
       
   120     out.device()->seek(0);
       
   121     out << (quint16)(block.size() - sizeof(quint16));
       
   122 //! [6] //! [7]
       
   123 
       
   124     QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
       
   125     connect(clientConnection, SIGNAL(disconnected()),
       
   126             clientConnection, SLOT(deleteLater()));
       
   127 //! [7] //! [8]
       
   128 
       
   129     clientConnection->write(block);
       
   130     clientConnection->disconnectFromHost();
       
   131 //! [5]
       
   132 }
       
   133 //! [8]