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 ?
QtWS25 Last Chance

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.
  • C Offline
    C Offline
    CHPOG
    wrote on 10 Oct 2019, 14:54 last edited by
    #1

    Hi,

    I have a simple class to connect to a sever using a QTcpSocket. I test this class with QUnit.
    But when I want to check that my socket is correctly open by QVERIFY(socket->open() ==true) the test fails. I just call socket->open() with no QVERIFY and the I am able to read data from my socket. But a SocketTimeoutError is raised. I d'ont know why ? And what is the cause.
    The error is raised when I'am doing this :

    /**
     * @brief UniwheelConnection::isWaitingForCommand
     * @return true if sensor wait command
     */
    bool UniwheelConnection::isWaitingForCommand() {
        QString fullData;
        int maxSize = 5000;
        //We wait end of data transmission but not too much if device is in start mode
        while(this->waitForReadyRead(1000) && fullData.size() < maxSize) {
            QByteArray data = readAll();
            skipHeader(&data);
            qDebug()<< "data added  " << data.toHex();
            QString dataAsString(data);
            qDebug()<< "data added in string  " << dataAsString;
            fullData.append(dataAsString);
        }
        qDebug()<< "isWaitingForCommand " << fullData;
        qDebug()<< "end ok " << fullData.endsWith(PROMPT_END);
    
        return fullData.endsWith(PROMPT_END) && this->atEnd();
    }
    
    

    Any idea why ?

    J 1 Reply Last reply 10 Oct 2019, 15:03
    0
    • C CHPOG
      10 Oct 2019, 14:54

      Hi,

      I have a simple class to connect to a sever using a QTcpSocket. I test this class with QUnit.
      But when I want to check that my socket is correctly open by QVERIFY(socket->open() ==true) the test fails. I just call socket->open() with no QVERIFY and the I am able to read data from my socket. But a SocketTimeoutError is raised. I d'ont know why ? And what is the cause.
      The error is raised when I'am doing this :

      /**
       * @brief UniwheelConnection::isWaitingForCommand
       * @return true if sensor wait command
       */
      bool UniwheelConnection::isWaitingForCommand() {
          QString fullData;
          int maxSize = 5000;
          //We wait end of data transmission but not too much if device is in start mode
          while(this->waitForReadyRead(1000) && fullData.size() < maxSize) {
              QByteArray data = readAll();
              skipHeader(&data);
              qDebug()<< "data added  " << data.toHex();
              QString dataAsString(data);
              qDebug()<< "data added in string  " << dataAsString;
              fullData.append(dataAsString);
          }
          qDebug()<< "isWaitingForCommand " << fullData;
          qDebug()<< "end ok " << fullData.endsWith(PROMPT_END);
      
          return fullData.endsWith(PROMPT_END) && this->atEnd();
      }
      
      

      Any idea why ?

      J Offline
      J Offline
      JonB
      wrote on 10 Oct 2019, 15:03 last edited by
      #2

      @CHPOG

      But when I want to check that my socket is correctly open by QVERIFY(socket->open() ==true) the test fails.

      Do you intend QSocket::open() or QSocket::isOpen()?

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 Oct 2019, 18:30 last edited by
        #3

        Hi,

        Why are you using open ?
        The connectToHost method is usually used to setup a connection or maybe bind depending on your use case.

        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
        • C Offline
          C Offline
          CHPOG
          wrote on 11 Oct 2019, 07:12 last edited by CHPOG 10 Nov 2019, 07:12
          #4

          sorry it lacks some informations.
          My Tcpsocket is connectTohost by doing this in the constructor :

              m_socket->connectToHost(ipAddress,port);
          
          

          And the function open call the open one of QIOdevice and return a boolean value that is set to true when signal connected is emitted.

          bool WifiUniwheel::open() {
              qDebug() << m_socket->state();
              UniwheelConnection::open();
              qDebug() << m_socket->state();
              return this->m_isConnected;
          }
          

          May be I must not call open of super class. My aim is to have a parent class which could be instantiate as a TCPSocket or a RS232 connection. Like an interface in Java.

          But by inspecting the result of my log I saw that the boolean value to know if this is open is set to true. The mystery is why this Socket error is raised ? Because I do a connectToHost and then an open ? I try this but the error raised again.

          J 1 Reply Last reply 11 Oct 2019, 07:41
          0
          • C CHPOG
            11 Oct 2019, 07:12

            sorry it lacks some informations.
            My Tcpsocket is connectTohost by doing this in the constructor :

                m_socket->connectToHost(ipAddress,port);
            
            

            And the function open call the open one of QIOdevice and return a boolean value that is set to true when signal connected is emitted.

            bool WifiUniwheel::open() {
                qDebug() << m_socket->state();
                UniwheelConnection::open();
                qDebug() << m_socket->state();
                return this->m_isConnected;
            }
            

            May be I must not call open of super class. My aim is to have a parent class which could be instantiate as a TCPSocket or a RS232 connection. Like an interface in Java.

            But by inspecting the result of my log I saw that the boolean value to know if this is open is set to true. The mystery is why this Socket error is raised ? Because I do a connectToHost and then an open ? I try this but the error raised again.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 11 Oct 2019, 07:41 last edited by
            #5

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

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

            J 1 Reply Last reply 11 Oct 2019, 07:44
            1
            • J jsulm
              11 Oct 2019, 07:41

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

              J Offline
              J Offline
              JonB
              wrote on 11 Oct 2019, 07:44 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....

              J 1 Reply Last reply 11 Oct 2019, 07:45
              0
              • 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.HilkJ 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.HilkJ Online
                                J.HilkJ Online
                                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

                                  1/16

                                  10 Oct 2019, 14:54

                                  • Login

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