I try to develop a chat application in c. I use sockets and select()
.
But if i close the server before the client, the client have a message "Broken Pipe".
I used select(), but i didn't know how to avoid it?
Asked
Active
Viewed 2,129 times
4
-
possible duplicate of [How to prevent SIGPIPEs (or handle them properly)](http://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly) – Apr 25 '13 at 16:51
-
Don't just close the pipe, say goodbye nicely. So that the other end knows to close it too. – Hans Passant Apr 25 '13 at 17:00
2 Answers
5
You can disable the signal:
signal(SIGPIPE, SIG_IGN);
Though the chosen answer was to ignore signal process wide, there are other alternatives:
Using send function with MSG_NOSIGNAL:
send(con, buff_enviar+enviado, length-enviado, MSG_NOSIGNAL);
Disabling SIGPIPE on socket level (not available on all kernels):
int flag = 1;
setsockopt(con, SOL_SOCKET, SO_NOSIGPIPE, &flag, sizeof(flag));
Disabling SIGPIPE for caller thread (you can restore it after):
sigset_t set;
sigemptyset (&set);
sigaddset (&set, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &set, NULL);

Valeri Atamaniouk
- 5,125
- 2
- 16
- 18
0
Register a handler for the PIPE signal (and maybe ignore the signal).

Kerrek SB
- 464,522
- 92
- 875
- 1,084
-
-
@heruma: You can use `SIG_IGN` to ignore `SIGPIPE`. Just read a manual page or something.. Some APIs also support extra pipe options to disable delivering a `SIGPIPE` from the kernel. See http://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly – Apr 25 '13 at 16:50