[Qt-interest] QTcpSocket problems
Donal O'Connor
donaloconnor at gmail.com
Mon Aug 3 19:19:35 CEST 2009
Hi Thiago,
Apologies for the code insertion.
Thanks for the explanation. That makes sense. I've figured it out, to keep
calling waitForReadyRead(). I'm guessing it is up to myself to Know when to
stop calling waitForReadyRead, as in know when the end of the stream
arrives.
Thanks,
Regards,
Donal
On Mon, Aug 3, 2009 at 6:12 PM, Thiago Macieira <
thiago.macieira at trolltech.com> wrote:
> Em Segunda-feira 03 Agosto 2009, às 18:19:19, Donal O'Connor escreveu:
> > This is my reading code after I write the request:
>
> Please use an mail user agent that allows you to paste indented code and
> doesn't insert spurious line breaks. It's quite hard to read what you
> pasted:
>
> > if (m_tcpSocket->waitForReadyRead(5000))
> >
> > {
> >
> > QByteArray inputData;
> >
> > while(true)
> >
> > {
> >
> > qDebug() << "Remaining bytes: " << m_tcpSocket->bytesAvailable();
> >
> > QByteArray thisBuffer;
> >
> > thisBuffer = m_tcpSocket->read(m_tcpSocket->bytesAvailable());
> >
> > if (thisBuffer.size() == 0)
> >
> > break;
> >
> > else
> >
> > inputData += thisBuffer;
> >
> > }
> >
> > QString returnedString = QString(inputData).trimmed();
> >
> > returnLines = returnedString.split("|");
> >
> > }
> >
> > else
> >
> > qWarning() << "Error reading response";
> >
> > I only get 1460 bytes (the packet size on this network). I've used
> > Wireshark, and I can see that the Java server is sending out all the
> packet
> > data but I only get one packet.
> > 0 Bytes are available after the first read occurence.
>
> That's because you only called waitForReadyRead once.
>
> QTcpSocket reads from the network only in two circumstances, neither of
> which
> are present inside your while-loop:
> 1) event loop
> 2) waitForReadyRead
>
> If you want to use QTcpSocket in blocking mode (i.e., without the event
> loop),
> you have to continuously call waitForReadyRead and waitForBytesWritten.
> Also
> note that waitForBytesWritten waits for any amount of bytes written -- not
> *all* bytes. So after it returns, there may be bytes still to be written.
>
> However, even in blocking mode, QTcpSocket does writes while doing
> waitForReadyRead and reads while doing waitForBytesWritten, so as to avoid
> network deadlocks caused by one side waiting for a response from the other
> side, without having sent the entire command yet. That's a very common case
> in
> protocols which send lots of data, like SMTP and IMAP4.
>
> --
> Thiago Macieira - thiago.macieira (AT) nokia.com
> Senior Product Manager - Nokia, Qt Software
> Sandakerveien 116, NO-0402 Oslo, Norway
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090803/19dff35c/attachment.html
More information about the Qt-interest-old
mailing list