4

我尝试用 c 开发一个聊天应用程序。我使用套接字和select(). 但是如果我在客户端之前关闭服务器,客户端会收到一条消息“Broken Pipe”。我使用了 select(),但我不知道如何避免它?

4

2 回答 2

4

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);
于 2013-04-25T16:23:55.953 回答
0

为 PIPE 信号注册一个处理程序(并且可能忽略该信号)。

于 2013-04-25T16:22:34.950 回答