Search code examples
c++qtsocketstcp

QThreadPoolServer is not responding


I wrote a simple threadpool server with qt. When i try to connect to server on win 32/64 all works good. But when I use linux centos 7 server is not responding. I use 127.0.0.1:8080 for server address. Also server uses database mysql. When I try to connect via telnet it connects but nothing happens. I checked for open ports with netstat. Maybe I missed something because of this the server is not working?

Here is my code for server. In fact, there is also an http request handler, but it does not reach it, I tried to output a string in the constructor - it is not called.

QthreadPoolServer.cpp

#include "QThreadPoolServer.h"
#include "QSocketRunnable.h"
#include "ConfigReader.h"
#include <memory>

QThreadPoolServer::QThreadPoolServer()
{
    ConfigReader reader(config_file_path);

    QHostAddress server_IP(reader.getServerAddress());
    int port = reader.getServerPort();

    listen(QHostAddress::localhost, 8080); 
    std:: cout << serverError() << errorString().toStdString();
    m_threadPool = std::make_shared<QThreadPool>(this);
}

void QThreadPoolServer::incomingConnection(int handle)
{
    std::shared_ptr<QSocketRunnable> runnable = std::make_shared<QSocketRunnable>(handle);
    runnable->setAutoDelete(false);
    m_threadPool->start(runnable.get());
}

QThreadPoolServer::~QThreadPoolServer()
{
    m_threadPool->~QThreadPool();
}

QThreadPoolServer.h

#ifndef QTHREADPOOLSERVER_H
#define QTHREADPOOLSERVER_H

#include <QTcpServer>
#include <QThreadPool>
#include <memory>

class QThreadPoolServer : public QTcpServer
{
public:
    explicit QThreadPoolServer();

    void incomingConnection(int handle);

    ~QThreadPoolServer();
private:
    std::shared_ptr<QThreadPool> m_threadPool;
};

#endif // QTHREADPOOLSERVER_H

QSocketRunnable.cpp

#include "QSocketRunnable.h"
#include <QString>
#include <memory>
#include <iostream>


QSocketRunnable::QSocketRunnable(int handle) : m_descriptor(handle) { }

void QSocketRunnable::run()
{
    QTcpSocket* socket = new QTcpSocket();
    socket->setSocketDescriptor(m_descriptor);
    socket->waitForReadyRead();
    QString request_data = QString(socket->readAll());

    HttpRequestHandler handler(request_data);
    handler.makeResponse();

    QString http_response_result = handler.getHttpResponse();
    std::cout << http_response_result.toStdString() << "\n";
    socket->write(http_response_result.toUtf8());
    socket->waitForBytesWritten(90000);

    socket->disconnectFromHost();
    socket->close();
    socket->deleteLater();
}


QSocketRunnable.h

#ifndef QSOCKETRUNNABLE_H
#define QSOCKETRUNNABLE_H

#include <QRunnable>
#include <QTcpSocket>
#include <QtDebug>
#include <QString>

//#include "IDHelper.h"
//#include "JsonFormatter.h"
//#include "HttpRequestHandler.h"

class QSocketRunnable : public QRunnable
{
public:
    QSocketRunnable(int handle);
    void run() override;
private:
    int m_descriptor;
};

#endif // QSOCKETRUNNABLE_H

main.cpp

#include <QCoreApplication>
#include "QThreadPoolServer.h"
#include "signal.h"
#include <sstream>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QThreadPoolServer server;

    return a.exec();
}

Also std:: cout << serverError() << errorString().toStdString(); returns "-1" that means QAbstractSocket::UnknownSocketError -1 An unidentified error occurred.


Solution

    1. As @chehrlic correctly noted: I had an incorrectly overloaded function, so here is the ritht version of QThreadPoolServer.h

    QThreadPoolServer.h

    #ifndef QTHREADPOOLSERVER_H
    #define QTHREADPOOLSERVER_H
    
    #include <QTcpServer>
    #include <QThreadPool>
    #include <memory>
    
    class QThreadPoolServer : public QTcpServer
    {
    public:
        explicit QThreadPoolServer();
    protected:
        void incomingConnection(qintptr handle) override;
    
        ~QThreadPoolServer();
    private:
        std::shared_ptr<QThreadPool> m_threadPool;
    };
    
    #endif // QTHREADPOOLSERVER_H
    
    1. My my implementation did not work correctly with a smart pointer to a runnable object:

    QThreadPoolServer.cpp

    void QThreadPoolServer::incomingConnection(qintptr handle)
    {
        QSocketRunnable* runnable = new QSocketRunnable(handle)
        runnable->setAutoDelete(true);
        m_threadPool->start(runnable);
    }