1

What is the correct use/implementation of the QAbstractSocket stateChanged()-Signal?

main.cpp:

#include <QCoreApplication>

#include <sslserver.h>
#include <QLoggingCategory>

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

QCoreApplication a(argc, argv);
QLoggingCategory::setFilterRules("*.debug=true");

SslServer *myTestServer = new SslServer();

return a.exec();
}

SslServer.h:

#ifndef SSLSERVER_H
#define SSLSERVER_H

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>

class SslServer : public QObject
{
    Q_OBJECT
public:
    explicit SslServer(QObject *parent = nullptr);

private slots:
    void newTestConnection();
    void sendTestdata();
    void connectionWasClosed(QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState);

private:
    QTcpServer *testServer;
    QTcpSocket *testSocket;
    QTimer *testTimer;
};

#endif // SSLSERVER_H

SslServer.cpp:

#include "sslserver.h"
#include <QDebug>

SslServer::SslServer(QObject *parent) : QObject(parent){

    testServer = new QTcpServer(this);
    testSocket = new QTcpSocket();
    testTimer = new QTimer();

    connect(testServer, SIGNAL(newConnection()), this, SLOT(newTestConnection()));  //works
    connect(testTimer, SIGNAL(timeout()), this, SLOT(sendTestdata()));              //works
    connect(testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(connectionWasClosed(QAbstractSocket::SocketState)));
    //Qt5 Connection was also tested, without any change to old connection-typo
    //connect(testSocket, &QAbstractSocket::stateChanged, this, &SslServer::verbindungWurdeBeendet);

    testTimer->start(1000);

    if(testServer->listen(QHostAddress::Any, 9999)){
        qDebug() << "Server running on Port 9999";
    }else{
        qDebug() << "Error while building server";
    }

}

void SslServer::newTestConnection(){
    testSocket->close();
    testSocket = testServer->nextPendingConnection();
    testSocket->write("Welcome on: Testserver!");
    testSocket->flush();

}

void SslServer::sendTestdata(){
    if(testSocket->isOpen()){
        qDebug() << testSocket->state();
        qDebug() << "Sending testdata to client";
        testSocket->write("testdata");
    }
}

void SslServer::connectionWasClosed(QAbstractSocket::SocketState){

    qDebug() << "!!! SocketState changed !!!";

}

I can see from the testSocket->state() statement that the state is changing from Unconnected to Connected to Unconnected, but the SLOT-Function is never called. What am I missing here?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

Your SLOT signature is not matching with SIGNAL.

Change the function declaration and definition to

void verbindungWurdeBeendet(QAbstractSocket::SocketState state);

And the connect statement to

connect(testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(verbindungWurdeBeendet(QAbstractSocket::SocketState)));

The SLOT function verbindungWurdeBeendet() (with out argument) is also acceptable, if you have default value in the function declaration.

ex:

In the below case you can have a SLOT in connect statement, without arguments.

 void verbindungWurdeBeendet(QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState);
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
0

The problem is caused because you are connecting to an object:

testSocket = new QTcpSocket();

But you expect to receive the signal from another object:

testSocket = testServer->nextPendingConnection();

The solution is to make the connection when you receive the new object:

void SslServer::newTestConnection(){
    if(testSocket)
        testSocket->close();
    testSocket = testServer->nextPendingConnection();
    connect(testSocket, &QAbstractSocket::stateChanged, this, &SslServer::connectionWasClosed);
    testSocket->write("Welcome on: Testserver!");
    testSocket->flush();
}

The complete example can be found in the following link

eyllanesc
  • 235,170
  • 19
  • 170
  • 241