Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Non-blocking writes don't work how you're expecting them to.</p> <p>If the write can't be completed immediately, write() returns to your code - but it <strong>doesn't</strong> keep trying to send the data in the background. All it does is say "I couldn't write at the moment - try again later". It does this by returning -1, with <em>errno</em> set to EAGAIN.</p> <p>Also, remember that when successful, write() returns the <em>number of bytes successfully written</em>. So if you're getting a return value of 1 when you asked for 2 bytes to be written, that means it was only partially successful, and you need to call write() again some time for the second byte.</p> <p>Basically, if you're using non-blocking IO, you want your txbytes() function to call write() in a loop until either your buffer is empty, or it returns -1. If it returned -1 you need to check errno - if it's EAGAIN, you'll have to call write() again some other time; anything else is probably a real error. Something like this:</p> <pre><code>ssize_t written = 0; while (oBufLength &gt; 0 &amp;&amp; written &gt; -1) { size_t i; printf("about to transmit %d bytes: ", oBufLength); for(i = 0; i &lt; oBufLength; i++) { printf(" %u",oBuf[i]); } printf("\n"); written = write(BAMid, oBuf, oBufLength); printf("Write returned %d\n", written); if (written &gt; 0) { /* The first "written" number of bytes in the buffer have now been sent, so * we discard them and move up the remaining bytes (if any) to the start of * the buffer */ oBufLength -= written; memmove(oBuf, oBuf + written, oBufLength); printf("Now have %d bytes left to send.\n", oBufLength); } } if (written &gt; -1 || errno == EAGAIN) { /* No fatal errors... */ return 0; } else /* error left in errno for caller to inspect */ return -1; } </code></pre> <p>Note that there's no need to duplicate the buffer - since write() doesn't do anything in parallel with your code. If it says its written the bytes, it doesn't need your buffer any more. Hope that helps!</p>
 

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