19 m_ptcpServer =
new QTcpServer(
this);
20 if (!m_ptcpServer->listen(QHostAddress::Any, nPort)) {
21 QMessageBox::critical(0,
23 "Unable to start the server:" 24 + m_ptcpServer->errorString()
26 m_ptcpServer->close();
29 connect(m_ptcpServer, SIGNAL(newConnection()),
33 m_ptxt =
new QTextEdit;
34 m_ptxt->setReadOnly(
true);
37 QVBoxLayout* pvbxLayout =
new QVBoxLayout;
38 pvbxLayout->addWidget(
new QLabel(
"<H1>Server</H1>"));
39 pvbxLayout->addWidget(m_ptxt);
40 setLayout(pvbxLayout);
46 QTcpSocket* pClientSocket = m_ptcpServer->nextPendingConnection();
47 connect(pClientSocket, SIGNAL(disconnected()),
48 pClientSocket, SLOT(deleteLater())
50 connect(pClientSocket, SIGNAL(readyRead()),
54 sendToClient(pClientSocket,
"Server Response: Connected!");
60 QTcpSocket* pClientSocket = (QTcpSocket*)sender();
61 QDataStream in(pClientSocket);
62 in.setVersion(QDataStream::Qt_5_3);
64 if (!m_nNextBlockSize) {
65 if (pClientSocket->bytesAvailable() < (int)
sizeof(quint16)) {
68 in >> m_nNextBlockSize;
71 if (pClientSocket->bytesAvailable() < m_nNextBlockSize) {
79 time.toString() +
" " +
"Client has sent - " + str;
80 m_ptxt->append(strMessage);
84 sendToClient(pClientSocket,
85 "Server Response: Received \"" + str +
"\"" 91 void MyServer::sendToClient(QTcpSocket* pSocket,
const QString& str)
94 QDataStream out(&arrBlock, QIODevice::WriteOnly);
95 out.setVersion(QDataStream::Qt_5_3);
96 out << quint16(0) << QTime::currentTime() << str;
98 out.device()->seek(0);
99 out << quint16(arrBlock.size() -
sizeof(quint16));
101 pSocket->write(arrBlock);
MyServer(int nPort, QWidget *pwgt=0)
virtual void slotNewConnection()