QTcpSocket Reading issue on slow network



  • 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


  • Moderators

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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.