这是我的客户端代码片段:
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);
首先它接收一个文件,然后监听 2 个服务器的消息。
这里是服务器的片段:
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);
}
问题是已被 RETRived 的文件还包含服务器发送的 2 条消息 Oo
为什么会发生这种情况?我已经对客户说“recv until the file size”...我不明白为什么该文件还包含 2 条消息-.-''