Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you are using the <a href="http://qt-project.org/doc/qt-4.8/qiodevice.html#read-2" rel="nofollow"><code>QByteArray QIODevice::read(qint64 maxSize)</code></a> function, you may not be detecting errors correctly:</p> <blockquote> <p>This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.</p> </blockquote> <p>Some things to try:</p> <ul> <li><p>Use the <a href="http://qt-project.org/doc/qt-4.8/qiodevice.html#read" rel="nofollow"><code>qint64 QIODevice::read(char* data, qint64 maxSize)</code></a> which reports errors:</p> <blockquote> <p>If an error occurs ... this function returns -1.</p> </blockquote></li> <li><p>Call <a href="http://qt-project.org/doc/qt-4.8/qiodevice.html#errorString" rel="nofollow"><code>QIODevice::errorString</code></a> and <a href="http://qt-project.org/doc/qt-4.8/qabstractsocket.html#error" rel="nofollow"><code>QAbstractSocket::error</code></a> to find out what is going wrong.</p> <ul> <li>For bonus points, listen to the <code>QAbstractSocket::error</code> error signal.</li> </ul></li> <li><p>If this is a new protocol you are creating, try using <a href="http://qt-project.org/doc/qt-4.8/qdatastream.html" rel="nofollow"><code>QDataStream</code></a> for serialization, this automatically handles length prefixs and is platform independent. Your <code>char_to_int32</code> will break if you mix platforms with different <a href="http://en.wikipedia.org/wiki/Endianness" rel="nofollow">endienness</a>, and may break between different OSs or compilers as <code>int</code> is not guaranteed to be 32 bits (it is defined as <a href="http://en.wikipedia.org/wiki/C_data_types" rel="nofollow">at least 16 bits</a>).</p> <ul> <li>If you can't use <code>QDataStream</code>, at least use the <a href="http://beej.us/guide/bgnet/output/html/multipage/htonsman.html" rel="nofollow">htons, ntohs ...</a> functions.</li> </ul></li> </ul> <p><strong>Edit</strong></p> <p>Here is some example code showing hton/ntoh usage. Note that <code>uint32_t</code> and not <code>int</code> is used as it's guaranteed to be 32 bits. I've also used <code>memcpy</code> rather than pointer casts in the encode/decode to prevent aliasing and alignment problems (I've just done a cast in the test function for brevity).</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;arpa/inet.h&gt; void encode(uint32_t in, char* out) { /* Host to Network long (32 bits) */ const uint32_t t = htonl(in); memcpy(out, &amp;t, sizeof(t)); } uint32_t decode(char* in) { uint32_t t; memcpy(&amp;t, in, sizeof(t)); /* Network to Host long (32 bits) */ return ntohl(t); } void test(uint32_t v) { char buffer[4]; printf("Host Input: %08x\n", v); encode(v, buffer); printf("Network: %08x\n", *((uint32_t*)buffer)); printf("Host Output: %08x\n\n", decode(buffer)); } int main(int argc, char** argv) { test(0); test(1); test(0x55); test(0x55000000); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload