user397232
user397232

Reputation: 1790

C file transfer question

I implemented a client-server program that allows to transfer files b/w them. The server is using select() to check changes of sockets. Every test is good except this one: - When server is sending a huge file to client (not yet finished), client hit "Ctrl-C" to kill the client program, then the server is killed too :(

The snippet:

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");

When the client is killed, the server terminal displays:

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

What's wrong with it? Thanks for your answers!

Upvotes: 1

Views: 279

Answers (3)

Adil
Adil

Reputation: 2538

MSG_NOSIGNAL is not portable and will not be available on Windows.

Upvotes: 0

StevenWang
StevenWang

Reputation: 3836

The send() call may be used only when the socket is in a connected state (so that the intended recipient is known). the return is the bytescount sent... if(send(sockClient, sendBuf, chunk_length, 0) < 0) so when disconnected, it skipped out...

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 994787

You probably want to ignore SIGPIPE. Try adding something like this in your server startup:

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

Upvotes: 4

Related Questions