<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[What is wrong with my QDataStream]]></title><description><![CDATA[<p dir="auto">Hello, I am writing piece of code for a basic TCP client based on the <a href="https://doc.qt.io/qt-6/qtnetwork-blockingfortuneclient-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">Blocking Fortune Client Example</a>.<br />
I adapted my code but I am facing an issue with this part of the code:</p>
<pre><code>do {
   if (!socket.waitForReadyRead(Timeout)) {
       emit error(socket.error(), socket.errorString());
       return;
    }

   in.startTransaction();
   in &gt;&gt; fortune;
} while (!in.commitTransaction());
</code></pre>
<p dir="auto">The problem is always get a timeout error even if the data are correctly sent by the server.</p>
<p dir="auto">I checked the satus of <em>in</em> with <em>in.status()</em> and I always get a <a href="https://doc.qt.io/qt-6/qdatastream.html#Status-enum" target="_blank" rel="noopener noreferrer nofollow ugc">QDataStream::ReadCorruptData</a>.</p>
<p dir="auto">I checked that data are here using</p>
<pre><code>const int len = 2048;
char *text = new char[len];
in.readRawData(text, len);
qDebug() &lt;&lt; text;
</code></pre>
<p dir="auto">Doing so, <em>text</em> contains data I expect to receive.</p>
<p dir="auto">So the question is why <em>in.commitTransaction()</em> is not true even if data are there? Or the question might be, how to use QDataStream?</p>
<p dir="auto">Thanks in advance.</p>
]]></description><link>https://forum.qt.io/topic/139640/what-is-wrong-with-my-qdatastream</link><generator>RSS for Node</generator><lastBuildDate>Wed, 11 Nov 2026 12:24:09 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139640.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 28 Sep 2022 08:09:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to What is wrong with my QDataStream on Wed, 28 Sep 2022 08:43:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/odelaune">@<bdi>odelaune</bdi></a><br />
I don't think you understand,  I am not asking you about your <em>receiving</em> code, I am asking about your <em>sending</em> code.  What Qt type is that <em>sending</em>?</p>
<p dir="auto">I have a feeling it is not sending any Qt type (like <code>QString</code> or <code>QByteArray</code>).  Rather it is just sending a stream of bytes/characters?  In which case, how do you expect a transaction to work at receiving side?  I suspect you are failing to understand that using <code>QDataStream</code>/types/transactions requires <em>both</em> sides to be using Qt and sending the same type as the receiver attempts to read?</p>
<blockquote>
<p dir="auto">As I am not sure what type of data the server sends (text or byte)</p>
</blockquote>
<p dir="auto">Then you can't use transactions.  You can only use the <code>readRawData()</code> you showed.  <a href="https://doc.qt.io/qt-6/qdatastream.html#reading-and-writing-raw-binary-data" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qdatastream.html#reading-and-writing-raw-binary-data</a></p>
]]></description><link>https://forum.qt.io/post/730492</link><guid isPermaLink="true">https://forum.qt.io/post/730492</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 28 Sep 2022 08:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to What is wrong with my QDataStream on Wed, 28 Sep 2022 08:57:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/odelaune">@<bdi>odelaune</bdi></a><br />
Yes, you will have to interpret received data yourself.</p>
<p dir="auto">Be aware that <code>waitForReadyRead()</code> will/may return as soon as at least one byte is received.  Not necessarily all bytes!  Consider changing your logic over not to use this "synchronous" call, but instead the <code>readyRead()</code> <em>signal</em>, which is called every time more bytes are available, no matter when they arrive.  Depending on your need, you may have to accumulate/buffer till you get all the bytes you are expecting, or work on bytes as they arrive knowing there may be more to come.</p>
]]></description><link>https://forum.qt.io/post/730498</link><guid isPermaLink="true">https://forum.qt.io/post/730498</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 28 Sep 2022 08:57:56 GMT</pubDate></item><item><title><![CDATA[Reply to What is wrong with my QDataStream on Wed, 28 Sep 2022 08:51:14 GMT]]></title><description><![CDATA[<p dir="auto">Indeed, you are write I cannot use QDataStream because the server does not use QDataStream to send data. So I just replaced the do/while loop by</p>
<pre><code>QByteArray data;
if (!socket.waitForReadyRead(Timeout)) {
    emit error(socket.error(), socket.errorString());
}
data = socket.readAll();
</code></pre>
<p dir="auto">And now it works fine.</p>
]]></description><link>https://forum.qt.io/post/730497</link><guid isPermaLink="true">https://forum.qt.io/post/730497</guid><dc:creator><![CDATA[odelaune]]></dc:creator><pubDate>Wed, 28 Sep 2022 08:51:14 GMT</pubDate></item><item><title><![CDATA[Reply to What is wrong with my QDataStream on Wed, 28 Sep 2022 08:43:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/odelaune">@<bdi>odelaune</bdi></a><br />
I don't think you understand,  I am not asking you about your <em>receiving</em> code, I am asking about your <em>sending</em> code.  What Qt type is that <em>sending</em>?</p>
<p dir="auto">I have a feeling it is not sending any Qt type (like <code>QString</code> or <code>QByteArray</code>).  Rather it is just sending a stream of bytes/characters?  In which case, how do you expect a transaction to work at receiving side?  I suspect you are failing to understand that using <code>QDataStream</code>/types/transactions requires <em>both</em> sides to be using Qt and sending the same type as the receiver attempts to read?</p>
<blockquote>
<p dir="auto">As I am not sure what type of data the server sends (text or byte)</p>
</blockquote>
<p dir="auto">Then you can't use transactions.  You can only use the <code>readRawData()</code> you showed.  <a href="https://doc.qt.io/qt-6/qdatastream.html#reading-and-writing-raw-binary-data" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qdatastream.html#reading-and-writing-raw-binary-data</a></p>
]]></description><link>https://forum.qt.io/post/730492</link><guid isPermaLink="true">https://forum.qt.io/post/730492</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 28 Sep 2022 08:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to What is wrong with my QDataStream on Wed, 28 Sep 2022 08:39:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/730484">What is wrong with my QDataStream</a>:</p>
<blockquote>
<p dir="auto">But is the writer sending a QString (assuming you have QString fortune), or just a stream of bytes/characters, which it sounds like to me?</p>
</blockquote>
<p dir="auto">Good point, <em>fortune</em> was declared as a QString. As I am not sure what type of data the server sends (text or byte), I switch the declaration of <em>fortune</em> to QByteArray.</p>
<p dir="auto">Now, the status has changed to <a href="https://doc.qt.io/qt-6/qdatastream.html#Status-enum" target="_blank" rel="noopener noreferrer nofollow ugc">QDataStream::ReadPastEnd</a> but my data are still empty.</p>
<p dir="auto">So to have the full view of the code, here is the current version</p>
<pre><code>QDataStream in(&amp;socket);
in.setVersion(QDataStream::Qt_4_0);
QByteArray data;
do {
    if (!socket.waitForReadyRead(Timeout)) {
        emit error(socket.error(), socket.errorString());
        return;
    }

    in.startTransaction();
    in &gt;&gt; data;
    qDebug() &lt;&lt; in.status();
} while (!in.commitTransaction());
</code></pre>
<p dir="auto">So something is still wrong.</p>
]]></description><link>https://forum.qt.io/post/730489</link><guid isPermaLink="true">https://forum.qt.io/post/730489</guid><dc:creator><![CDATA[odelaune]]></dc:creator><pubDate>Wed, 28 Sep 2022 08:39:40 GMT</pubDate></item><item><title><![CDATA[Reply to What is wrong with my QDataStream on Wed, 28 Sep 2022 08:18:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/odelaune">@<bdi>odelaune</bdi></a><br />
But is the writer sending a <code>QString</code> (assuming you have <code>QString fortune</code>), or just a stream of bytes/characters, which it sounds like to me?</p>
]]></description><link>https://forum.qt.io/post/730484</link><guid isPermaLink="true">https://forum.qt.io/post/730484</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 28 Sep 2022 08:18:03 GMT</pubDate></item></channel></rss>