Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Socket doesn't fire "Connected()" signal

Socket doesn't fire "Connected()" signal

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rackover
    wrote on last edited by
    #1

    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 :

    ///Header
    #ifndef SOCKETTEST_H
    #define SOCKETTEST_H
    
    #include <QString>
    #include <QTcpSocket>
    #include <QJsonObject>
    
    namespace Pico{
        namespace Server{
    
        class Funcs : public QObject
        {
            Q_OBJECT
            public:
                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();
    
            signals:
                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);
    
            private:
    
            };
        }
    }
    
    #endif // SOCKETTEST_H
    

    namespace Pico{
        namespace Server{
    
            Pico::Logging::Funcs logging;
    
            Pico::Settings::FD_CONFIG FDC;
    
            Funcs::Funcs(QObject *parent) :
                QObject(parent)
            {
    
    
                ///
                /// Connecting the wires for async system
                ///
                connect(&socket,
                        SIGNAL(connected()),
                        SLOT(OnConnected()));
    
                connect(&socket,
                        SIGNAL(error(QAbstractSocket::SocketError)),
                        SLOT(OnSocketError(QAbstractSocket::SocketError)));
    
                connect(&socket,
                        SIGNAL(readyRead()),
                        SLOT(OnReceivedData()));
    
                connect(&socket,
                        SIGNAL(disconnected()),
                        SLOT(OnClosedConnection()));
    
                socket.connectToHost(QString::fromStdString(FDC.SERVER_ADDRESS), (quint16) FDC.SERVER_PORT);
                socket.setSocketOption(QAbstractSocket::KeepAliveOption, 1);
    
            }
    ...stuff...
    

    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 ?

    kshegunovK 1 Reply Last reply
    0
    • R rackover

      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 :

      ///Header
      #ifndef SOCKETTEST_H
      #define SOCKETTEST_H
      
      #include <QString>
      #include <QTcpSocket>
      #include <QJsonObject>
      
      namespace Pico{
          namespace Server{
      
          class Funcs : public QObject
          {
              Q_OBJECT
              public:
                  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();
      
              signals:
                  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);
      
              private:
      
              };
          }
      }
      
      #endif // SOCKETTEST_H
      

      namespace Pico{
          namespace Server{
      
              Pico::Logging::Funcs logging;
      
              Pico::Settings::FD_CONFIG FDC;
      
              Funcs::Funcs(QObject *parent) :
                  QObject(parent)
              {
      
      
                  ///
                  /// Connecting the wires for async system
                  ///
                  connect(&socket,
                          SIGNAL(connected()),
                          SLOT(OnConnected()));
      
                  connect(&socket,
                          SIGNAL(error(QAbstractSocket::SocketError)),
                          SLOT(OnSocketError(QAbstractSocket::SocketError)));
      
                  connect(&socket,
                          SIGNAL(readyRead()),
                          SLOT(OnReceivedData()));
      
                  connect(&socket,
                          SIGNAL(disconnected()),
                          SLOT(OnClosedConnection()));
      
                  socket.connectToHost(QString::fromStdString(FDC.SERVER_ADDRESS), (quint16) FDC.SERVER_PORT);
                  socket.setSocketOption(QAbstractSocket::KeepAliveOption, 1);
      
              }
      ...stuff...
      

      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 ?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

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

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      4
      • R Offline
        R Offline
        rackover
        wrote on last edited by
        #3

        @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.

        aha_1980A 1 Reply Last reply
        0
        • R rackover

          @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.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rackover

          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.

          Regards

          Qt has to stay free or it will die.

          kshegunovK 1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • aha_1980A aha_1980

              @rackover

              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.

              Regards

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @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*

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1
              • R Offline
                R Offline
                rackover
                wrote on last edited by
                #7

                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 ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • R Offline
                    R Offline
                    rackover
                    wrote on last edited by
                    #9

                    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.

                    jsulmJ 1 Reply Last reply
                    0
                    • R rackover

                      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.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3
                      • R Offline
                        R Offline
                        rackover
                        wrote on last edited by rackover
                        #11

                        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 ?

                        jsulmJ 1 Reply Last reply
                        0
                        • R rackover

                          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 ?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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
                          private:
                              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();
                          ...
                          

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          3

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved