123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | // Copyright (C) 2017 The Qt Company Ltd.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause#include <QtWidgets>#include <QtNetwork>#include <QtCore>#include"server.h"Server::Server(QWidget *parent):QDialog(parent),statusLabel(new QLabel){ statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);initServer();//! [2] fortunes <<tr("You've been leading a dog's life. Stay off the furniture.")<<tr("You've got to think about tomorrow.")<<tr("You will be surprised by a loud noise.")<<tr("You will feel hungry again in another hour.")<<tr("You might have mail.")<<tr("You cannot kill time without injuring eternity.")<<tr("Computers are not intelligent. They only think they are.");//! [2]auto quitButton =newQPushButton(tr("Quit")); quitButton->setAutoDefault(false);connect(quitButton, &QAbstractButton::clicked,this, &QWidget::close);//! [3]connect(tcpServer, &QTcpServer::newConnection,this, &Server::sendFortune);//! [3]auto buttonLayout =new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(quitButton); buttonLayout->addStretch(1); QVBoxLayout *mainLayout =nullptr;if(QGuiApplication::styleHints()->showIsFullScreen() ||QGuiApplication::styleHints()->showIsMaximized()) {auto outerVerticalLayout =newQVBoxLayout(this); outerVerticalLayout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding));auto outerHorizontalLayout =new QHBoxLayout; outerHorizontalLayout->addItem(newQSpacerItem(0,0,QSizePolicy::MinimumExpanding,QSizePolicy::Ignored));auto groupBox =newQGroupBox(QGuiApplication::applicationDisplayName()); mainLayout =newQVBoxLayout(groupBox); outerHorizontalLayout->addWidget(groupBox); outerHorizontalLayout->addItem(newQSpacerItem(0,0,QSizePolicy::MinimumExpanding,QSizePolicy::Ignored)); outerVerticalLayout->addLayout(outerHorizontalLayout); outerVerticalLayout->addItem(newQSpacerItem(0,0,QSizePolicy::Ignored,QSizePolicy::MinimumExpanding));}else{ mainLayout =newQVBoxLayout(this);} mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout);setWindowTitle(QGuiApplication::applicationDisplayName());}voidServer::initServer(){//! [0] //! [1] tcpServer =newQTcpServer(this);if(!tcpServer->listen()) {QMessageBox::critical(this,tr("Fortune Server"),tr("Unable to start the server: %1.").arg(tcpServer->errorString()));close();return;}//! [0] QString ipAddress;const QList<QHostAddress> ipAddressesList =QNetworkInterface::allAddresses();// use the first non-localhost IPv4 addressfor(const QHostAddress &entry : ipAddressesList) {if(entry !=QHostAddress::LocalHost && entry.toIPv4Address()) { ipAddress = entry.toString();break;}}// if we did not find one, use IPv4 localhostif(ipAddress.isEmpty()) ipAddress =QHostAddress(QHostAddress::LocalHost).toString(); statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n""Run the Fortune Client example now.").arg(ipAddress).arg(tcpServer->serverPort()));//! [1]}//! [4]voidServer::sendFortune(){//! [5] QByteArray block; QDataStream out(&block,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_6_5); out << fortunes[QRandomGenerator::global()->bounded(fortunes.size())];//! [4] //! [7] QTcpSocket *clientConnection = tcpServer->nextPendingConnection();connect(clientConnection, &QAbstractSocket::disconnected, clientConnection, &QObject::deleteLater);//! [7] //! [8] clientConnection->write(block); clientConnection->disconnectFromHost();//! [5]}//! [8]
|