1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28// Qt
29#include <QByteArray>
30#include <QCoreApplication>
31#include <QDataStream>
32#include <QTimer>
33
34// Test
35#include "Test.h"
36
37//------------------------------------------------------------------------------
38My4Socket::My4Socket(QObject *parent)
39 : QTcpSocket(parent), safeShutDown(false)
40{
41 connect(sender: this, SIGNAL(readyRead()), receiver: this, SLOT(read()));
42 connect(sender: this, SIGNAL(disconnected()), receiver: this, SLOT(closed()));
43}
44
45//------------------------------------------------------------------------------
46void My4Socket::read(void)
47{
48 QDataStream in(this);
49
50 quint32 num = 0;
51 quint32 reply = 0;
52
53 while (bytesAvailable()) {
54 in >> num;
55 if (num == 42) {
56 safeShutDown = true;
57 qDebug(msg: "SUCCESS");
58 QCoreApplication::instance()->quit();
59 return;
60 }
61 reply = num + 1;
62 if (reply == 42)
63 ++reply;
64 }
65
66 // Reply with a bigger number
67 sendTest(num: reply);
68}
69
70//------------------------------------------------------------------------------
71void My4Socket::closed(void)
72{
73 if (!safeShutDown)
74 qDebug(msg: "FAILED");
75 QCoreApplication::instance()->quit();
76}
77
78//------------------------------------------------------------------------------
79void My4Socket::sendTest(quint32 num)
80{
81 QByteArray block;
82 QDataStream out(&block, QIODevice::WriteOnly);
83 out << num;
84
85 write(data: block, len: block.size());
86}
87
88//------------------------------------------------------------------------------
89My4Server::My4Server(QObject *parent)
90 : QTcpServer(parent)
91{
92 if (listen(address: QHostAddress::Any, port: 7700))
93 qDebug(msg: "qt4server");
94 QTimer::singleShot(msec: 5000, receiver: this, SLOT(stopServer()));
95}
96
97//------------------------------------------------------------------------------
98void My4Server::incomingConnection(qintptr socketId)
99{
100 m_socket = new My4Socket(this);
101 m_socket->setSocketDescriptor(socketDescriptor: socketId);
102}
103
104//------------------------------------------------------------------------------
105void My4Server::stopServer()
106{
107 if (m_socket) {
108 qDebug(msg: "SUCCESS");
109 m_socket->safeShutDown = true;
110 m_socket->sendTest(num: 42);
111 } else {
112 QCoreApplication::instance()->quit();
113 }
114}
115
116//------------------------------------------------------------------------------
117Test::Test(Type type)
118{
119 switch (type) {
120 case Qt4Client: {
121 qDebug(msg: "qt4client");
122 My4Socket *s = new My4Socket(this);
123 s->connectToHost(hostName: "localhost", port: 7700);
124 s->sendTest(num: 1);
125 break;
126 }
127 case Qt4Server: {
128 new My4Server(this);
129 break;
130 }
131 default:
132 break;
133 }
134}
135

source code of qtbase/tests/auto/network/socket/qtcpsocket/stressTest/Test.cpp