Fortune Server Example▲
This example is intended to be run alongside the Fortune Client example or the Blocking Fortune Client Example.

It uses QTcpServer to accept incoming TCP connections, and a simple QDataStream based data transfer protocol to write a fortune to the connecting client (from the Fortune Client example), before closing the connection.
class
Server : public
QDialog
{
Q_OBJECT
public
:
explicit
Server(QWidget *
parent =
nullptr
);
private
slots:
void
sessionOpened();
void
sendFortune();
private
:
QLabel *
statusLabel =
nullptr
;
QTcpServer *
tcpServer =
nullptr
;
QVector&
lt;QString&
gt; fortunes;
QNetworkSession *
networkSession =
nullptr
;
}
;
The server is implemented using a simple class with only one slot, for handling incoming connections.
tcpServer =
new
QTcpServer(this
);
if
(!
tcpServer-&
gt;listen()) {
QMessageBox::
critical(this
, tr("Fortune Server"
),
tr("Unable to start the server: %1."
)
.arg(tcpServer-&
gt;errorString()));
close();
return
;
}
QString ipAddress;
QList&
lt;QHostAddress&
gt; ipAddressesList =
QNetworkInterface::
allAddresses();
// use the first non-localhost IPv4 address
for
(int
i =
0
; i &
lt; ipAddressesList.size(); ++
i) {
if
(ipAddressesList.at(i) !=
QHostAddress::
LocalHost &
amp;&
amp;
ipAddressesList.at(i).toIPv4Address()) {
ipAddress =
ipAddressesList.at(i).toString();
break
;
}
}
// if we did not find one, use IPv4 localhost
if
(ipAddress.isEmpty())
ipAddress =
QHostAddress(QHostAddress::
LocalHost).toString();
statusLabel-&
gt;setText(tr("The server is running on
\n\n
IP: %1
\n
port: %2
\n\n
"
"Run the Fortune Client example now."
)
.arg(ipAddress).arg(tcpServer-&
gt;serverPort()));
In its constructor, our Server object calls QTcpServer::listen() to set up a QTcpServer to listen on all addresses, on an arbitrary port. In then displays the port QTcpServer picked in a label, so that user knows which port the fortune client should connect to.
fortunes &
lt;&
lt; tr("You've been leading a dog's life. Stay off the furniture."
)
&
lt;&
lt; tr("You've got to think about tomorrow."
)
&
lt;&
lt; tr("You will be surprised by a loud noise."
)
&
lt;&
lt; tr("You will feel hungry again in another hour."
)
&
lt;&
lt; tr("You might have mail."
)
&
lt;&
lt; tr("You cannot kill time without injuring eternity."
)
&
lt;&
lt; tr("Computers are not intelligent. They only think they are."
);
Our server generates a list of random fortunes that it can send to connecting clients.
connect(tcpServer, &
amp;QTcpServer::
newConnection,