Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suspect what's happening is that <code>fgets()</code> is reading the NUL characters properly, but you're interpreting the first one as being the end of the line that was read. It's tricky because <code>fgets()</code> is intended for textual input, and it does use the '\0' as a sentinel, not returning the number of characters read. Even if you know each line will be <code>\n</code> terminated, there's the posibility that raw binary data embedded in a line will include a <code>\n</code> too. So, switch to <code>fread()</code>, which is meant for binary. You can put a fixed-length (e.g. 2-byte, 4-byte) message size on the front of each message so the other side can read that first then do a <code>fread()</code> for the exact message size, avoiding messy issues with partial message reads.</p> <p>If you <em>really</em> want to keep it some strange hybrid of text and binary, you could try using <code>ftell()</code> after <code>fgets()</code> to find out how much further along the stream you are, and hence how many characters should be in your buffer, but I've never seen a serious system do something so hacky.</p> <p>For the record, without doing something markedly inefficient like hex-encoding every single character in the binary data, you could just encode the troublesome character(s). For example, the C string literal escape sequences could be used, with <code>\0</code> representing a NUL and <code>\\</code> a single <code>\</code>. Though less easy to visually check, it may be easier programmatically to encode/decode non-printable characters using say octal <code>\NNN</code>. If there are a lot of non-printable characters, then a base-64 uuencode approach - such as used in MIME email attachments - is another suitable but totally non-readable encoding.</p>
    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