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 ?

QTcpSocket why SocketTimeoutError is raised ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 2.0k 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.
  • J JonB
    11 Oct 2019, 07:44

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

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 11 Oct 2019, 07:45 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

    J 1 Reply Last reply 11 Oct 2019, 07:47
    1
    • J jsulm
      11 Oct 2019, 07:45

      @JonB said in QTcpSocket why SocketTimeoutError is raised ?:

      but he didn't respond....

      This is often the case...

      J Offline
      J Offline
      JonB
      wrote on 11 Oct 2019, 07:47 last edited by JonB 10 Nov 2019, 07:47
      #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 11 Oct 2019, 11:20 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.

        J 1 Reply Last reply 11 Oct 2019, 11:50
        0
        • C CHPOG
          11 Oct 2019, 11:20

          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.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 11 Oct 2019, 11:50 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 14 Oct 2019, 07:28 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.

            J 1 Reply Last reply 14 Oct 2019, 07:36
            0
            • C CHPOG
              14 Oct 2019, 07:28

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 14 Oct 2019, 07:36 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 14 Oct 2019, 09:31 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 14 Oct 2019, 09:56 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 1 Reply Last reply 14 Oct 2019, 10:34
                  0
                  • C CHPOG
                    14 Oct 2019, 09:56

                    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 Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 14 Oct 2019, 10:34 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 14 Oct 2019, 10:54 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

                      16/16

                      14 Oct 2019, 10:54

                      • Login

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