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 why SocketTimeoutError is raised ?
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket why SocketTimeoutError is raised ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 2.2k Views 1 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.
  • jsulmJ jsulm

    @CHPOG If you want to check whether open or not call https://doc.qt.io/qt-5/qiodevice.html#isOpen not open()

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #6

    @jsulm

    @CHPOG If you want to check whether open or not call https://doc.qt.io/qt-5/qiodevice.html#isOpen not open()

    Which is why I asked the OP that above, but he didn't respond....

    jsulmJ 1 Reply Last reply
    0
    • JonBJ JonB

      @jsulm

      @CHPOG If you want to check whether open or not call https://doc.qt.io/qt-5/qiodevice.html#isOpen not open()

      Which is why I asked the OP that above, but he didn't respond....

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

      @JonB said in QTcpSocket why SocketTimeoutError is raised ?:

      but he didn't respond....

      This is often the case...

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

      JonBJ 1 Reply Last reply
      1
      • jsulmJ jsulm

        @JonB said in QTcpSocket why SocketTimeoutError is raised ?:

        but he didn't respond....

        This is often the case...

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #8

        @jsulm
        Sigh, yes :)
        I'm wondering whether the OP's "timeout error" is because he's open()ing the same socket twice, and maybe that's what you get if you try to do that....

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CHPOG
          wrote on last edited by
          #9

          Thanks for your answer and sorry if I didn't directly try what you said @JonB .
          So I try to remove all the call to open(). I call open() in my constructor of my Rs232 object and connectToHost in the object of my socket connection.
          I had the same error raised.
          But right to check that connection is open isOpen works fine.
          But as I have this socketTimeoutError my tests fails.

          jsulmJ 1 Reply Last reply
          0
          • C CHPOG

            Thanks for your answer and sorry if I didn't directly try what you said @JonB .
            So I try to remove all the call to open(). I call open() in my constructor of my Rs232 object and connectToHost in the object of my socket connection.
            I had the same error raised.
            But right to check that connection is open isOpen works fine.
            But as I have this socketTimeoutError my tests fails.

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

            @CHPOG said in QTcpSocket why SocketTimeoutError is raised ?:

            I call open() in my constructor of my Rs232 object and connectToHost in the object of my socket connection.

            Why do you call open()? connectToHost() is enough.

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

            1 Reply Last reply
            1
            • C Offline
              C Offline
              CHPOG
              wrote on last edited by
              #11

              @jsulm
              Sorry if I am not really clear.
              But I have two classes one for a RS232 which call open() in the constructor.
              An other one for Socket which call connectToHost() in the constructor.
              So for now I am not calling open() for Socket connection. I only use connectToHost().
              And the SocketTimeouError is still raised.

              jsulmJ 1 Reply Last reply
              0
              • C CHPOG

                @jsulm
                Sorry if I am not really clear.
                But I have two classes one for a RS232 which call open() in the constructor.
                An other one for Socket which call connectToHost() in the constructor.
                So for now I am not calling open() for Socket connection. I only use connectToHost().
                And the SocketTimeouError is still raised.

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

                @CHPOG You wrote: "But when I want to check that my socket is correctly open by QVERIFY(socket->open() ==true) the test fails". So, you do call open() to check whether the socket is open? If so, then please change it to isOpen().

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

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  CHPOG
                  wrote on last edited by
                  #13

                  @jsulm Yes you're right I write this at the start of my post. But after I follow you advice and use conn->isOpen() instead. This does not solve my issue with SocketTimeoutError.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    CHPOG
                    wrote on last edited by CHPOG
                    #14

                    To be easier for everyone to understand I test with a main and get the same error.

                    #include <QCoreApplication>
                    #include <QTcpSocket>
                    #include <QDebug>
                    
                    int main(int argc, char *argv[])
                    {
                        QCoreApplication a(argc, argv);
                        QTcpSocket *socket = new QTcpSocket();
                        socket->connectToHost("10.1.0.1",1470);
                        socket->write("list\r");
                        QString fullData;
                        int maxSize = 5000;
                        //We wait end of data transmission but not too much if device is in start mode
                        while(socket->waitForReadyRead(1000) && fullData.size() < maxSize) {
                            QByteArray data = socket->readAll();
                            //Removing TCP header
                            data.remove(0,32);
                            //qDebug()<< "data added  " << data.toHex();
                            QString dataAsString(data);
                            //qDebug()<< "data added in string  " << dataAsString;
                            fullData.append(dataAsString);
                        }
                        qDebug()<< "isWaitingForCommand " << fullData;
                        qDebug()<< "ERROR = " << socket->errorString();
                        socket->close();
                    
                        return a.exec();
                    
                    }
                    

                    I can do more simple. And I always do not understood why this error is raised.
                    Below this is the ouput :

                    isWaitingForCommand  "; value ; unity ; minimum value ; maximum value ; description\r\nsample_rate     ;      800 ;           Hz ;      200 ;     1100 ; FrÚquence d'Úchantillonnage de la centrale ADIS ; \r\nsensors         ;        7 ;              ;        1 ;        7 ; Capteurs actifs (BIT0=gyros BIT1=accÚlÚros BIT2=magnÚtos) ; \r\nsynchro         ;        0 ;              ;        0 ;        1 ; Active l'emission d'un 0x02 en dÚbut de trame ; \r\ncounter         ;        0 ;              ;        0 ;    compteur de trame avant les donnÚes capteurs ; \r\nbt_rate         ;   230400 ;              ;   115200 ;   230400 ; Baudrate pour la communication bluetooth ; \r\npassword        ;        0 ;              ;        0 ;   999999 ; Mot de passe pour accÞs aux paramÞtres systÞmes ; \r\nsensor$ "
                    ERROR =  "Network operation timed out"
                    
                    J.HilkJ 1 Reply Last reply
                    0
                    • C CHPOG

                      To be easier for everyone to understand I test with a main and get the same error.

                      #include <QCoreApplication>
                      #include <QTcpSocket>
                      #include <QDebug>
                      
                      int main(int argc, char *argv[])
                      {
                          QCoreApplication a(argc, argv);
                          QTcpSocket *socket = new QTcpSocket();
                          socket->connectToHost("10.1.0.1",1470);
                          socket->write("list\r");
                          QString fullData;
                          int maxSize = 5000;
                          //We wait end of data transmission but not too much if device is in start mode
                          while(socket->waitForReadyRead(1000) && fullData.size() < maxSize) {
                              QByteArray data = socket->readAll();
                              //Removing TCP header
                              data.remove(0,32);
                              //qDebug()<< "data added  " << data.toHex();
                              QString dataAsString(data);
                              //qDebug()<< "data added in string  " << dataAsString;
                              fullData.append(dataAsString);
                          }
                          qDebug()<< "isWaitingForCommand " << fullData;
                          qDebug()<< "ERROR = " << socket->errorString();
                          socket->close();
                      
                          return a.exec();
                      
                      }
                      

                      I can do more simple. And I always do not understood why this error is raised.
                      Below this is the ouput :

                      isWaitingForCommand  "; value ; unity ; minimum value ; maximum value ; description\r\nsample_rate     ;      800 ;           Hz ;      200 ;     1100 ; FrÚquence d'Úchantillonnage de la centrale ADIS ; \r\nsensors         ;        7 ;              ;        1 ;        7 ; Capteurs actifs (BIT0=gyros BIT1=accÚlÚros BIT2=magnÚtos) ; \r\nsynchro         ;        0 ;              ;        0 ;        1 ; Active l'emission d'un 0x02 en dÚbut de trame ; \r\ncounter         ;        0 ;              ;        0 ;    compteur de trame avant les donnÚes capteurs ; \r\nbt_rate         ;   230400 ;              ;   115200 ;   230400 ; Baudrate pour la communication bluetooth ; \r\npassword        ;        0 ;              ;        0 ;   999999 ; Mot de passe pour accÞs aux paramÞtres systÞmes ; \r\nsensor$ "
                      ERROR =  "Network operation timed out"
                      
                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #15

                      @CHPOG since you insist on using the synchronous api, you should at least use https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected

                      for the connection to the host to be established...


                      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.

                      1 Reply Last reply
                      1
                      • C Offline
                        C Offline
                        CHPOG
                        wrote on last edited by
                        #16

                        @J-Hilk I tried what you said. But the error is still raised. And the message "Connected!" is displayed.
                        The two lines of code are added just after the connectToHost.

                        1 Reply Last reply
                        0

                        • Login

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