Qt6 topics merged with General

Socket doesn't fire "Connected()" signal

  • Hi,
    I've got a TCP socket that should connect to an address, at some port. When I fire it, it does never fire the connected() even, but it doesn't fire any error neither. Nothing happens.

    Here is my code :

    #ifndef SOCKETTEST_H
    #define SOCKETTEST_H
    #include <QString>
    #include <QTcpSocket>
    #include <QJsonObject>
    namespace Pico{
        namespace Server{
        class Funcs : public QObject
                explicit Funcs(QObject *parent = 0);
                QTcpSocket socket;
                QString MakeHelloMessage();
                void SendData(QString data);
                bool IsConnected();
                QString GetCommand(QJsonObject serverData);
                void LogIn(QJsonObject serverData);
                QJsonObject JsonDecode(QString jsonString);
                void CloseConnection(QString errorMsg="No error message.");
                QString IsolateMessage();
                void AddedGame(int id, QJsonObject game);
                void DeletedGame(int id);
                void Connected();
                void Disconnected();
                void StatusChanged(QString status);
            public slots:
                void OnReceivedData();
                void OnClosedConnection();
                void OnConnected();
                void OnSocketError(QAbstractSocket::SocketError error);
    #endif // SOCKETTEST_H

    namespace Pico{
        namespace Server{
            Pico::Logging::Funcs logging;
            Pico::Settings::FD_CONFIG FDC;
            Funcs::Funcs(QObject *parent) :
                /// Connecting the wires for async system
                socket.connectToHost(QString::fromStdString(FDC.SERVER_ADDRESS), (quint16) FDC.SERVER_PORT);
                socket.setSocketOption(QAbstractSocket::KeepAliveOption, 1);

    Both address and port are valid, I've debugged them. Each function I've connected the socket signals to immediately outputs a debug to file, and I've tested them separately (they work). The socket just never fires them and I don't know why.

    How can I investigate further ?

  • Moderators

    Use the Qt5 connect syntax and make sure you have success in establishing the signal-slot connection (i.e. QObject::connect returns true).

  • @kshegunov said in Socket doesn't fire "Connected()" signal:

    Use the Qt5 connect syntax and make sure you have success in establishing the signal-slot connection (i.e. QObject::connect returns true).

    Every one of these four connects return true. Imma see if I can convert this syntax to the QT5 syntax. Could it be that ? I'd prefer to stick to the old syntax if possible.

  • Lifetime Qt Champion


    the new syntax gives you compile time checking. if connect returns true, then the old syntax should work fine, too.

    looking at your snippet (from my limited phone sceen), may it be that socket is a stack variable and goes out of scope? create it on the heap then.


  • Lifetime Qt Champion


    From the looks of it, you are initiating a new connection while there's no event loop running yet.

    I'd recommend ensuring that it's properly started before doing it. You can use a single shot QTimer for that purpose for example.

  • Moderators

    @aha_1980 said in Socket doesn't fire "Connected()" signal:

    may it be that socket is a stack variable and goes out of scope?

    auto-storage with public scope *wink*

  • The event loop should already be started when this code runs, since it's called from a Main function which is started with a QTimer like you said.

    Here is the github page of this project, so you can have a better overview of the code :

    The file which I took the code from : https://github.com/Rackover/PicoFAF/blob/master/serverlink.cpp
    Main loop : https://github.com/Rackover/PicoFAF/blob/master/main.cpp

    How can I check if the socket is creating out of scope ? If I create it on the heap, will I still be able to access it after that ?

  • Lifetime Qt Champion

    The Run method doesn't do much. It declares some variables, do some connections and then it's finished so all these variables are destroyed and nothing will happen.

  • The run method is instanciating the server variable which will create the socket and start connecting, logging in and fire other stuff (look at the server.cpp). Isn't it ? That's how I wrote it but maybe I'm wrong.

  • Lifetime Qt Champion

    @rackover What @SGaist is saying: all variables inside Run() are LOCAL variables allocated on the stack and are destroyed as soon as Run() finishes!

    // All this variables only exist while Run() is being executed. As soon as Run() finishes they are all destroyed!
    Pico::Logging::Funcs logging;
    Pico::Server::Funcs server;
    Pico::Display::Funcs display;
    Pico::Input::Funcs input;
    Pico::Games::Funcs games;
    Pico::Downloader::Funcs downloader;

    Make this variables class members.

  • But if those variables are created within the Mainframe class definition, since some of them (like server) contains code to execute on instanciation, won't this make code run before the event loop is started ?

    If server is instantiated before the loop starts, the socket events won't trigger anyway, will they ?

  • Lifetime Qt Champion

    @rackover Simply add them as POINTERS in your class and create instances in Run(), then they will be initialized in Run(), but will not be destroyed when Run() finishes:

    // In class
        Pico::Logging::Funcs *logging;
        Pico::Server::Funcs *server;
        Pico::Display::Funcs *display;
        Pico::Input::Funcs *input;
        Pico::Games::Funcs *games;
        Pico::Downloader::Funcs *downloader;
    // In Run()
    logging = new Pico::Logging::Funcs();

Log in to reply