According to QUIP-18 [1], all tests file should be LicenseRef-Qt-Commercial OR GPL-3.0-only [1]: https://contribute.qt-project.org/quips/18 Task-number: QTBUG-121787 Change-Id: I9657df5d660820e56c96d511ea49d321c54682e8 Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de> (cherry picked from commit ff1039c217fb1ae03b701557a5a50c2112555991)
51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
// Copyright (C) 2022 The Qt Company Ltd.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
|
|
|
#include <QtCore>
|
|
#include <QtNetwork>
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
// This example connects to localhost, but note that the host can
|
|
// be any host reachable from the client using webscokets, at any port.
|
|
QString hostName = "localhost";
|
|
int port = 1515;
|
|
qDebug() << "## This example connects to a server at" << hostName << "port" << port << ","
|
|
<< "where it expects to find a WebSockify server, which forwards to the fortune server.";
|
|
|
|
auto echo = [hostName, port]() {
|
|
qDebug() << "Connecting to" << hostName << port;
|
|
|
|
QTcpSocket socket;
|
|
socket.connectToHost(hostName, port);
|
|
bool connected = socket.waitForConnected(3000);
|
|
if (!connected) {
|
|
qDebug() << "connect failure";
|
|
return;
|
|
}
|
|
|
|
qDebug() << "Connected";
|
|
socket.write("echo:Hello, echo server!;");
|
|
socket.flush();
|
|
|
|
qDebug() << "Calling waitForReadyRead()";
|
|
socket.waitForReadyRead(20000);
|
|
QByteArray data = socket.readAll();
|
|
qDebug() << "Got echo:" << data;
|
|
|
|
socket.disconnectFromHost();
|
|
socket.deleteLater();
|
|
qDebug() << "Disconnected";
|
|
};
|
|
|
|
QThread thread;
|
|
QObject::connect(&thread, &QThread::started, [echo](){
|
|
echo();
|
|
});
|
|
thread.start();
|
|
|
|
app.exec();
|
|
}
|