tags:

views:

122

answers:

4

Hi there, 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!

+4  A: 

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

#include <signal.h>
signal(SIGPIPE, SIG_IGN);
Greg Hewgill
A: 

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...

Macroideal
Right, what you describe as "skipped out" is where your process receives a SIGPIPE from the system.
Greg Hewgill
You can control whether you get a signal with MSG_NOSIGNAL in place of the 0 in the send(). You still get error EPIPE back (though if you don't have a SIGPIPE handler installed, the send() doesn't return).
Jonathan Leffler
A: 

Thanks, guys. The SIGNAL is exactly the error. I'm pretty new with it though. Could you explain more on this & show me a good tutorial on this stuff.

Actually, Wikipedia turns out to be as good as anything here: http://en.wikipedia.org/wiki/Signal_%28computing%29
Greg Hewgill
A: 

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

Adil