Qt 5.10 Book Examples
MyServer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // MyServer.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 <QtNetwork>
12 #include <QtWidgets>
13 #include "MyServer.h"
14 
15 // ----------------------------------------------------------------------
16 MyServer::MyServer(int nPort, QWidget* pwgt /*=0*/) : QWidget(pwgt)
17  , m_nNextBlockSize(0)
18 {
19  m_ptcpServer = new QTcpServer(this);
20  if (!m_ptcpServer->listen(QHostAddress::Any, nPort)) {
21  QMessageBox::critical(0,
22  "Server Error",
23  "Unable to start the server:"
24  + m_ptcpServer->errorString()
25  );
26  m_ptcpServer->close();
27  return;
28  }
29  connect(m_ptcpServer, SIGNAL(newConnection()),
30  this, SLOT(slotNewConnection())
31  );
32 
33  m_ptxt = new QTextEdit;
34  m_ptxt->setReadOnly(true);
35 
36  //Layout setup
37  QVBoxLayout* pvbxLayout = new QVBoxLayout;
38  pvbxLayout->addWidget(new QLabel("<H1>Server</H1>"));
39  pvbxLayout->addWidget(m_ptxt);
40  setLayout(pvbxLayout);
41 }
42 
43 // ----------------------------------------------------------------------
44 /*virtual*/ void MyServer::slotNewConnection()
45 {
46  QTcpSocket* pClientSocket = m_ptcpServer->nextPendingConnection();
47  connect(pClientSocket, SIGNAL(disconnected()),
48  pClientSocket, SLOT(deleteLater())
49  );
50  connect(pClientSocket, SIGNAL(readyRead()),
51  this, SLOT(slotReadClient())
52  );
53 
54  sendToClient(pClientSocket, "Server Response: Connected!");
55 }
56 
57 // ----------------------------------------------------------------------
59 {
60  QTcpSocket* pClientSocket = (QTcpSocket*)sender();
61  QDataStream in(pClientSocket);
62  in.setVersion(QDataStream::Qt_5_3);
63  for (;;) {
64  if (!m_nNextBlockSize) {
65  if (pClientSocket->bytesAvailable() < (int)sizeof(quint16)) {
66  break;
67  }
68  in >> m_nNextBlockSize;
69  }
70 
71  if (pClientSocket->bytesAvailable() < m_nNextBlockSize) {
72  break;
73  }
74  QTime time;
75  QString str;
76  in >> time >> str;
77 
78  QString strMessage =
79  time.toString() + " " + "Client has sent - " + str;
80  m_ptxt->append(strMessage);
81 
82  m_nNextBlockSize = 0;
83 
84  sendToClient(pClientSocket,
85  "Server Response: Received \"" + str + "\""
86  );
87  }
88 }
89 
90 // ----------------------------------------------------------------------
91 void MyServer::sendToClient(QTcpSocket* pSocket, const QString& str)
92 {
93  QByteArray arrBlock;
94  QDataStream out(&arrBlock, QIODevice::WriteOnly);
95  out.setVersion(QDataStream::Qt_5_3);
96  out << quint16(0) << QTime::currentTime() << str;
97 
98  out.device()->seek(0);
99  out << quint16(arrBlock.size() - sizeof(quint16));
100 
101  pSocket->write(arrBlock);
102 }
MyServer(int nPort, QWidget *pwgt=0)
Definition: MyServer.cpp:16
void slotReadClient()
Definition: MyServer.cpp:58
virtual void slotNewConnection()
Definition: MyServer.cpp:44