2009-11-05 57 views
1

我實現了一個客戶端 - 服務器程序,允許它們傳輸文件。服務器正在使用select()來檢查套接字的更改。 除了這一個,每個測試都很好: - 當服務器發送一個巨大的文件到客戶端(尚未完成)時,客戶端點擊「Ctrl-C」殺死客戶端程序,然後服務器也被殺死:(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$ 

什麼地方錯了, 謝謝您的回答

回答

4

您可能想要忽略SIGPIPE。嘗試在服務器啓動時加入這樣的事情:

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

send()調用可用於只有當插座處於connected狀態(So預期的接收者是已知的)。回報是發送bytescount ... if(send(sockClient, sendBuf, chunk_length, 0) < 0) 所以在斷開連接時,它跳過了......

+0

正確的,你所描述的「跳出」是你的進程從系統接收SIGPIPE的地方。 – 2009-11-05 03:35:48

+1

您可以控制是否使用MSG_NOSIGNAL代替send()中的0。您仍然收到錯誤EPIPE(儘管如果您沒有安裝SIGPIPE處理程序,則send()不會返回)。 – 2009-11-05 05:33:06

0

MSG_NOSIGNAL是不可移植的,並不會在Windows上使用。