title: How many bytes should I read/write to a socket?
body: <p>I'm having some doubts about the number of bytes I should write/read through a socket in C on Unix. I'm used to sending 1024 bytes, but this is really too much sometimes when I send short strings.</p>
  
  <p>I read a string from a file, and I don't know how many bytes this string is, it can vary every time, it can be 10, 20 or 1000. I only know for sure that it's &lt; 1024. So, when I write the code, I don't know the size of bytes to read on the client side, (on the server I can use <code>strlen()</code>). So, is the only solution to always read a maximum number of bytes (1024 in this case), regardless of the length of the string I read from the file?</p>
  
  <p>For instance, with this code:</p>
  
  <pre><code>read(socket,stringBuff,SIZE);
  </code></pre>
  
  <p>wouldn't it be better if <code>SIZE</code> is 10 instead of 1024 if I want to read a 10 byte string?</p>