Note that there are some explanatory texts on larger screens.

plurals
  1. POQT socket read losing bytes
    primarykey
    data
    text
    <p>I'm trying to work with length-preceded TCP messages using Qt. I have following method:</p> <pre><code>QByteArray con::read() { QByteArray s; s = _pSocket-&gt;read(4); if (s.length() == 4) { int size = char_to_int32(s); s = _pSocket-&gt;read(size); } return s; } </code></pre> <p>Well, it does not work. Looks like I lose all data after reading first 4 bytes: the first read works fine, but <code>read(size)</code> returns nothing. Is there a way to solve this?</p> <p>The char_to_int32 is:</p> <pre><code>int char_to_int32(QByteArray s) { int size = 0; size |= (s.at(0) &lt;&lt; 24); size |= (s.at(1) &lt;&lt; 16); size |= (s.at(2) &lt;&lt; 8); size |= (s.at(3)); return size; } </code></pre> <p><strong>EDIT :</strong></p> <p>The sending function (plain C):</p> <pre><code>int send(int connfd, const unsigned char* message, unsigned int size) { int c; unsigned char* bytes = (unsigned char*) malloc(4 + size); int32_to_char(size, bytes); // converts message size to 4 bytes memcpy(bytes + 4, message, size); c = write(connfd, bytes, 4 + size); free(bytes); if (c &lt;= 0) return -1; else return 0; } </code></pre> <p>By the way, when I call _pSocket->readAll(), the entire packet is read, including 4-byte size and message itself.</p> <p><strong>EDIT :</strong></p> <pre><code>void int32_to_char(uint32_t in, char* bytes) { bytes[0] = (in &gt;&gt; 24) &amp; 0xFF; bytes[1] = (in &gt;&gt; 16) &amp; 0xFF; bytes[2] = (in &gt;&gt; 8) &amp; 0xFF; bytes[3] = in &amp; 0xFF; return; } </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.
 

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