title: Recv() not getting EOF?
body: <p>I'm trying to send over socket TCP using send() and recv().  For testing, I'm sending a smaller text file that is 38 bytes.  I receive the file using this:</p>
  
  <pre><code>char * b = (char *)malloc(chunk + 2); 
  while (size &gt; 0) {
      memset(b, 0, chunk);
      if (chunk &gt; size) {
          chunk = size;
      }
      if (size == total_size) {
          gettimeofday(&amp;tvB, NULL);
      }   
      bytes_read = recv(ptr-&gt;sock, b, chunk, MSG_WAITALL);
  
      if (bytes_read &lt; 0) {
          printf("ERROR: READ\n\n");
          break;
      }   
      if (bytes_read == 0) break;
  
          int err = fwrite(b, sizeof(char), bytes_read, fp);
          if (err &lt;= 0) {
              printf("ERROR: FWRITE\n\n");
              break;
          }   
          size -= bytes_read;
          printf("SIZE: %d bytes_read = %d CHUNK: %d\n\n", size, bytes_read, chunk);
          printf("TotalSize - size %d\n\n", total_size - size);
      }
      fclose(fp);
      gettimeofday(&amp;tvA, NULL);
  </code></pre>
  
  <p>And my sending side:</p>
  
  <pre><code> char * c = (char *)malloc(transfer_size + 2);
   while (bytes_to_send &gt; 0) {
       if (transfer_size &gt; bytes_to_send ) {
           transfer_size = bytes_to_send;
       }
       size_t t = fread(c, 1, transfer_size, fp);
       if (bytes_to_send == total_size) {
           gettimeofday(&amp;tvB, NULL);
       }
  
       bytes_sent = send(n-&gt;sock, c, transfer_size, 1);
       if (bytes_sent &lt;=0) {
           // error
           printf("SENDING ERROR\n\n");
           break;
       }
       bytes_to_send -= bytes_sent;
  
       pos = total_size - bytes_to_send;
       printf("bytes_sent: %d bytes_to_send: %d\n\n", bytes_sent, bytes_to_send);
  
       fseek(fp, pos, SEEK_SET);
       memset(c, 0, transfer_size * sizeof(char));
   }
  </code></pre>
  
  <p>My issue is that recv is </p>
  
  <p>A.) Not getting the last byte of all the files I send, and </p>
  
  <p>B.) It's not receiving the whole "chunk" every time, even though send says it has sent everything.</p>
  
  <p>Just for further info, here's the output of running a transfer of a small file.</p>
  
  <p>Receiving side:</p>
  
  <pre><code>Size: 38, Filename: hi.txt
  
  SIZE: 1 bytes_read = 37 CHUNK: 38
  
  TotalSize - size 37
  </code></pre>
  
  <p>Sending side:</p>
  
  <pre><code>bytes_sent: 38 bytes_to_send: 0
  
  Successfully sent hi.txt to dest
  </code></pre>