Help needed in making a tcp server confused in connectrun and Qrunnable part.
-
Hello everyone,
I have been trying to implement a TCP server with QTcpServer and QthreadPool. My connection to this server always fails and i think i know where the problem is but cant seem to figure it out.
Attached below are all the header files and c++ classes and the problem i think is in line 4 of myconnectrun.cpp where i am using a wrong object or something i think.header - mytcpserver.h
#ifndef MYTCPSERVER_H #define MYTCPSERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QThreadPool> #include <iostream> class MyTcpServer : public QTcpServer { public: explicit MyTcpServer(QObject *parent = nullptr); void startServer(); signals: protected: void incomingConnection(qintptr handle) override; private: QThreadPool *pool; }; #endif // MYTCPSERVER_H
Header - myconnectrun.h
#ifndef MYCONNECTRUN_H #define MYCONNECTRUN_H #include "mytcpserver.h" #include <QRunnable> class myConnectRun : public QRunnable { public: myConnectRun(); qintptr socketdescriptor; protected: void run() override; }; #endif // MYCONNECTRUN_H
c++ class - main.cpp
#include <QCoreApplication> #include <QLocale> #include <QTranslator> [link text](link url) int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTranslator translator; const QStringList uiLanguages = QLocale::system().uiLanguages(); for (const QString &locale : uiLanguages) { const QString baseName = "GUI_intfproject_server_" + QLocale(locale).name(); if (translator.load(":/i18n/" + baseName)) { a.installTranslator(&translator); break; } } return a.exec(); }
myconnectrun.cpp c++ class
#include "mytcpserver.h" #include "myconnectrun.h" myConnectRun::myConnectRun() : QRunnable(){ } void myConnectRun::run() { QByteArray inBuff; if(!socketdescriptor) return; QTcpSocket mySocket; mySocket.setSocketDescriptor(socketdescriptor); if(mySocket.isOpen()){ mySocket.write("Hello Client I am Server"); mySocket.waitForBytesWritten(); mySocket.flush(); while(mySocket.waitForReadyRead()){ inBuff = mySocket.readAll(); qInfo() << socketdescriptor << ":" << inBuff; if(!strncmp(inBuff, "quit", 4)){ break; } mySocket.write("Server Reply: "); mySocket.write(inBuff); mySocket.waitForBytesWritten(); mySocket.flush(); } mySocket.close(); } }
mytcpserver.cpp c++ class
#include "mytcpserver.h" #include "myconnectrun.h" MyTcpServer::MyTcpServer(QObject *parent) : QTcpServer(parent){ pool = new QThreadPool(this); pool->setMaxThreadCount(5); } void MyTcpServer::startServer(){ if (this->listen(QHostAddress::Any, 1234)){ qInfo("Server Started Successfully on localhost port 1234"); } else{ qInfo("Server Starting not Successfully on localhost port 1234"); } } void MyTcpServer::incomingConnection(qintptr handle){ qInfo() << "Incoming Connection Received" << handle; myConnectRun *task = new myConnectRun(); task->socketdescriptor = handle; task->setAutoDelete(true); pool->start(task); }
Please let me know what i might be doing wrong in line 4 of myconnectrun.cpp.
Thank you all soo much.```
code_text -
Hi and welcome to devnet,
From the code you posted, you don't have a server running so that might be the issue.
The other thing is that you don't print what error happened in case the connection failed. That would help your diagnose the issue.