Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C sendfile,然后是其他文本_C_Sockets_Sendfile - Fatal编程技术网

C sendfile,然后是其他文本

C sendfile,然后是其他文本,c,sockets,sendfile,C,Sockets,Sendfile,这是我的客户机代码片段: 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); }

这是我的客户机代码片段:

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);
while(((uint32_t)total_bytes_read!=fsize)&((nread=read(sockd,filebuffer,sizeof(filebuffer)))>0)){
if(写入(fd、filebuffer、nread)<0){
佩罗(“书面”);
关闭(sockd);
出口(1);
}
总字节数读取+=nread;
}
memset(buffer,0,sizeof(buffer));
if(recv(sockd,buffer,sizeof(buffer),0)<0){
佩罗尔(Errore ricezione 226);
关闭(sockd);
出口(1);
}
printf(“%s”,缓冲区);
memset(buffer,0,sizeof(buffer));
if(recv(sockd,buffer,sizeof(buffer),0)<0){
佩罗尔(Errore ricezione 221);
关闭(sockd);
出口(1);
}
printf(“%s”,缓冲区);
关闭(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);
}
offset=0;
rc=sendfile(newsockd、fd和offset、fileStat.st_size);
如果(rc==-1){
fprintf(标准,“因维奥的错误:'%s'\n',strerror(errno));
onexit(newsockd、sockd、fd、3);
}
如果((uint32_t)rc!=fsize){
fprintf(stderr,“传输不完整:%d di%d字节invati\n”,rc,(int)fileStat.st_size);
onexit(newsockd、sockd、fd、3);
}
memset(buffer,0,sizeof(buffer));
strcpy(缓冲区,“226文件传输成功\n”);
如果(发送(新闻组、缓冲区、strlen(缓冲区)、0)<0){
perror(“Errore durante l'invio 226”);
onexit(newsockd,sockd,0,2);
}
memset(buffer,0,sizeof(buffer));
strcpy(缓冲区,“221\n”);
如果(发送(新闻组、缓冲区、strlen(缓冲区)、0)<0){
perror(“Errore durante l'invio 221”);
onexit(newsockd,sockd,0,2);
}
问题是,已检索的文件还包含服务器O.O发送的2条消息
为什么会这样?我已经对客户说了“在文件大小之前记录”…我不明白为什么文件中也包含这两条消息-。-“”

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


如果您碰巧读取的字节比fsize多一个字节,则该语句仍然为true,并且不会中断循环

根据这里的内容使用
时,我根本不理解标准部分,但是如果您的文件很小,则可能编码错误,在写入任何内容之前,您脱离了读取循环,并立即将缓冲区归零。它将文件打印到屏幕上,而不是将其保存为O.O,但为什么只有在我放入
时才会发生这种情况,很抱歉,我没有看到任何错误基于您发布的内容和一个小改动,对stdout行为进行解释。你知道改变本身并不足以满足你的需求,对吧?您还需要进行其他编码更改,以将文件内容与消息分开。