Qtcpclient in "wait for connected" loop and reconnect after connection breaks



  • hi,
    because i dont know who starts first: my qt programm(tcp client) or the tcp server,
    i take care of the case that the client programm starts first.

    so i have a runtime which tries to connect, and if fails (error-method), it starts (the init-method) again.
    this works well for the first connection, but, when the connection is broken, it doesn't reconnect,
    it doesn't start the "try-to-connect"-loop again.

    any ideas?

    tcpclient.h
    @
    #ifndef TCPCLIENT_H
    #define TCPCLIENT_H

    #include <QObject>
    #include <QTcpSocket>
    #include <QDebug>
    #include <QAbstractSocket>
    #include <QDateTime>
    #include <QString>
    #include <QFile>
    #include <QString>
    #include <QTextStream>

    class tcpclient : public QObject
    {
    Q_OBJECT
    public:
    explicit tcpclient(QObject *parent = 0);
    QTcpSocket *tcp_client_socket;

    signals:
    void sig_client_verbunden();
    void sig_client_verbunden_un();

    private slots:
    void client_error();
    void client_connected();
    void client_disconnected();

    public slots:
    void init();
    };

    #endif // TCPCLIENT_H
    @

    tcpclient.cpp
    @
    #include "tcpclient.h"

    tcpclient::tcpclient(QObject *parent) :
    QObject(parent)
    {
    tcp_client_socket = new QTcpSocket(this);
    }

    void tcpclient::init(){
    qDebug() << "init";
    tcp_client_socket->disconnect();
    tcp_client_socket->disconnectFromHost();
    connect(tcp_client_socket,SIGNAL(connected()), this, SLOT(client_connected()));
    connect(tcp_client_socket,SIGNAL(disconnected()),this,SLOT(client_disconnected()));
    connect(tcp_client_socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(client_error()));
    tcp_client_socket->connectToHost("127.0.0.1",77);
    }

    void tcpclient::client_connected()
    {
    qDebug() << "DEBUG: R->G: VERBUNDEN ALS CLIENT!";
    emit sig_client_verbunden();
    }

    void tcpclient::client_disconnected()
    {
    qDebug() << "DEBUG: R->G: DISCONNECTED!";
    emit sig_client_verbunden_un();

    }

    void tcpclient::client_error()
    {
    qDebug() << "DEBUG: R->G: ERROR!";
    emit sig_client_verbunden_un();
    init();
    }
    @

    so what happens after the connection is broken:
    the error signal is emitted so the client_error() method runs one time, and it launches the init() method.
    (i know because of qDebug() output)
    but then finish.
    i dont know why it doesn't go to client_error() again, after failing.

    thanks a lot

    PS:
    i create one object from this class inside my main-ui-class:
    first i initiate it in the heap with new,
    than i make connections to the signals,
    and then i initialise it with
    tcpclient->init();

    like this no problems with the signals ( that i don't catch the connection one, because its emmited before the connection of signals and slots is made)



  • you need something like this in your init slot:

    @
    connect(tcp_client_socket, SIGNAL(error(QAbstractSocket::SocketError )), this,
    SLOT(HandleTcpError(QAbstractSocket::SocketError)))
    @

    and in the handleTcpError(..) slot you can then check the error and depending on the error try to reconnect

    @
    void tcpclient::handleTcpError(QAbstractSocket::SocketError a_socketError)
    {
    if(tcp_client_socket)
    {
    qDebug() << QString("tcpclient::handleTcpError: %1").arg(tcp_client_socket->errorString());
    }
    ReleaseConnection();
    // Try to reconnect after 5 seconds
    QTimer::singleShot(5000, this, SLOT(init()));
    }

    void tcpclient::releaseConnection()
    {
    if (!tcp_client_socket)
    return;

    if (tcp_client_socket->isValid())
    tcp_client_socket->disconnectFromHost();

    //disconnect all signal->slot connections related to your socket

    tcp_client_socket->deleteLater();
    tcp_client_socket = 0;
    }
    @

    For this to work you need to create your tcp socket in the init() slot and not in the constructor. something like this:
    @
    if(!tcp_client_socket)
    {
    tcp_client_socket = QTcpSocket(this);
    //do all the connect calls
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.