A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FQt%2FQTcpServer</id>
		<title>C++/Qt/QTcpServer - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FQt%2FQTcpServer"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QTcpServer&amp;action=history"/>
		<updated>2026-04-18T22:23:39Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QTcpServer&amp;diff=1210&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QTcpServer&amp;diff=1210&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QTcpServer&amp;diff=1211&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QTcpServer&amp;diff=1211&amp;oldid=prev"/>
				<updated>2010-05-25T10:25:09Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Loopback QTcpServer and QTcpSocket==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
/****************************************************************************&lt;br /&gt;
**&lt;br /&gt;
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).&lt;br /&gt;
** All rights reserved.&lt;br /&gt;
** Contact: Nokia Corporation (qt-info@nokia.com)&lt;br /&gt;
**&lt;br /&gt;
** This file is part of the examples of the Qt Toolkit.&lt;br /&gt;
**&lt;br /&gt;
** $QT_BEGIN_LICENSE:LGPL$&lt;br /&gt;
** Commercial Usage&lt;br /&gt;
** Licensees holding valid Qt Commercial licenses may use this file in&lt;br /&gt;
** accordance with the Qt Commercial License Agreement provided with the&lt;br /&gt;
** Software or, alternatively, in accordance with the terms contained in&lt;br /&gt;
** a written agreement between you and Nokia.&lt;br /&gt;
**&lt;br /&gt;
** GNU Lesser General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU Lesser&lt;br /&gt;
** General Public License version 2.1 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.LGPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU Lesser General Public License version 2.1 requirements&lt;br /&gt;
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.&lt;br /&gt;
**&lt;br /&gt;
** In addition, as a special exception, Nokia gives you certain additional&lt;br /&gt;
** rights.  These rights are described in the Nokia Qt LGPL Exception&lt;br /&gt;
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.&lt;br /&gt;
**&lt;br /&gt;
** GNU General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU&lt;br /&gt;
** General Public License version 3.0 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.GPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU General Public License version 3.0 requirements will be&lt;br /&gt;
** met: http://www.gnu.org/copyleft/gpl.html.&lt;br /&gt;
**&lt;br /&gt;
** If you have questions regarding the use of this file, please contact&lt;br /&gt;
** Nokia at qt-info@nokia.com.&lt;br /&gt;
** $QT_END_LICENSE$&lt;br /&gt;
**&lt;br /&gt;
****************************************************************************/&lt;br /&gt;
#ifndef DIALOG_H&lt;br /&gt;
#define DIALOG_H&lt;br /&gt;
#include &amp;lt;QDialog&amp;gt;&lt;br /&gt;
#include &amp;lt;QTcpServer&amp;gt;&lt;br /&gt;
#include &amp;lt;QTcpSocket&amp;gt;&lt;br /&gt;
QT_BEGIN_NAMESPACE&lt;br /&gt;
class QDialogButtonBox;&lt;br /&gt;
class QLabel;&lt;br /&gt;
class QProgressBar;&lt;br /&gt;
class QPushButton;&lt;br /&gt;
class QTcpServer;&lt;br /&gt;
class QTcpSocket;&lt;br /&gt;
QT_END_NAMESPACE&lt;br /&gt;
class Dialog : public QDialog&lt;br /&gt;
{&lt;br /&gt;
    Q_OBJECT&lt;br /&gt;
public:&lt;br /&gt;
    Dialog(QWidget *parent = 0);&lt;br /&gt;
public slots:&lt;br /&gt;
    void start();&lt;br /&gt;
    void acceptConnection();&lt;br /&gt;
    void startTransfer();&lt;br /&gt;
    void updateServerProgress();&lt;br /&gt;
    void updateClientProgress(qint64 numBytes);&lt;br /&gt;
    void displayError(QAbstractSocket::SocketError socketError);&lt;br /&gt;
private:&lt;br /&gt;
    QProgressBar *clientProgressBar;&lt;br /&gt;
    QProgressBar *serverProgressBar;&lt;br /&gt;
    QLabel *clientStatusLabel;&lt;br /&gt;
    QLabel *serverStatusLabel;&lt;br /&gt;
    QPushButton *startButton;&lt;br /&gt;
    QPushButton *quitButton;&lt;br /&gt;
    QDialogButtonBox *buttonBox;&lt;br /&gt;
    QTcpServer tcpServer;&lt;br /&gt;
    QTcpSocket tcpClient;&lt;br /&gt;
    QTcpSocket *tcpServerConnection;&lt;br /&gt;
    int bytesToWrite;&lt;br /&gt;
    int bytesWritten;&lt;br /&gt;
    int bytesReceived;&lt;br /&gt;
};&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QtGui&amp;gt;&lt;br /&gt;
#include &amp;lt;QtNetwork&amp;gt;&lt;br /&gt;
#include &amp;quot;dialog.h&amp;quot;&lt;br /&gt;
#if !defined(Q_OS_WINCE)&lt;br /&gt;
static const int TotalBytes = 50 * 1024 * 1024;&lt;br /&gt;
#else&lt;br /&gt;
static const int TotalBytes = 5 * 1024 * 1024;&lt;br /&gt;
#endif&lt;br /&gt;
static const int PayloadSize = 65536;&lt;br /&gt;
Dialog::Dialog(QWidget *parent)&lt;br /&gt;
    : QDialog(parent)&lt;br /&gt;
{&lt;br /&gt;
    clientProgressBar = new QProgressBar;&lt;br /&gt;
    clientStatusLabel = new QLabel(tr(&amp;quot;Client ready&amp;quot;));&lt;br /&gt;
    serverProgressBar = new QProgressBar;&lt;br /&gt;
    serverStatusLabel = new QLabel(tr(&amp;quot;Server ready&amp;quot;));&lt;br /&gt;
    startButton = new QPushButton(tr(&amp;quot;&amp;amp;Start&amp;quot;));&lt;br /&gt;
    quitButton = new QPushButton(tr(&amp;quot;&amp;amp;Quit&amp;quot;));&lt;br /&gt;
    buttonBox = new QDialogButtonBox;&lt;br /&gt;
    buttonBox-&amp;gt;addButton(startButton, QDialogButtonBox::ActionRole);&lt;br /&gt;
    buttonBox-&amp;gt;addButton(quitButton, QDialogButtonBox::RejectRole);&lt;br /&gt;
    connect(startButton, SIGNAL(clicked()), this, SLOT(start()));&lt;br /&gt;
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));&lt;br /&gt;
    connect(&amp;amp;tcpServer, SIGNAL(newConnection()),&lt;br /&gt;
            this, SLOT(acceptConnection()));&lt;br /&gt;
    connect(&amp;amp;tcpClient, SIGNAL(connected()), this, SLOT(startTransfer()));&lt;br /&gt;
    connect(&amp;amp;tcpClient, SIGNAL(bytesWritten(qint64)),&lt;br /&gt;
            this, SLOT(updateClientProgress(qint64)));&lt;br /&gt;
    connect(&amp;amp;tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),&lt;br /&gt;
            this, SLOT(displayError(QAbstractSocket::SocketError)));&lt;br /&gt;
    QVBoxLayout *mainLayout = new QVBoxLayout;&lt;br /&gt;
    mainLayout-&amp;gt;addWidget(clientProgressBar);&lt;br /&gt;
    mainLayout-&amp;gt;addWidget(clientStatusLabel);&lt;br /&gt;
    mainLayout-&amp;gt;addWidget(serverProgressBar);&lt;br /&gt;
    mainLayout-&amp;gt;addWidget(serverStatusLabel);&lt;br /&gt;
    mainLayout-&amp;gt;addStretch(1);&lt;br /&gt;
    mainLayout-&amp;gt;addSpacing(10);&lt;br /&gt;
    mainLayout-&amp;gt;addWidget(buttonBox);&lt;br /&gt;
    setLayout(mainLayout);&lt;br /&gt;
    setWindowTitle(tr(&amp;quot;Loopback&amp;quot;));&lt;br /&gt;
}&lt;br /&gt;
void Dialog::start()&lt;br /&gt;
{&lt;br /&gt;
    startButton-&amp;gt;setEnabled(false);&lt;br /&gt;
#ifndef QT_NO_CURSOR&lt;br /&gt;
    QApplication::setOverrideCursor(Qt::WaitCursor);&lt;br /&gt;
#endif&lt;br /&gt;
    bytesWritten = 0;&lt;br /&gt;
    bytesReceived = 0;&lt;br /&gt;
    while (!tcpServer.isListening() &amp;amp;&amp;amp; !tcpServer.listen()) {&lt;br /&gt;
        QMessageBox::StandardButton ret = QMessageBox::critical(this,&lt;br /&gt;
                                        tr(&amp;quot;Loopback&amp;quot;),&lt;br /&gt;
                                        tr(&amp;quot;Unable to start the test: %1.&amp;quot;)&lt;br /&gt;
          .arg(tcpServer.errorString()),&lt;br /&gt;
                                        QMessageBox::Retry&lt;br /&gt;
          | QMessageBox::Cancel);&lt;br /&gt;
        if (ret == QMessageBox::Cancel)&lt;br /&gt;
            return;&lt;br /&gt;
    }&lt;br /&gt;
    serverStatusLabel-&amp;gt;setText(tr(&amp;quot;Listening&amp;quot;));&lt;br /&gt;
    clientStatusLabel-&amp;gt;setText(tr(&amp;quot;Connecting&amp;quot;));&lt;br /&gt;
    tcpClient.connectToHost(QHostAddress::LocalHost, tcpServer.serverPort());&lt;br /&gt;
}&lt;br /&gt;
void Dialog::acceptConnection()&lt;br /&gt;
{&lt;br /&gt;
    tcpServerConnection = tcpServer.nextPendingConnection();&lt;br /&gt;
    connect(tcpServerConnection, SIGNAL(readyRead()),&lt;br /&gt;
            this, SLOT(updateServerProgress()));&lt;br /&gt;
    connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),&lt;br /&gt;
            this, SLOT(displayError(QAbstractSocket::SocketError)));&lt;br /&gt;
    serverStatusLabel-&amp;gt;setText(tr(&amp;quot;Accepted connection&amp;quot;));&lt;br /&gt;
    tcpServer.close();&lt;br /&gt;
}&lt;br /&gt;
void Dialog::startTransfer()&lt;br /&gt;
{&lt;br /&gt;
    bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, &amp;quot;@&amp;quot;));&lt;br /&gt;
    clientStatusLabel-&amp;gt;setText(tr(&amp;quot;Connected&amp;quot;));&lt;br /&gt;
}&lt;br /&gt;
void Dialog::updateServerProgress()&lt;br /&gt;
{&lt;br /&gt;
    bytesReceived += (int)tcpServerConnection-&amp;gt;bytesAvailable();&lt;br /&gt;
    tcpServerConnection-&amp;gt;readAll();&lt;br /&gt;
    serverProgressBar-&amp;gt;setMaximum(TotalBytes);&lt;br /&gt;
    serverProgressBar-&amp;gt;setValue(bytesReceived);&lt;br /&gt;
    serverStatusLabel-&amp;gt;setText(tr(&amp;quot;Received %1MB&amp;quot;)&lt;br /&gt;
                               .arg(bytesReceived / (1024 * 1024)));&lt;br /&gt;
    if (bytesReceived == TotalBytes) {&lt;br /&gt;
        tcpServerConnection-&amp;gt;close();&lt;br /&gt;
        startButton-&amp;gt;setEnabled(true);&lt;br /&gt;
#ifndef QT_NO_CURSOR&lt;br /&gt;
        QApplication::restoreOverrideCursor();&lt;br /&gt;
#endif&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
void Dialog::updateClientProgress(qint64 numBytes)&lt;br /&gt;
{&lt;br /&gt;
    bytesWritten += (int)numBytes;&lt;br /&gt;
    if (bytesToWrite &amp;gt; 0)&lt;br /&gt;
        bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), &amp;quot;@&amp;quot;));&lt;br /&gt;
    clientProgressBar-&amp;gt;setMaximum(TotalBytes);&lt;br /&gt;
    clientProgressBar-&amp;gt;setValue(bytesWritten);&lt;br /&gt;
    clientStatusLabel-&amp;gt;setText(tr(&amp;quot;Sent %1MB&amp;quot;)&lt;br /&gt;
                               .arg(bytesWritten / (1024 * 1024)));&lt;br /&gt;
}&lt;br /&gt;
void Dialog::displayError(QAbstractSocket::SocketError socketError)&lt;br /&gt;
{&lt;br /&gt;
    if (socketError == QTcpSocket::RemoteHostClosedError)&lt;br /&gt;
        return;&lt;br /&gt;
    QMessageBox::information(this, tr(&amp;quot;Network error&amp;quot;),&lt;br /&gt;
                             tr(&amp;quot;The following error occurred: %1.&amp;quot;)&lt;br /&gt;
                             .arg(tcpClient.errorString()));&lt;br /&gt;
    tcpClient.close();&lt;br /&gt;
    tcpServer.close();&lt;br /&gt;
    clientProgressBar-&amp;gt;reset();&lt;br /&gt;
    serverProgressBar-&amp;gt;reset();&lt;br /&gt;
    clientStatusLabel-&amp;gt;setText(tr(&amp;quot;Client ready&amp;quot;));&lt;br /&gt;
    serverStatusLabel-&amp;gt;setText(tr(&amp;quot;Server ready&amp;quot;));&lt;br /&gt;
    startButton-&amp;gt;setEnabled(true);&lt;br /&gt;
#ifndef QT_NO_CURSOR&lt;br /&gt;
    QApplication::restoreOverrideCursor();&lt;br /&gt;
#endif&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;quot;dialog.h&amp;quot;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    QApplication app(argc, argv);&lt;br /&gt;
    Dialog dialog;&lt;br /&gt;
    dialog.show();&lt;br /&gt;
    return dialog.exec();&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==QTcpServer based server==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Foundations of Qt Development\Chapter14\tcpserver\main.cpp&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;QCoreApplication&amp;gt;&lt;br /&gt;
#include &amp;quot;server.h&amp;quot;&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
  QCoreApplication app( argc, argv );&lt;br /&gt;
  &lt;br /&gt;
  Server server;&lt;br /&gt;
  if( !server.listen( QHostAddress::Any, 9876 ) )&lt;br /&gt;
  {&lt;br /&gt;
    qCritical( &amp;quot;Cannot listen to port 9876.&amp;quot; );&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Foundations of Qt Development\Chapter14\tcpserver\server.cpp&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;QTcpServer&amp;gt;&lt;br /&gt;
#include &amp;lt;QTcpSocket&amp;gt;&lt;br /&gt;
#include &amp;quot;server.h&amp;quot;&lt;br /&gt;
#include &amp;quot;serverthread.h&amp;quot;&lt;br /&gt;
Server::Server() : QTcpServer()&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
void Server::incomingConnection( int descriptor )&lt;br /&gt;
{&lt;br /&gt;
  ServerThread *thread = new ServerThread( descriptor, this );&lt;br /&gt;
  &lt;br /&gt;
  connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );&lt;br /&gt;
  thread-&amp;gt;start();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Foundations of Qt Development\Chapter14\tcpserver\server.h&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#ifndef SERVER_H&lt;br /&gt;
#define SERVER_H&lt;br /&gt;
#include &amp;lt;QTcpServer&amp;gt;&lt;br /&gt;
class Server : public QTcpServer&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
  Server();&lt;br /&gt;
  &lt;br /&gt;
protected:&lt;br /&gt;
  void incomingConnection( int descriptor );&lt;br /&gt;
};&lt;br /&gt;
#endif // SERVER_H&lt;br /&gt;
&lt;br /&gt;
Foundations of Qt Development\Chapter14\tcpserver\serverthread.cpp&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;QBuffer&amp;gt;&lt;br /&gt;
#include &amp;lt;QImage&amp;gt;&lt;br /&gt;
#include &amp;lt;QImageWriter&amp;gt;&lt;br /&gt;
#include &amp;lt;QTcpSocket&amp;gt;&lt;br /&gt;
#include &amp;lt;QDir&amp;gt;&lt;br /&gt;
#include &amp;lt;QTime&amp;gt;&lt;br /&gt;
#include &amp;quot;serverthread.h&amp;quot;&lt;br /&gt;
ServerThread::ServerThread( int descriptor, QObject *parent ) : QThread( parent )&lt;br /&gt;
{&lt;br /&gt;
  m_descriptor = descriptor;&lt;br /&gt;
}&lt;br /&gt;
void ServerThread::run()&lt;br /&gt;
{&lt;br /&gt;
  QTcpSocket socket;&lt;br /&gt;
  &lt;br /&gt;
  if( !socket.setSocketDescriptor( m_descriptor ) )&lt;br /&gt;
  {&lt;br /&gt;
    qDebug( &amp;quot;Socket error!&amp;quot; );&lt;br /&gt;
    return;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  QBuffer buffer;&lt;br /&gt;
  QImageWriter writer(&amp;amp;buffer, &amp;quot;PNG&amp;quot;);&lt;br /&gt;
  writer.write( randomImage() );&lt;br /&gt;
  &lt;br /&gt;
  QByteArray data;&lt;br /&gt;
  QDataStream stream( &amp;amp;data, QIODevice::WriteOnly );&lt;br /&gt;
  stream.setVersion( QDataStream::Qt_4_0 );&lt;br /&gt;
  stream &amp;lt;&amp;lt; (quint32)buffer.data().size();&lt;br /&gt;
  data.append( buffer.data() );&lt;br /&gt;
  &lt;br /&gt;
  socket.write( data );&lt;br /&gt;
  &lt;br /&gt;
  socket.disconnectFromHost();&lt;br /&gt;
  socket.waitForDisconnected();&lt;br /&gt;
}&lt;br /&gt;
QImage ServerThread::randomImage()&lt;br /&gt;
{&lt;br /&gt;
  qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));&lt;br /&gt;
  &lt;br /&gt;
  QDir dir(&amp;quot;./images&amp;quot;);&lt;br /&gt;
  dir.setFilter( QDir::Files );&lt;br /&gt;
  QFileInfoList entries = dir.entryInfoList();&lt;br /&gt;
  &lt;br /&gt;
  if( entries.size() == 0 )&lt;br /&gt;
  {&lt;br /&gt;
    qDebug( &amp;quot;No images to show!&amp;quot; );&lt;br /&gt;
    return QImage();&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  return QImage( entries.at( qrand() % entries.size() ).absoluteFilePath() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Foundations of Qt Development\Chapter14\tcpserver\serverthread.h&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#ifndef SERVERTHREAD_H&lt;br /&gt;
#define SERVERTHREAD_H&lt;br /&gt;
#include &amp;lt;QThread&amp;gt;&lt;br /&gt;
#include &amp;lt;QImage&amp;gt;&lt;br /&gt;
class ServerThread : public QThread&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
  ServerThread( int descriptor, QObject *parent );&lt;br /&gt;
  &lt;br /&gt;
  void run();&lt;br /&gt;
private:&lt;br /&gt;
  QImage randomImage();&lt;br /&gt;
  &lt;br /&gt;
  int m_descriptor;&lt;br /&gt;
};&lt;br /&gt;
#endif // SERVERTHREAD_H&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==QTcpServer based server 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
/****************************************************************************&lt;br /&gt;
**&lt;br /&gt;
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).&lt;br /&gt;
** All rights reserved.&lt;br /&gt;
** Contact: Nokia Corporation (qt-info@nokia.com)&lt;br /&gt;
**&lt;br /&gt;
** This file is part of the examples of the Qt Toolkit.&lt;br /&gt;
**&lt;br /&gt;
** $QT_BEGIN_LICENSE:LGPL$&lt;br /&gt;
** Commercial Usage&lt;br /&gt;
** Licensees holding valid Qt Commercial licenses may use this file in&lt;br /&gt;
** accordance with the Qt Commercial License Agreement provided with the&lt;br /&gt;
** Software or, alternatively, in accordance with the terms contained in&lt;br /&gt;
** a written agreement between you and Nokia.&lt;br /&gt;
**&lt;br /&gt;
** GNU Lesser General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU Lesser&lt;br /&gt;
** General Public License version 2.1 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.LGPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU Lesser General Public License version 2.1 requirements&lt;br /&gt;
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.&lt;br /&gt;
**&lt;br /&gt;
** In addition, as a special exception, Nokia gives you certain additional&lt;br /&gt;
** rights.  These rights are described in the Nokia Qt LGPL Exception&lt;br /&gt;
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.&lt;br /&gt;
**&lt;br /&gt;
** GNU General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU&lt;br /&gt;
** General Public License version 3.0 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.GPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU General Public License version 3.0 requirements will be&lt;br /&gt;
** met: http://www.gnu.org/copyleft/gpl.html.&lt;br /&gt;
**&lt;br /&gt;
** If you have questions regarding the use of this file, please contact&lt;br /&gt;
** Nokia at qt-info@nokia.com.&lt;br /&gt;
** $QT_END_LICENSE$&lt;br /&gt;
**&lt;br /&gt;
****************************************************************************/&lt;br /&gt;
#ifndef SERVER_H&lt;br /&gt;
#define SERVER_H&lt;br /&gt;
#include &amp;lt;QDialog&amp;gt;&lt;br /&gt;
QT_BEGIN_NAMESPACE&lt;br /&gt;
class QLabel;&lt;br /&gt;
class QPushButton;&lt;br /&gt;
class QTcpServer;&lt;br /&gt;
QT_END_NAMESPACE&lt;br /&gt;
&lt;br /&gt;
class Server : public QDialog&lt;br /&gt;
{&lt;br /&gt;
    Q_OBJECT&lt;br /&gt;
public:&lt;br /&gt;
    Server(QWidget *parent = 0);&lt;br /&gt;
private slots:&lt;br /&gt;
    void sendFortune();&lt;br /&gt;
private:&lt;br /&gt;
    QLabel *statusLabel;&lt;br /&gt;
    QPushButton *quitButton;&lt;br /&gt;
    QTcpServer *tcpServer;&lt;br /&gt;
    QStringList fortunes;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QtGui&amp;gt;&lt;br /&gt;
#include &amp;lt;QtNetwork&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;quot;server.h&amp;quot;&lt;br /&gt;
Server::Server(QWidget *parent)&lt;br /&gt;
    : QDialog(parent)&lt;br /&gt;
{&lt;br /&gt;
    statusLabel = new QLabel;&lt;br /&gt;
    quitButton = new QPushButton(tr(&amp;quot;Quit&amp;quot;));&lt;br /&gt;
    quitButton-&amp;gt;setAutoDefault(false);&lt;br /&gt;
 &lt;br /&gt;
    tcpServer = new QTcpServer(this);&lt;br /&gt;
    if (!tcpServer-&amp;gt;listen()) {&lt;br /&gt;
        QMessageBox::critical(this, tr(&amp;quot;Fortune Server&amp;quot;),&lt;br /&gt;
                              tr(&amp;quot;Unable to start the server: %1.&amp;quot;)&lt;br /&gt;
                              .arg(tcpServer-&amp;gt;errorString()));&lt;br /&gt;
        close();&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    statusLabel-&amp;gt;setText(tr(&amp;quot;The server is running on port %1.\n&amp;quot;&lt;br /&gt;
                            &amp;quot;Run the Fortune Client example now.&amp;quot;)&lt;br /&gt;
                         .arg(tcpServer-&amp;gt;serverPort()));&lt;br /&gt;
&lt;br /&gt;
    fortunes &amp;lt;&amp;lt; tr(&amp;quot;You&amp;quot;ve been leading a dog&amp;quot;s life. Stay off the furniture.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You&amp;quot;ve got to think about tomorrow.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You will be surprised by a loud noise.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You will feel hungry again in another hour.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You might have mail.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You cannot kill time without injuring eternity.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;Computers are not intelligent. They only think they are.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));&lt;br /&gt;
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));&lt;br /&gt;
&lt;br /&gt;
    QHBoxLayout *buttonLayout = new QHBoxLayout;&lt;br /&gt;
    buttonLayout-&amp;gt;addStretch(1);&lt;br /&gt;
    buttonLayout-&amp;gt;addWidget(quitButton);&lt;br /&gt;
    buttonLayout-&amp;gt;addStretch(1);&lt;br /&gt;
    QVBoxLayout *mainLayout = new QVBoxLayout;&lt;br /&gt;
    mainLayout-&amp;gt;addWidget(statusLabel);&lt;br /&gt;
    mainLayout-&amp;gt;addLayout(buttonLayout);&lt;br /&gt;
    setLayout(mainLayout);&lt;br /&gt;
    setWindowTitle(tr(&amp;quot;Fortune Server&amp;quot;));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void Server::sendFortune()&lt;br /&gt;
{&lt;br /&gt;
    QByteArray block;&lt;br /&gt;
    QDataStream out(&amp;amp;block, QIODevice::WriteOnly);&lt;br /&gt;
    out.setVersion(QDataStream::Qt_4_0);&lt;br /&gt;
 &lt;br /&gt;
    out &amp;lt;&amp;lt; (quint16)0;&lt;br /&gt;
    out &amp;lt;&amp;lt; fortunes.at(qrand() % fortunes.size());&lt;br /&gt;
    out.device()-&amp;gt;seek(0);&lt;br /&gt;
    out &amp;lt;&amp;lt; (quint16)(block.size() - sizeof(quint16));&lt;br /&gt;
 &lt;br /&gt;
    QTcpSocket *clientConnection = tcpServer-&amp;gt;nextPendingConnection();&lt;br /&gt;
    connect(clientConnection, SIGNAL(disconnected()),&lt;br /&gt;
            clientConnection, SLOT(deleteLater()));&lt;br /&gt;
 &lt;br /&gt;
    clientConnection-&amp;gt;write(block);&lt;br /&gt;
    clientConnection-&amp;gt;disconnectFromHost();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;QtCore&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;quot;server.h&amp;quot;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    QApplication app(argc, argv);&lt;br /&gt;
    Server server;&lt;br /&gt;
    server.show();&lt;br /&gt;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));&lt;br /&gt;
    return server.exec();&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Thread based server==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
/****************************************************************************&lt;br /&gt;
**&lt;br /&gt;
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).&lt;br /&gt;
** All rights reserved.&lt;br /&gt;
** Contact: Nokia Corporation (qt-info@nokia.com)&lt;br /&gt;
**&lt;br /&gt;
** This file is part of the examples of the Qt Toolkit.&lt;br /&gt;
**&lt;br /&gt;
** $QT_BEGIN_LICENSE:LGPL$&lt;br /&gt;
** Commercial Usage&lt;br /&gt;
** Licensees holding valid Qt Commercial licenses may use this file in&lt;br /&gt;
** accordance with the Qt Commercial License Agreement provided with the&lt;br /&gt;
** Software or, alternatively, in accordance with the terms contained in&lt;br /&gt;
** a written agreement between you and Nokia.&lt;br /&gt;
**&lt;br /&gt;
** GNU Lesser General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU Lesser&lt;br /&gt;
** General Public License version 2.1 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.LGPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU Lesser General Public License version 2.1 requirements&lt;br /&gt;
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.&lt;br /&gt;
**&lt;br /&gt;
** In addition, as a special exception, Nokia gives you certain additional&lt;br /&gt;
** rights.  These rights are described in the Nokia Qt LGPL Exception&lt;br /&gt;
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.&lt;br /&gt;
**&lt;br /&gt;
** GNU General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU&lt;br /&gt;
** General Public License version 3.0 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.GPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU General Public License version 3.0 requirements will be&lt;br /&gt;
** met: http://www.gnu.org/copyleft/gpl.html.&lt;br /&gt;
**&lt;br /&gt;
** If you have questions regarding the use of this file, please contact&lt;br /&gt;
** Nokia at qt-info@nokia.com.&lt;br /&gt;
** $QT_END_LICENSE$&lt;br /&gt;
**&lt;br /&gt;
****************************************************************************/&lt;br /&gt;
#ifndef FORTUNESERVER_H&lt;br /&gt;
#define FORTUNESERVER_H&lt;br /&gt;
#include &amp;lt;QStringList&amp;gt;&lt;br /&gt;
#include &amp;lt;QTcpServer&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FortuneServer : public QTcpServer&lt;br /&gt;
{&lt;br /&gt;
    Q_OBJECT&lt;br /&gt;
public:&lt;br /&gt;
    FortuneServer(QObject *parent = 0);&lt;br /&gt;
protected:&lt;br /&gt;
    void incomingConnection(int socketDescriptor);&lt;br /&gt;
private:&lt;br /&gt;
    QStringList fortunes;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;fortuneserver.h&amp;quot;&lt;br /&gt;
#include &amp;quot;fortunethread.h&amp;quot;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FortuneServer::FortuneServer(QObject *parent)&lt;br /&gt;
    : QTcpServer(parent)&lt;br /&gt;
{&lt;br /&gt;
    fortunes &amp;lt;&amp;lt; tr(&amp;quot;You&amp;quot;ve been leading a dog&amp;quot;s life. Stay off the furniture.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You&amp;quot;ve got to think about tomorrow.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You will be surprised by a loud noise.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You will feel hungry again in another hour.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You might have mail.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;You cannot kill time without injuring eternity.&amp;quot;)&lt;br /&gt;
             &amp;lt;&amp;lt; tr(&amp;quot;Computers are not intelligent. They only think they are.&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void FortuneServer::incomingConnection(int socketDescriptor)&lt;br /&gt;
{&lt;br /&gt;
    QString fortune = fortunes.at(qrand() % fortunes.size());&lt;br /&gt;
    FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);&lt;br /&gt;
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));&lt;br /&gt;
    thread-&amp;gt;start();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#ifndef DIALOG_H&lt;br /&gt;
#define DIALOG_H&lt;br /&gt;
#include &amp;lt;QDialog&amp;gt;&lt;br /&gt;
#include &amp;quot;fortuneserver.h&amp;quot;&lt;br /&gt;
QT_BEGIN_NAMESPACE&lt;br /&gt;
class QLabel;&lt;br /&gt;
class QPushButton;&lt;br /&gt;
QT_END_NAMESPACE&lt;br /&gt;
class Dialog : public QDialog&lt;br /&gt;
{&lt;br /&gt;
    Q_OBJECT&lt;br /&gt;
public:&lt;br /&gt;
    Dialog(QWidget *parent = 0);&lt;br /&gt;
private:&lt;br /&gt;
    QLabel *statusLabel;&lt;br /&gt;
    QPushButton *quitButton;&lt;br /&gt;
    FortuneServer server;&lt;br /&gt;
};&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QtGui&amp;gt;&lt;br /&gt;
#include &amp;lt;QtNetwork&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;quot;dialog.h&amp;quot;&lt;br /&gt;
#include &amp;quot;fortuneserver.h&amp;quot;&lt;br /&gt;
Dialog::Dialog(QWidget *parent)&lt;br /&gt;
    : QDialog(parent)&lt;br /&gt;
{&lt;br /&gt;
    statusLabel = new QLabel;&lt;br /&gt;
    quitButton = new QPushButton(tr(&amp;quot;Quit&amp;quot;));&lt;br /&gt;
    quitButton-&amp;gt;setAutoDefault(false);&lt;br /&gt;
    if (!server.listen()) {&lt;br /&gt;
        QMessageBox::critical(this, tr(&amp;quot;Threaded Fortune Server&amp;quot;),&lt;br /&gt;
                              tr(&amp;quot;Unable to start the server: %1.&amp;quot;)&lt;br /&gt;
                              .arg(server.errorString()));&lt;br /&gt;
        close();&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    statusLabel-&amp;gt;setText(tr(&amp;quot;The server is running on port %1.\n&amp;quot;&lt;br /&gt;
                            &amp;quot;Run the Fortune Client example now.&amp;quot;)&lt;br /&gt;
                         .arg(server.serverPort()));&lt;br /&gt;
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));&lt;br /&gt;
    QHBoxLayout *buttonLayout = new QHBoxLayout;&lt;br /&gt;
    buttonLayout-&amp;gt;addStretch(1);&lt;br /&gt;
    buttonLayout-&amp;gt;addWidget(quitButton);&lt;br /&gt;
    buttonLayout-&amp;gt;addStretch(1);&lt;br /&gt;
    QVBoxLayout *mainLayout = new QVBoxLayout;&lt;br /&gt;
    mainLayout-&amp;gt;addWidget(statusLabel);&lt;br /&gt;
    mainLayout-&amp;gt;addLayout(buttonLayout);&lt;br /&gt;
    setLayout(mainLayout);&lt;br /&gt;
    setWindowTitle(tr(&amp;quot;Threaded Fortune Server&amp;quot;));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#ifndef FORTUNETHREAD_H&lt;br /&gt;
#define FORTUNETHREAD_H&lt;br /&gt;
#include &amp;lt;QThread&amp;gt;&lt;br /&gt;
#include &amp;lt;QTcpSocket&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FortuneThread : public QThread&lt;br /&gt;
{&lt;br /&gt;
    Q_OBJECT&lt;br /&gt;
public:&lt;br /&gt;
    FortuneThread(int socketDescriptor, const QString &amp;amp;fortune, QObject *parent);&lt;br /&gt;
    void run();&lt;br /&gt;
signals:&lt;br /&gt;
    void error(QTcpSocket::SocketError socketError);&lt;br /&gt;
private:&lt;br /&gt;
    int socketDescriptor;&lt;br /&gt;
    QString text;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;fortunethread.h&amp;quot;&lt;br /&gt;
#include &amp;lt;QtNetwork&amp;gt;&lt;br /&gt;
&lt;br /&gt;
FortuneThread::FortuneThread(int socketDescriptor, const QString &amp;amp;fortune, QObject *parent)&lt;br /&gt;
    : QThread(parent), socketDescriptor(socketDescriptor), text(fortune)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void FortuneThread::run()&lt;br /&gt;
{&lt;br /&gt;
    QTcpSocket tcpSocket;&lt;br /&gt;
 &lt;br /&gt;
    if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {&lt;br /&gt;
        emit error(tcpSocket.error());&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    QByteArray block;&lt;br /&gt;
    QDataStream out(&amp;amp;block, QIODevice::WriteOnly);&lt;br /&gt;
    out.setVersion(QDataStream::Qt_4_0);&lt;br /&gt;
    out &amp;lt;&amp;lt; (quint16)0;&lt;br /&gt;
    out &amp;lt;&amp;lt; text;&lt;br /&gt;
    out.device()-&amp;gt;seek(0);&lt;br /&gt;
    out &amp;lt;&amp;lt; (quint16)(block.size() - sizeof(quint16));&lt;br /&gt;
 &lt;br /&gt;
    tcpSocket.write(block);&lt;br /&gt;
    tcpSocket.disconnectFromHost();&lt;br /&gt;
    tcpSocket.waitForDisconnected();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;QtCore&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;quot;dialog.h&amp;quot;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    QApplication app(argc, argv);&lt;br /&gt;
    Dialog dialog;&lt;br /&gt;
    dialog.show();&lt;br /&gt;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));&lt;br /&gt;
    return dialog.exec();&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>