0

This is a snippet of my client code:

while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0)){
    if(write(fd, filebuffer, nread) < 0){
        perror("write");
        close(sockd);
        exit(1);
    }
    total_bytes_read += nread;
}
memset(buffer, 0, sizeof(buffer));
if(recv(sockd, buffer, sizeof(buffer), 0) < 0){
    perror("Errore ricezione 226");
    close(sockd);
    exit(1);
}
printf("%s", buffer);
memset(buffer, 0, sizeof(buffer));
if(recv(sockd, buffer, sizeof(buffer), 0) < 0){
    perror("Errore ricezione 221");
    close(sockd);
    exit(1);
}
printf("%s", buffer);
close(fd);

where first it receives a file and than it listen for the 2 server's messages.
Here the snippet of the server:

offset = 0;
rc = sendfile(newsockd, fd, &offset, fileStat.st_size);
if(rc == -1) {
        fprintf(stderr, "Errore durante l'invio di: '%s'\n", strerror(errno));
        onexit(newsockd, sockd, fd, 3);
}
if((uint32_t)rc != fsize) {
    fprintf(stderr, "Trasferimento incompleto: %d di %d bytes inviati\n", rc, (int)fileStat.st_size);
    onexit(newsockd, sockd, fd, 3);
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "226 File trasferito con successo\n");
if(send(newsockd, buffer, strlen(buffer), 0) < 0){
  perror("Errore durante l'invio 226");
  onexit(newsockd, sockd, 0, 2);
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "221 Goodbye\n");
if(send(newsockd, buffer, strlen(buffer), 0) < 0){
  perror("Errore durante l'invio 221");
  onexit(newsockd, sockd, 0, 2);
}

The problem is that the file that have been RETRived contains also the 2 messages that were sent by the server O.o
Why it happens? I've said to the client "recv until the file size"...i don't understand why the file contains also the 2 messages -.-''

polslinux
  • 1,739
  • 9
  • 34
  • 73

1 Answers1

1

while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0))

If you happen to read ONE more byte than fsize the statement is still true and you won't break out of the loop.

Duck
  • 26,924
  • 5
  • 64
  • 92
  • <=, write what's left of the file, then start doing whatever is appropriate for the 2 messages since you likely will have some bytes of that already. – Duck Jul 07 '12 at 14:35
  • with `<` and also with `<=` i got a strange behaviour: file is retrived but it is empty and its content is wrote to stdout :O – polslinux Jul 07 '12 at 14:38
  • Based on what is here I don't understand the stdout part at all but if your file is small it is possible coded it wrong and you fall out of the read loop before you write anything and immediately zero out the buffer. – Duck Jul 07 '12 at 14:45
  • It print the file to screen instead of save it O.o but why it happens only if i put `<=`?? – polslinux Jul 07 '12 at 15:03
  • I'm sorry I don't see any explanation for that stdout behavior based on what you have posted and that one small change. You understand that change isn't in and of itself sufficient to do what you want, right? You still have other coding changes to make to separate the file content from the messages. – Duck Jul 07 '12 at 16:18