Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] TcpSocket timeout HELP

    General and Desktop
    3
    5
    5756
    Loading More Posts
    • 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.
    • N
      nitramuk last edited by

      Hi
      Hope this is the correct place to post ?

      Can anyone please give me any pointers as to why i keep getting time out errors from a tcpsocket ?

      According to a packet sniffer the data is being sent well within the timeslot but my code times out about 90% of the time !

      Function is pasted below
      The command gets sent ok
      The data gets sent ok
      The reply packet length gets received ok
      The reply packet times out 90% of the time !
      Even though according to network sniffing the data HAS BEEN sent from the server ??

      This has been bugging me for days.
      Any help much appreciated.

      @// Send a complex command with data attached
      void CtcpClient::slotCommand(const QString commandStr,const QString dataStr )
      {
      // Output stream
      QTextStream outText(socket);

      // Send command
      if (commandStr.length ()<4)
      {
      emit sigError (TCPERR_INVALID_COMMAND,"Invalid command");
      return;
      }
      outText << commandStr.left (4);
      outText.flush ();

      // Send data length
      if (dataStr.length ()<1)
      {
      emit sigError (TCPERR_INVALID_DATA,"Invalid data");
      return;
      }
      QString dataLen = QString::number (dataStr.size ()).rightJustified (4,'0');
      outText << dataLen;
      outText.flush ();

      // Send data
      outText << dataStr;
      outText.flush ();

      // Wait untill we have 4 bytes of data ( packet length )
      while (socket->bytesAvailable ()< 4)
      {
      if (!socket->waitForReadyRead(Timeout))
      {
      emit sigError(socket->error(), socket->errorString());
      return;
      }
      }

      // Then read packet length
      QTextStream inLength(socket);
      QString packetLen = inLength.read (4);

      // Wait untill we have packetlength of data

      while (socket->bytesAvailable ()< packetLen.toInt ())
      {
      if (!socket->waitForReadyRead(Timeout))
      {
      emit sigError(socket->error(), socket->errorString()); // +++++++ KEEPS EXITING HERE +++++++++
      return;
      }
      }

      // Then read data
      QTextStream inData(socket);
      QString data = inData.read (packetLen.toInt ());

      // Then emit sigReply(packet)
      emit sigReply (data);
      }@

      1 Reply Last reply Reply Quote 0
      • D
        dangelog last edited by

        You need to return to the event loop or call waitForBytesWritten on the socket.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply Reply Quote 0
        • A
          andre last edited by

          Yes. If possible at all, I would recommend you use the first approach peppe suggests: return to the eventloop.

          That means, that you split up your code in sections, and use QTcpSocket's signals and slots to trigger actions. Avoid the waitFor* functions if at all possible.

          1 Reply Last reply Reply Quote 0
          • N
            nitramuk last edited by

            Hi.
            Many thanks peppe & Andre.
            Between the two of you my problem is solved !

            Had to do a fairly major rewrite of both server and client so replies could be associated with the relevant sent command, and as suggested moved receiving on the client to a separate slot connected to TcpSocket.ReadyRead() signal.

            This solved all time-out issues.

            Again many thanks for the help it was much appreciated ( and needed ).

            1 Reply Last reply Reply Quote 0
            • A
              andre last edited by

              Thank you for reporting back with your solution!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post