I cannot imagine an easy way for a signal handler to know that the current socket is being processed if you do not set any global state each time you perform a socket operation.
You can ignore SIGPIPE from the main. You do not define your own handler; instead, use SIG_IGN .
signal(SIGPIPE, SIG_IGN);
Or if you use sigaction :
struct sigaction act; act.sa_handler = SIG_IGN; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGPIPE, &act, NULL);
Alternatively, you can issue the MSG_NOSIGNAL flag when calling send. This will suppress the generation of SIGPIPE and instead generate an EPIPE error (what will happen if you ignore SIGPIPE ):
ssize_t sent = send(sock, buf, sizeof(buf), MSG_NOSIGNAL); if (sent > 0) { } else { assert(sent < 0); swtich (errno) { case EPIPE: } }
jxh
source share