2012-08-08 108 views
1

這是列出FTP的CMD的一個片段:Ç的sendfile不工作的第二次

count = file_list("./", &files); 
if((fp_list = fopen("listfiles.txt", "w")) == NULL){ 
    perror("Impossibile aprire il file per la scrittura LIST"); 
    onexit(newsockd, sockd, 0, 2); 
} 
for(i=0; i < count; i++){ 
    if(strcmp(files[i], "DIR ..") == 0 || strcmp(files[i], "DIR .") == 0) continue; 
    else{ 
    fprintf(fp_list, "%s\n", files[i]); 
    } 
} 
fclose(fp_list); 
if((fpl = open("listfiles.txt", O_RDONLY)) < 0){ 
    perror("open file with open"); 
    onexit(newsockd, sockd, 0, 2); 
    exit(1); 
} 
if(fstat(fpl, &fileStat) < 0){ 
    perror("Errore fstat"); 
    onexit(newsockd, sockd, fpl, 3); 
} 
fsize = fileStat.st_size; 
if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){ 
    perror("Errore durante l'invio grande file list"); 
    onexit(newsockd, sockd, fpl, 3); 
} 
rc_list = sendfile(newsockd, fpl, &offset_list, fileStat.st_size); 
if(rc_list == -1){ 
    perror("Invio file list non riuscito"); 
    onexit(newsockd, sockd, fpl, 3); 
} 
if((uint32_t)rc_list != fsize){ 
    fprintf(stderr, "Error: transfer incomplete: %d di %d bytes inviati\n", rc_list, (int)fileStat.st_size); 
    onexit(newsockd, sockd, fpl, 3); 
} 
printf("OK\n"); 
close(fpl); 
if(remove("listfiles.txt") == -1){ 
    perror("errore cancellazione file"); 
    onexit(newsockd, sockd, 0, 2); 
} 

wher &files被聲明爲char **files和功能list_files是我寫的一個功能不相關爲我的問題。
我的問題:首次LIST CMD這就是所謂的它工作正常,但如果我叫列表中的其他時間也總是給我「的錯誤,不完備轉移」我不明白爲什麼...

+1

從您在問題中發佈的代碼中看到「錯誤,轉移不完整」消息嗎?在那種情況下呢?否則,問題報告中的代碼是什麼? – 2012-08-08 08:02:31

+0

是的,這是我發佈到問題:)當sendfile函數不發送所有文件時,它給我一個錯誤... – polslinux 2012-08-08 08:38:56

+0

什麼是'offset_list'和你如何初始化它?請記住,如果'sendfile'不能發送所有內容,那麼它將返回一個大於請求大小的值,並且您將不得不調整大小並再次嘗試。 – 2012-08-08 08:45:58

回答

3

sendfile函數可能不會在一次調用中發送所有數據,在這種情況下,它將返回比請求數量少。您將此視爲錯誤,但您應該再試一次。一種方法是使用類似於此的循環:

off_t offset = 0; 
for (size_t size_to_send = fsize; size_to_send > 0;) 
{ 
    ssize_t sent = sendfile(newsockd, fpl, &offset, size_to_send); 
    if (sent <= 0) 
    { 
     if (sent != 0) 
      perror("sendfile"); 
     break; 
    } 

    offset += sent; 
    size_to_send -= sent; 
} 
+0

哦謝謝:)我已經張貼我的答案,沒有看到你的!然而,我發現我的問題,但你的解決方案更好:) – polslinux 2012-08-08 09:02:54

+0

但如果'發送'是'<= 0'我必須退出並關閉套接字...爲什麼你只做一個休息? – polslinux 2012-08-08 09:23:05

+2

@polslinux你可以在代碼中做任何你想做的事情。 'break'只是一個例子。 – 2012-08-08 09:24:46

2

我發現我的問題。
當我多次呼叫sendfile變量off_t offset_list;仍然「髒」
如果第一次調用sendfile offest_list將有一個值,不會第二次我刪除了這個函數。
所以如果我必須在rc_list = sendfile(newsockd, fpl, &offset_list, fileStat.st_size);之前編寫offset_list = 0;並且一切正常!

+0

它也解決了我的問題!我整整呆了一整天!我不能說我現在有多快樂。謝謝 – UrbiJr 2017-01-15 16:16:49

相關問題