source: trunk/examples/network/fortuneserver/server.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 4.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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 examples 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#include <QtGui>
43#include <QtNetwork>
44
45#include <stdlib.h>
46
47#include "server.h"
48
49Server::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 break;
74 }
75 }
76 // if we did not find one, use IPv4 localhost
77 if (ipAddress.isEmpty())
78 ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
79 statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
80 "Run the Fortune Client example now.")
81 .arg(ipAddress).arg(tcpServer->serverPort()));
82//! [1]
83
84//! [2]
85 fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
86 << tr("You've got to think about tomorrow.")
87 << tr("You will be surprised by a loud noise.")
88 << tr("You will feel hungry again in another hour.")
89 << tr("You might have mail.")
90 << tr("You cannot kill time without injuring eternity.")
91 << tr("Computers are not intelligent. They only think they are.");
92//! [2]
93
94 connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
95//! [3]
96 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
97//! [3]
98
99 QHBoxLayout *buttonLayout = new QHBoxLayout;
100 buttonLayout->addStretch(1);
101 buttonLayout->addWidget(quitButton);
102 buttonLayout->addStretch(1);
103
104 QVBoxLayout *mainLayout = new QVBoxLayout;
105 mainLayout->addWidget(statusLabel);
106 mainLayout->addLayout(buttonLayout);
107 setLayout(mainLayout);
108
109 setWindowTitle(tr("Fortune Server"));
110}
111
112//! [4]
113void Server::sendFortune()
114{
115//! [5]
116 QByteArray block;
117 QDataStream out(&block, QIODevice::WriteOnly);
118 out.setVersion(QDataStream::Qt_4_0);
119//! [4] //! [6]
120 out << (quint16)0;
121 out << fortunes.at(qrand() % fortunes.size());
122 out.device()->seek(0);
123 out << (quint16)(block.size() - sizeof(quint16));
124//! [6] //! [7]
125
126 QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
127 connect(clientConnection, SIGNAL(disconnected()),
128 clientConnection, SLOT(deleteLater()));
129//! [7] //! [8]
130
131 clientConnection->write(block);
132 clientConnection->disconnectFromHost();
133//! [5]
134}
135//! [8]
Note: See TracBrowser for help on using the repository browser.