1

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!

3 Answers 3

4

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

#include <signal.h> signal(SIGPIPE, SIG_IGN); 
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Right, what you describe as "skipped out" is where your process receives a SIGPIPE from the system.
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).
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.