Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you need to work directly with byte arrays returned by the socket functions.</p> <p>For these purposes it's good to have two distinct parts of a message in your protocol. The first part is a fixed-size "header". This will include the size of the byes that follow, the "payload", or, <code>data</code> in your example.</p> <p>So, to borrow some of your snippets and expand on them, maybe you'll have something like this:</p> <blockquote><pre> typedef struct { unsigned int sender_id; unsigned int sequence_number; <b>unsigned int data_length;</b> // this is new } PacketHeader;</pre></blockquote> <p>So then when you get a buffer in, you'll treat it as a <code>PacketHeader*</code>, and check <code>data_length</code> to know how much bytes will appear in the byte vector that follows.</p> <p>I would also add a few points...</p> <ul> <li><p>Making these fields <code>unsigned int</code> is not wise. The standards for C and C++ don't specify how big <code>int</code> is, and you want something that will be predictable on all compilers. I suggest the C99 type <code>uint32_t</code> defined in <code>&lt;stdint.h&gt;</code></p></li> <li><p>Note that when you get bytes from the socket... It is in no way guaranteed to be the same size as what the other end wrote to <code>send()</code> or <code>write()</code>. You might get incomplete messages ("packets" in your terminology), or you might get multiple ones in a single <code>read()</code> or <code>recv()</code> call. It's your responsibility to buffer these if they are short of a single request, or loop through them if you get multiple requests in the same pass.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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