1
votes

See EDIT1 at the end of the question for a possible solution - It would be great if somebody could comment on my interpretation, so that I can understand better what's happening

I'm writing a simple TCP client, based on QTcpSocket and managed by a QStateMachine (connect to server -> transmit data -> if disconnected for any reason, reconnect to server).

I noticed that if the connection is shut down on the server side (client is notified with RemoteHostClosedError), after reconnection the QTcpSocket write() method succeeds but no data is transmitted on the wire - nothing is received by the server, and the bytesWritten() signal on the client side does not fire up.

I found in the documentation for error() signal (https://doc.qt.io/qt-5/qabstractsocket.html#error) that

When this signal is emitted, the socket may not be ready for a reconnect attempt. In that case, attempts to reconnect should be done from the event loop".

I think I'm already ok with that, as the reconnection happens in one of the QStateMachine states, and QStateMachine should have its own event loop according to the QT docs.

Below some simplified code to reproduce the issue (sorry, not so minimal but I could not find a simpler way to show the problem):

testclient.h

#ifndef TESTCLIENT_H
#define TESTCLIENT_H

#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QStateMachine>

class TestClient : public QObject
{
    Q_OBJECT

public:
    explicit TestClient(QObject *parent = nullptr);

public slots:
    void start();

signals:
    // FSM events
    void fsmEvtConnected();
    void fsmEvtError();

private slots:
    void onSocketConnected();                       // Notify connection to TCP server
    void onSocketDisconnected();                    // Notify disconnection from TCP server
    void onSocketBytesWritten(qint64 bytes);        // Notify number of bytes written to TCP server
    void onSocketError(QAbstractSocket::SocketError err);

    // FSM state enter/exit actions
    void onfsmConnectEntered();
    void onfsmTransmitEntered();
    void onfsmTransmitExited();

private:
    // Member variables
    QTcpSocket*         m_socket;       // TCP socket used for communications to server
    QStateMachine*      m_clientFsm;      // FSM defining general client behaviour

private:
    void createClientFsm();             // Create client FSM
};

#endif // TESTCLIENT_H

testclient.cpp

#include "testclient.h"
#include <QState>
#include <QThread>      // Sleep

//-----------------------------------------------------------------------------
// PUBLIC METHODS
//-----------------------------------------------------------------------------

TestClient::TestClient(QObject *parent) : QObject(parent)
{
    m_socket = new QTcpSocket(this);

    connect(m_socket, SIGNAL(connected()),this, SLOT(onSocketConnected()));
    connect(m_socket, SIGNAL(disconnected()),this, SLOT(onSocketDisconnected()));
    connect(m_socket, SIGNAL(bytesWritten(qint64)),this, SLOT(onSocketBytesWritten(qint64)));
    connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
}

void TestClient::start()
{
    createClientFsm();
    m_clientFsm->start();
}


//-----------------------------------------------------------------------------
// TCP CONNECTION MANAGEMENT SLOTS
//-----------------------------------------------------------------------------
void TestClient::onSocketConnected()
{
    qDebug() << "connected...";
    emit fsmEvtConnected();
}

void TestClient::onSocketDisconnected()
{
    qDebug() << "disconnected...";
    emit fsmEvtError();
}

void TestClient::onSocketBytesWritten(qint64 bytes)
{
    qDebug() << bytes << " bytes written...";
}

void TestClient::onSocketError(QAbstractSocket::SocketError err)
{
    qDebug() << "socket error " << err;
}

//-----------------------------------------------------------------------------
// FSM MANAGEMENT
//-----------------------------------------------------------------------------
void TestClient::createClientFsm()
{
    m_clientFsm = new QStateMachine(this);

    // Create states
    QState* sConnect = new QState();
    QState* sTransmit = new QState();

    // Add transitions between states
    sConnect->addTransition(this, SIGNAL(fsmEvtConnected()), sTransmit);
    sTransmit->addTransition(this, SIGNAL(fsmEvtError()), sConnect);

    // Add entry actions to states
    connect(sConnect, SIGNAL(entered()), this, SLOT(onfsmConnectEntered()));
    connect(sTransmit, SIGNAL(entered()), this, SLOT(onfsmTransmitEntered()));

    // Add exit actions to states
    connect(sTransmit, SIGNAL(exited()), this, SLOT(onfsmTransmitExited()));

    // Create state machine
    m_clientFsm->addState(sConnect);
    m_clientFsm->addState(sTransmit);
    m_clientFsm->setInitialState(sConnect);
}


void TestClient::onfsmConnectEntered()
{
    qDebug() << "connecting...";
    m_socket->connectToHost("localhost", 11000);

    // Wait for connection result
    if(!m_socket->waitForConnected(10000))
    {
        qDebug() << "Error: " << m_socket->errorString();
        emit fsmEvtError();
    }
}

void TestClient::onfsmTransmitEntered()
{
    qDebug() << "sending data...";
    m_socket->write("TEST MESSAGE");
}

void TestClient::onfsmTransmitExited()
{
    qDebug() <<  "waiting before reconnection attempt...";
    QThread::sleep(2);
}

main.cpp

#include <QCoreApplication>
#include "testclient.h"

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

    TestClient client(&a);
    client.start();

    return a.exec();
}

To test, you can just launch netcat (nc -l -p 11000) , then close the nc process after receiving TEST MESSAGE and finally relaunch it again. The second time, TEST MESSAGE is not received, and we don't have the onSocketBytesWritten() printout, see below:

connecting...
connected...
sending data...
12  bytes written...    <<<<<<<<<< Correct transmission, event fires up
socket error  QAbstractSocket::RemoteHostClosedError
disconnected...
waiting before reconnection attempt...
connecting...
connected...
sending data...    <<<<<<<<<< No transmission, event does not fire up, no socket errors!

EDIT1: I found out that if I create the QTcpSocket on connection and destroy it on disconnection, the problem does not happen. Is this the expected/proper way to use sockets?

Wouldn't it be possible instead to create the socket just once and just connect/disconnect? Maybe it is just a matter of flushing or cleaning up in a specific manner, but I could not find it so far.

Here are the modifications that make the code above work on server-side disconnection:

Move socket creation from class constructor to onfsmConnectEntered() - handler for entry in the "Connect" QState:

void TestClient::onfsmConnectEntered()
{    
    m_socket = new QTcpSocket(this);

    connect(m_socket, SIGNAL(connected()),this, SLOT(onSocketConnected()));
    connect(m_socket, SIGNAL(disconnected()),this, SLOT(onSocketDisconnected()));
    connect(m_socket, SIGNAL(bytesWritten(qint64)),this, SLOT(onSocketBytesWritten(qint64)));
    connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));

    qDebug() << "connecting...";
    m_socket->connectToHost("localhost", 11000);
    // The rest of the method is the same
}

Delete the socket on disconnection, so that it is deallocated and will be created again on reconnection:

void TestClient::onSocketDisconnected()
{
    qDebug() << "disconnected...";
    m_socket->deleteLater();
    m_socket = nullptr;

    emit fsmEvtError();
}
1

1 Answers

1
votes

Do not use waitForX methods as they block the event loop and prevent them from using that resource as the signals do not do their job correctly or the QStateMachine.

Considering the above, the solution is:

void TestClient::onfsmConnectEntered()
{
    m_socket->connectToHost("localhost", 11000);
}

But even so your code has errors since it does not consider other cases such as:

  • If when you start the client the server is not running, your application will try to connect the error will be launched and nothing else.

  • If the server fails for a longer time than the 10000 ms timeout set to waitForConnected(), the same will happen as in the previous case.

Then the idea is to try to connect until you are sure of the connection and that can be done through a QTimer with an appropriate period.

testclient.h

#ifndef TESTCLIENT_H
#define TESTCLIENT_H

#include <QObject>

class QTcpSocket;
class QStateMachine;
class QTimer;

#include <QAbstractSocket>

class TestClient : public QObject
{
    Q_OBJECT
public:
    explicit TestClient(QObject *parent = nullptr);
public slots:
    void start();
signals:
    // FSM events
    void fsmEvtConnected();
    void fsmEvtError();
private slots:
    void onSocketConnected();                       // Notify connection to TCP server
    void onSocketDisconnected();                    // Notify disconnection from TCP server
    void onSocketBytesWritten(qint64 bytes);        // Notify number of bytes written to TCP server
    void onSocketError(QAbstractSocket::SocketError err);
    // FSM state enter/exit actions
    void onfsmConnectEntered();
    void onfsmTransmitEntered();
private:
    // Member variables
    QTcpSocket*         m_socket;       // TCP socket used for communications to server
    QStateMachine*      m_clientFsm;      // FSM defining general client behaviour
    QTimer*             m_timer;
private:
    void createClientFsm();             // Create client FSM
    void tryConnect();
};

#endif // TESTCLIENT_H

testclient.cpp

#include "testclient.h"
#include <QState>
#include <QStateMachine>
#include <QTcpSocket>
#include <QThread>      // Sleep
#include <QTimer>

//-----------------------------------------------------------------------------
// PUBLIC METHODS
//-----------------------------------------------------------------------------
TestClient::TestClient(QObject *parent) : QObject(parent)
{
    m_socket = new QTcpSocket(this);
    m_timer = new QTimer(this);
    m_timer->setInterval(100);
    connect(m_timer, &QTimer::timeout, this, &TestClient::tryConnect);
    connect(m_socket, &QAbstractSocket::connected,this, &TestClient::onSocketConnected);
    connect(m_socket, &QAbstractSocket::disconnected,this, &TestClient::onSocketDisconnected);
    connect(m_socket, &QIODevice::bytesWritten,this, &TestClient::onSocketBytesWritten);
    connect(m_socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &TestClient::onSocketError);
}
void TestClient::start()
{
    createClientFsm();
    m_clientFsm->start();
}
//-----------------------------------------------------------------------------
// TCP CONNECTION MANAGEMENT SLOTS
//-----------------------------------------------------------------------------
void TestClient::onSocketConnected()
{
    m_timer->stop();
    qDebug() << "connected...";
    emit fsmEvtConnected();
}
void TestClient::onSocketDisconnected()
{
    qDebug() << "disconnected...";
    emit fsmEvtError();
}
void TestClient::onSocketBytesWritten(qint64 bytes)
{
    qDebug() << bytes << " bytes written...";
}
void TestClient::onSocketError(QAbstractSocket::SocketError err)
{
    qDebug() << "socket error " << err;
}
//-----------------------------------------------------------------------------
// FSM MANAGEMENT
//-----------------------------------------------------------------------------
void TestClient::createClientFsm()
{
    m_clientFsm = new QStateMachine(this);
    // Create states
    QState* sConnect = new QState();
    QState* sTransmit = new QState();
    // Add transitions between states
    sConnect->addTransition(this, SIGNAL(fsmEvtConnected()), sTransmit);
    sTransmit->addTransition(this, SIGNAL(fsmEvtError()), sConnect);
    // Add entry actions to states
    connect(sConnect, &QAbstractState::entered, this, &TestClient::onfsmConnectEntered);
    connect(sTransmit, &QAbstractState::entered, this, &TestClient::onfsmTransmitEntered);
    // Create state machine
    m_clientFsm->addState(sConnect);
    m_clientFsm->addState(sTransmit);
    m_clientFsm->setInitialState(sConnect);
}
void TestClient::tryConnect(){
    m_socket->connectToHost("localhost", 11000);
}
void TestClient::onfsmConnectEntered()
{
    m_timer->start();
}
void TestClient::onfsmTransmitEntered()
{
    qDebug() << "sending data...";
    m_socket->write("TEST MESSAGE");
}