簡體   English   中英

C文件傳輸問題

[英]C file transfer question

我實現了一個客戶端服務器程序,允許傳輸文件。 服務器使用select()來檢查套接字的更改。 每個測試都很好,除了這一個: - 當服務器向客戶端發送一個巨大的文件(尚未完成)時,客戶端點擊“Ctrl-C”來殺死客戶端程序,然后服務器也被殺死:(

片段:

fprintf(stderr,"Reading done, sending ...\n");
if(send(sockClient, sendBuf, chunk_length, 0) < 0)
{
    printf("Failed to send through socket %d \n", sockClient);
    return -1;
}
fprintf(stderr,"Sending done\n");

當客戶端被殺死時,服務器終端顯示:

user$./server
Reading done, sending ...
Sending done
Reading done, sending ...
Sending done
Reading done, sending ...
Sending done
Reading done, sending ...
user$

它出什么問題了? 謝謝你的回答!

您可能想忽略SIGPIPE。 嘗試在服務器啟動時添加這樣的內容:

#include <signal.h>
signal(SIGPIPE, SIG_IGN);

僅當套接字處於connected狀態時才可以使用send()調用(以便知道預期的接收者)。 返回是發送的bytescount ... if(send(sockClient, sendBuf, chunk_length, 0) < 0)所以當斷開連接時,它會跳過...

MSG_NOSIGNAL不可移植,在Windows上不可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM