title: c send and receive file
body: <p>This is the server (sendfile) part:</p>
  
  <pre><code>offset = 0;
  for (size_to_send = fsize; size_to_send &gt; 0; ){
    rc = sendfile(newsockd, fd, &amp;offset, size_to_send);
    if (rc &lt;= 0){
      perror("sendfile");
      onexit(newsockd, sockd, fd, 3);
    }
    offset += rc;
    size_to_send -= rc;
  }
  close(fd); /* la chiusura del file va qui altrimenti rischio loop infinito e scrittura all'interno del file */
  
  memset(buffer, 0, sizeof(buffer));
  strcpy(buffer, "226 File Successfully transfered\n");
  if(send(newsockd, buffer, strlen(buffer), 0) &lt; 0){
    perror("Errore durante l'invio 226");
    onexit(newsockd, sockd, 0, 2);
  }
  memset(buffer, 0, sizeof(buffer));
  </code></pre>
  
  <p>and this is the part of the client (recv file) part:</p>
  
  <pre><code>    fsize_tmp = fsize;
      sInfo.filebuffer = malloc(fsize);
    if(sInfo.filebuffer == NULL){
      perror("malloc");
      onexit(sockd, 0, fd, 4);
    }
  
    while(((uint32_t)total_bytes_read != fsize) &amp;&amp; ((nread = read(sockd, sInfo.filebuffer, fsize_tmp)) &gt; 0)){
      if(write(fd, sInfo.filebuffer, nread) != nread){
              perror("write RETR");
              onexit(sockd, 0, 0, 1);
          }
          total_bytes_read += nread;
          fsize_tmp -= nread;
      }
    close(fd); /* la chiusura del file va qui altrimenti client entra in loop infinito e si scrive all'interno del file */
  
      memset(buffer, 0, sizeof(buffer));
      if(recv(sockd, buffer, 34, 0) &lt; 0){
      perror("Errore ricezione 226");
      onexit(sockd, 0, 0, 1);
    }
    printf("%s", buffer);
    memset(buffer, 0, sizeof(buffer));
    memset(dirpath, 0, sizeof(dirpath));
    free(sInfo.filebuffer);
  </code></pre>
  
  <p>The problem is that the string <em>"226 File etc etc"</em> is written <strong>inside</strong> the file that has been sent.<br>
  I've tried to do a small debug and so i've added a <code>printf</code> after the for loop (server sendfile) and a <code>printf</code> after the while loop (client) and i've noticed that the file is sent but on the client it doesn't exit from the while because the <code>printf</code> isn't printed...<br>
  Why i got this strange behaviour??<br>br></p>
  
  <p><strong>EDIT:</strong><br>
  The server send the file size to the client whit this code:<br></p>
  
  <pre><code>  fd = open(filename, O_RDONLY);
      if(fd &lt; 0){
      error!!
      }
  
      if(fstat(fd, &amp;fileStat) &lt; 0){
          perror("Errore fstat");
          onexit(newsockd, sockd, fd, 3);
      }
      fsize = fileStat.st_size;
      if(send(newsockd, &amp;fsize, sizeof(fsize), 0) &lt; 0){
        perror("Errore durante l'invio della grandezza del file\n");
        onexit(newsockd, sockd, fd, 3);
       }
  </code></pre>
  
  <p>the client receives the fsize from the server with this code:<br></p>
  
  <pre><code>if(read(sockd, &amp;fsize, sizeof(fsize)) &lt; 0){
      perror("Errore durante ricezione grandezza file\n");
      onexit(sockd, 0 ,0 ,1);
  }
  fd = open(sInfo.filename, O_CREAT | O_WRONLY, 0644);
  if (fd  &lt; 0) {
      perror("open");
      onexit(sockd, 0 ,0 ,1);
  }
  fsize_tmp = fsize;
  </code></pre>
  
  <p>both <code>fsize</code> are declared as <code>uint32_t</code>...</p>