This is a working snippet of a while loop:
while(total_bytes_read != fsize){
while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
if(write(fd, filebuffer, nread) < 0){
perror("write");
close(sockd);
exit(1);
}
total_bytes_read += nread;
if(total_bytes_read == fsize) break;
}
}
This is an example of a NON working snippet of a while loop:
while(total_bytes_read != fsize){
while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
if(write(fd, filebuffer, nread) < 0){
perror("write");
close(sockd);
exit(1);
}
total_bytes_read += nread;
}
}
And also this, is an example of a NON working snippet of a while loop:
while(total_bytes_read < fsize){
while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
if(write(fd, filebuffer, nread) < 0){
perror("write");
close(sockd);
exit(1);
}
}
total_bytes_read += nread;
}
I would like to know why into the 2 snippet above when total_bytes_read is equal to fsize the loop won't exit :O
Thanks in advance!
read(2)from sockets doesn't return0unless the other party has issued closed the connection in a proper way.