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 Reading issue on slow network
QtWS25 Last Chance

QTcpSocket Reading issue on slow network

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.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.
  • L Offline
    L Offline
    lloydqt
    wrote on last edited by
    #1

    Hi

    I am using QTcpSocket to connect and read data from a server. The server response is slow. I have tried using QIODevice::readData() to read from the socket, but the requested data is not read always, it returns partial data. I have solved this with "waitForReadyRead()" call. Now I am using this socket as input to the "QXmlStreamReader()", and calling the "readNextStartElement()" to get the elements. When I debug the program in single step mode slowly it returns the elements, otherwise "atEnd()" returns true and no data could be read from the stream.

    Is there an assured way of getting data from the stream in this kind of scenario?

    As a solution I have written the following code fragment...

    @ while (1)
    {
    if(xmlstream.atEnd())
    {
    if(xmlstream.hasError())
    {
    if(xmlstream.error()==QXmlStreamReader::PrematureEndOfDocumentError)
    {
    mysocket.waitForReadyRead();
    }
    else
    {
    mbox->setText("data error");
    mbox->exec();
    }
    }
    else
    {
    break;
    }
    }
    if (xmlstream.readNextStartElement())
    {

    }
    }@

    The above code fragment is not working as expected. I am sure that the XML is well formatted, but the end of document (the server closes the connection), still "xmlstream.error()==QXmlStreamReader::PrematureEndOfDocumentError" returns true!! Thus the code runs in an infinite loop...

    How people usually handle this kind of problems?

    Thanks,
    Lloyd

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      You might consider reading the entire XML stream into a buffer, and then processing the XML once the transfer is complete.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lloydqt
        wrote on last edited by
        #3

        I thought of this solution but did not feel as a clean solution. The problem is, the streamed XML data could be very large. So I have read all this into a large temp buffer and process.

        I wonder, Qt seems to provide a solution but it is not working as expected. Is there any technical difficulty implementing it?

        Thanks,
        Lloyd

        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