title: Should I use file descriptors or streams for reading/writing to sockets
body: <p>After a socket has been setup, is it better practice to use read(2) and write(2) calls on the socket descriptor, or to associate a stream with the socket descriptor using fdopen(3) and then use stdio(3) functions?</p>
  
  <pre><code>int sfd = socket(PF_INET, SOCK_STREAM, 0);
  // setup the socket using sfd
  FILE * stream = fdopen(sfd, "r+");
  // use fprintf, fscanf, etc
  </code></pre>
  
  <p>EDIT: I also unbuffer the stream</p>
  
  <pre><code>setbuf(stream, NULL)
  </code></pre>
  
  <p>To avoid having to flush it as mentioned in the comments.</p>
  
  <p>I've been using this approach because it lets me reuse code written for FILE* streams, and I have the advantage of being able to use format strings (I am working with human readable text). GNU seems to be suggesting that this is a good idea.</p>
  
  <p><a href="http://www.gnu.org/software/libc/manual/html_node/Streams-and-File-Descriptors.html" rel="nofollow noreferrer">http://www.gnu.org/software/libc/manual/html_node/Streams-and-File-Descriptors.html</a></p>
  
  <p>However usually when I see code using sockets, the socket descriptor is used instead of a stream for all operations. Is there an advantage to using the lower level functions?</p>