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. QTcpSocket always connectingState
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket always connectingState

Scheduled Pinned Locked Moved General and Desktop
15 Posts 7 Posters 1.8k Views 2 Watching
  • 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.
  • S Shen sida

    Hi all

    I try to connect a tcp socket to send data. But after connectToHost(), the state of socket is connectingState not the connectedState.

    I also set the option as follow:

    socket->setSocketOption(QAbstractSocket::KeepAliveOption,1);
    socket->setSocketOption(QAbstractSocket::LowDelayOption,1);
    

    I'm working on Qt 6.4.2 on Windows 11.
    What should I do?
    Let me know if you need more information.

    Best regards

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #2

    @Shen-sida more information please,

    my 0815 guess is, you're immediately checking the state, and do not listen to the state changed signal and therefore never progress. The connectedState will only come when the event loop is running and processing events.


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    S 1 Reply Last reply
    1
    • J.HilkJ J.Hilk

      @Shen-sida more information please,

      my 0815 guess is, you're immediately checking the state, and do not listen to the state changed signal and therefore never progress. The connectedState will only come when the event loop is running and processing events.

      S Offline
      S Offline
      Shen sida
      wrote on last edited by Shen sida
      #3

      @J-Hilk Thanks for reply first.

      I have listen the state changed signal before connectToHost(), see the code as follow.

      void CenterManageSystem::init()
      {    
          socket = new QTcpSocket(this);
      
          socket->setSocketOption(QAbstractSocket::KeepAliveOption,1);
          socket->setSocketOption(QAbstractSocket::LowDelayOption,1);
      
          connect(socket, &QTcpSocket::connected, this, &CenterManageSystem::socketConnected);
          connect(socket, &QTcpSocket::disconnected, this, &CenterManageSystem::socketDisconnected);
          connect(socket, &QTcpSocket::stateChanged, this, &CenterManageSystem::socketStateChanged);
          connect(socket, &QTcpSocket::readyRead, this, &CenterManageSystem::socketReceivedData);
      
          socket->connectToHost("192.168.5.181",24680);
      }
      
      void CenterManageSystem::socketConnected()
      {
          qDebug()<<"socket connected";
      }
      
      void CenterManageSystem::socketDisconnected()
      {
          qDebug()<<"CMS socket disconnected";
      }
      
      void CenterManageSystem::socketStateChanged(QAbstractSocket::SocketState state)
      {
          qDebug()<<"Socket state changed:"<<state;
      }
      
      void CenterManageSystem::socketReceivedData()
      {
          receiveData.append(socket->readAll());
      
          // TODO process data
      }
      
      void CenterManageSystem::socketError(QAbstractSocket::SocketError error)
      {
          qDebug()<<error;
      }
      
      
      

      Then I only get the output:

      Socket state changed: QAbstractSocket::HostLookupState
      Socket state changed: QAbstractSocket::ConnectingState
      
      Christian EhrlicherC 1 Reply Last reply
      0
      • S Shen sida

        @J-Hilk Thanks for reply first.

        I have listen the state changed signal before connectToHost(), see the code as follow.

        void CenterManageSystem::init()
        {    
            socket = new QTcpSocket(this);
        
            socket->setSocketOption(QAbstractSocket::KeepAliveOption,1);
            socket->setSocketOption(QAbstractSocket::LowDelayOption,1);
        
            connect(socket, &QTcpSocket::connected, this, &CenterManageSystem::socketConnected);
            connect(socket, &QTcpSocket::disconnected, this, &CenterManageSystem::socketDisconnected);
            connect(socket, &QTcpSocket::stateChanged, this, &CenterManageSystem::socketStateChanged);
            connect(socket, &QTcpSocket::readyRead, this, &CenterManageSystem::socketReceivedData);
        
            socket->connectToHost("192.168.5.181",24680);
        }
        
        void CenterManageSystem::socketConnected()
        {
            qDebug()<<"socket connected";
        }
        
        void CenterManageSystem::socketDisconnected()
        {
            qDebug()<<"CMS socket disconnected";
        }
        
        void CenterManageSystem::socketStateChanged(QAbstractSocket::SocketState state)
        {
            qDebug()<<"Socket state changed:"<<state;
        }
        
        void CenterManageSystem::socketReceivedData()
        {
            receiveData.append(socket->readAll());
        
            // TODO process data
        }
        
        void CenterManageSystem::socketError(QAbstractSocket::SocketError error)
        {
            qDebug()<<error;
        }
        
        
        

        Then I only get the output:

        Socket state changed: QAbstractSocket::HostLookupState
        Socket state changed: QAbstractSocket::ConnectingState
        
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        And where do you actually connect to an endpoint?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S Christian EhrlicherC 2 Replies Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          And where do you actually connect to an endpoint?

          S Offline
          S Offline
          Shen sida
          wrote on last edited by
          #5

          @Christian-Ehrlicher
          For now I connect it on the same device, that means the server and this program are running on the same devices.
          I also tried connect with the loop back IP (127.0.0.1) but it was still in connectingState .

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            And where do you actually connect to an endpoint?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Ok, now there is a connect. But do you listen and accept on the server side?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              Ok, now there is a connect. But do you listen and accept on the server side?

              S Offline
              S Offline
              Shen sida
              wrote on last edited by
              #7

              @Christian-Ehrlicher Sure, the server has been finished and used with other programs.

              Christian EhrlicherC 1 Reply Last reply
              0
              • S Shen sida

                @Christian-Ehrlicher Sure, the server has been finished and used with other programs.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                I doubt the server accepts the connection or even get a connection. Make sure there is no firewall and the server really gets a connection request.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                S 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  I doubt the server accepts the connection or even get a connection. Make sure there is no firewall and the server really gets a connection request.

                  S Offline
                  S Offline
                  Shen sida
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher I turn off all the firewalls and try again, but it'is still in the connecting state.

                  JonBJ JoeCFDJ 2 Replies Last reply
                  0
                  • S Shen sida

                    @Christian-Ehrlicher I turn off all the firewalls and try again, but it'is still in the connecting state.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #10

                    @Shen-sida
                    Test your server outside of a Qt program, e.g. telnet 192.168.5.181 24680. Does that actually connect?

                    SGaistS 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Shen-sida
                      Test your server outside of a Qt program, e.g. telnet 192.168.5.181 24680. Does that actually connect?

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Hi,

                      To add to my fellow, you did not connect the errorOccurred signal.

                      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
                      1
                      • S Shen sida

                        @Christian-Ehrlicher I turn off all the firewalls and try again, but it'is still in the connecting state.

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #12

                        @Shen-sida You can also ping your server to see if it is visible.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Shen sida
                          wrote on last edited by
                          #13

                          @JoeCFD @SGaist

                          I did more tests and there are some questions about that:

                          1. when I want to reconnect the socket, there is an error id 19, what is it.
                          // In my tick function
                           if(QAbstractSocket::ConnectedState != socket->state() && socketTimer.elapsed() >= 5000)
                              {
                                  qDebug()<<"Try to reconnect socket";
                                  socket->disconnectFromHost();
                                  socket->connectToHost("192.168.5.233",24680);
                                  socketTimer.restart();
                              }
                          
                          // output as follow
                          Try to reconnect socket
                          QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233"
                          QAbstractSocket::SocketError(19)
                          
                          1. I try to setup the socket in main theard and it work well! But it doesn't work if the socket setup in other thread.

                          2. What's the different between normal object pointer and QPointer<>? I'm still using QPointer<> to declarate this task with tcp socket

                          class CenterManageSystem : public QObject
                          {
                              ...
                          private:
                              QTcpSocket *socket;
                          }
                          
                          class MainWindow : public QMainWindow
                          {
                              Q_OBJECT
                          
                          public:
                              MainWindow(QWidget *parent = nullptr);
                              ~MainWindow();
                          
                          private:
                              Ui::MainWindow *ui;
                              QPointer<CenterManageSystem> pCMS = nullptr;
                          };
                          
                          // move the object to thread
                                  pCMS = new CenterManageSystem();
                                  CenterManageSystemThread = new QThread();
                                  QThread::connect(CenterManageSystemThread, &QThread::started, pCMS , &CenterManageSystem::main, Qt::DirectConnection);
                                  pCMS ->moveToThread(CenterManageSystemThread);
                                  CenterManageSystemThread->start();
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • S Shen sida

                            @JoeCFD @SGaist

                            I did more tests and there are some questions about that:

                            1. when I want to reconnect the socket, there is an error id 19, what is it.
                            // In my tick function
                             if(QAbstractSocket::ConnectedState != socket->state() && socketTimer.elapsed() >= 5000)
                                {
                                    qDebug()<<"Try to reconnect socket";
                                    socket->disconnectFromHost();
                                    socket->connectToHost("192.168.5.233",24680);
                                    socketTimer.restart();
                                }
                            
                            // output as follow
                            Try to reconnect socket
                            QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233"
                            QAbstractSocket::SocketError(19)
                            
                            1. I try to setup the socket in main theard and it work well! But it doesn't work if the socket setup in other thread.

                            2. What's the different between normal object pointer and QPointer<>? I'm still using QPointer<> to declarate this task with tcp socket

                            class CenterManageSystem : public QObject
                            {
                                ...
                            private:
                                QTcpSocket *socket;
                            }
                            
                            class MainWindow : public QMainWindow
                            {
                                Q_OBJECT
                            
                            public:
                                MainWindow(QWidget *parent = nullptr);
                                ~MainWindow();
                            
                            private:
                                Ui::MainWindow *ui;
                                QPointer<CenterManageSystem> pCMS = nullptr;
                            };
                            
                            // move the object to thread
                                    pCMS = new CenterManageSystem();
                                    CenterManageSystemThread = new QThread();
                                    QThread::connect(CenterManageSystemThread, &QThread::started, pCMS , &CenterManageSystem::main, Qt::DirectConnection);
                                    pCMS ->moveToThread(CenterManageSystemThread);
                                    CenterManageSystemThread->start();
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @Shen-sida said in QTcpSocket always connectingState:

                            QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233"

                            This seems to be your problem.

                            "But it doesn't work if the socket setup in other thread" - why do you want to have the socket in another thread? Qt is assynchronous. If you really want to do that then you have to make sure the QTcpSocket instance is created in another thread (when CenterManageSystem::main is called).

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

                            S 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Shen-sida said in QTcpSocket always connectingState:

                              QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233"

                              This seems to be your problem.

                              "But it doesn't work if the socket setup in other thread" - why do you want to have the socket in another thread? Qt is assynchronous. If you really want to do that then you have to make sure the QTcpSocket instance is created in another thread (when CenterManageSystem::main is called).

                              S Offline
                              S Offline
                              Shen sida
                              wrote on last edited by
                              #15

                              @jsulm said in QTcpSocket always connectingState:

                              If you really want to do that then you have to make sure the QTcpSocket instance is created in another thread (when CenterManageSystem::main is called).

                              Sure, the instance will be created in &CenterManageSystem::main.
                              Because this socket will process much data so I move it to another thread then the data can be sent by signal to update UI.

                              1 Reply Last reply
                              0
                              • S Shen sida marked this topic as a regular topic on

                              • Login

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