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. [Solved] TcpSocket timeout HELP
QtWS25 Last Chance

[Solved] TcpSocket timeout HELP

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.2k 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.
  • N Offline
    N Offline
    nitramuk
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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
        0
        • N Offline
          N Offline
          nitramuk
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Thank you for reporting back with your solution!

            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