Reputation: 1779
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 -.-''
Upvotes: 0
Views: 135
Reputation: 27542
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.
Upvotes: 1