Qt 5.10 Book Examples
main.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // main.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtCore>
12 #include <QtNetwork>
13 
14 // ----------------------------------------------------------------------
15 int main(int argc, char** argv)
16 {
17  QCoreApplication app(argc, argv);
18  QTcpServer tcpServer;
19  int nPort = 2424;
20 
21  if (!tcpServer.listen(QHostAddress::Any, nPort)) {
22  qDebug() << "Can't listen on port: " << nPort;
23  return 0;
24  }
25 
26  forever {
27  while (tcpServer.waitForNewConnection(60000)) {
28  do {
29  QTcpSocket* pSocket = tcpServer.nextPendingConnection();
30  QString strDateTime =
31  QDateTime::currentDateTime()
32  .toString("yyyy.MM.dd hh:mm:ss");
33  pSocket->write(strDateTime.toLatin1());
34  pSocket->flush();
35  qDebug() << "Server date & time:" + strDateTime;
36  pSocket->disconnectFromHost();
37  if (pSocket->state() == QAbstractSocket::ConnectedState) {
38  pSocket->waitForDisconnected();
39  }
40  delete pSocket;
41  } while (tcpServer.hasPendingConnections());
42  }
43  }
44 
45  return 0;
46 }
47 
int main(int argc, char **argv)
Definition: main.cpp:15