[Qt-interest] QTcpSocket problems
Thiago Macieira
thiago.macieira at trolltech.com
Mon Aug 3 19:12:46 CEST 2009
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090803/4337e308/attachment.bin
More information about the Qt-interest-old
mailing list