Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help needed in making a tcp server confused in connectrun and Qrunnable part.
Forum Updated to NodeBB v4.3 + New Features

Help needed in making a tcp server confused in connectrun and Qrunnable part.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 128 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qtcreatorinlearning
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved