NewConnection or incomingConnection not called when inheriting from QTcpServer

Viewed 19

I tried to create a customized tcpserver by inheriting from the QTcpServer class, however, when i testing the basic function, i realized that although the connection from client is established.

here is the minimal functional code from my codes (note that the btcpserver is compiled into a dynamic library, namely the dll)

the BTcpServer.h

#ifndef BTCPSERVER_H
#define BTCPSERVER_H

#include <QTcpServer>

typedef enum 
    { 
        E_BR,
        E_RAW
    }BProtocolType;

class DISPATCHER_EXPORT BTcpServer : public QTcpServer 
{
    Q_OBJECT

public: 
    BTcpServer(const QHostAddress & address, quint16 port, BProtocolType protocol = E_RAW,
                    int backlog = 1, QObject * parent = 0, const char * name = 0 );
    virtual ~BTcpServer();
    const QHostAddress address() const;
    const quint16 port() const;

public: 

       void runServer();
       void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;

protected slots: 
       void acceptNewConnection();
       void connectError(QAbstractSocket::SocketError error);

private: // Attribute
    QHostAddress _addr;
    quint16 _port;
    BProtocolType _protocol;
};


#endif // BTCPSERVER_H

the BTcpServer.cpp

#include <QAbstractSocket>
#include <QTcpSocket>
#include <QDebug>

#include "BTcpServer.h"


BTcpServer::BTcpServer(const QHostAddress& address, quint16 port, BProtocolType protocol, int backlog, QObject* parent, const char* name)
    : QTcpServer(parent),
      _addr(address),
      _port(port)
{
    //this->listen(address, port);
    this->setMaxPendingConnections(backlog);
    _clientList = new BTcpClientList;   
    //qDebug() << "Connecting newConnection to slot" << connect(this, SIGNAL(newConnection()), this, SLOT(acceptNewConnection()));
    qDebug() << connect(this, &QTcpServer::newConnection, this, &BTcpServer::acceptNewConnection);
    _protocol = protocol;
    this->runServer();
}

BTcpServer::~BTcpServer()
{
    
}

const QHostAddress BTcpServer::address() const
{
    return _addr;
}

const quint16 BTcpServer::port() const
{
    return _port;
}

void BTcpServer::runServer()
{
    if (this->listen(_addr, _port))
    {
        //qDebug() << "Server successfully startet, listening : " << _addr.toString() << ":" << _port;
        qDebug() << "Server successfully started, listening : " << this->serverAddress().toString() << ":" << this->serverPort();
    }
    else
    {
        qDebug() << "Failed to start server on addr : " << _addr.toString() << ":" << _port;
    }
    connect(this, &BTcpServer::acceptError, this, &BTcpServer::connectError);
}

void BTcpServer::incomingConnection(qintptr socketDescriptor)
{

    qDebug() << "Incoming Connection!";
    _clientList->append(socketDescriptor, _protocol);

    QTcpServer::incomingConnection(socketDescriptor);
}

void BTcpServer::acceptNewConnection()
{
    qDebug() << "New Connections!";
}


void BTcpServer::connectError(QAbstractSocket::SocketError error)
{
    qDebug() << "Error : " << error;

    qDebug() << this->errorString();
}

the main.h

#ifndef __DISPATCHER_TEST_H__
#define __DISPATCHER_TEST_H__

#include <QtCore>
#include "dispatcher/BTcpServer.h"

class BDispatcherTester : public QObject
{
    Q_OBJECT
public :
    BDispatcherTester(QObject* parent = 0);
public slots:
    void runTest();

signals:
    void finished();

private :
    BTcpServer _tcpServer;
};


#endif

and the main.cpp

#include "main.h"
#include <QThread>
#include <QDebug>

BDispatcherTester::BDispatcherTester(QObject* parent)
    : QObject(parent),
     _tcpServer(QHostAddress(QStringLiteral("127.0.0.1")), 5000, BProtocolType::E_RAW )
{
  
}

void BDispatcherTester::runTest()
{
    QByteArray testPayload("Hello Dispatcher!\n");

    for (int i = 0; i < 20; i++)
    {
        QThread::msleep(1000);
    }

    emit finished();
}

int main(int argc, char* argv[])
{
    QCoreApplication * app = new QCoreApplication(argc, argv);

    BDispatcherTester* tester = new BDispatcherTester(app);

    QObject::connect(tester, SIGNAL(finished()), app, SLOT(quit()));

    QTimer::singleShot(0, tester, SLOT(runTest()));

    return app->exec();
}

i tried to connect with a simple echo tcp client with python

import socket

HOST = "127.0.0.1"  # The server's hostname or IP address
PORT = 5000  # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    print("Connected to server")
    s.sendall(b"Hello, world")
    data = s.recv(1024)

print(f"Received {data!r}")

when the C++ program is running, i get

> python .\simpleEchoClient.py
Connected to server

from tcp client, but either the function incomingConnection nor the signal newConnection() got called from server side (used breakpoints and qDebug to test)

On the server side the output looks like:

true
Server successfully started, listening :  "127.0.0.1" : 5000

D:\dispatcher\build\bin\Debug\dispatcherTest.exe (process 24976) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
0 Answers
Related