如何知道远程端是否使用send系统调用关闭了套接字?
即使远程端关闭了连接,send() 系统调用第一次也会成功。 如果我再次发送,则会生成 sigpipe。 我不想使用recv系统调用来知道远程端是否关闭了连接。
那么有人可以告诉我如何仅使用发送系统调用来检测远程端是否关闭了连接?
the send() system call succeeds for the first time even if the remote end closed the connection.
If i send again, then sigpipe will be generated.
I don't want to use recv system call to know whether remote end closed the connection.
So can someone tell me how can i detect whether the remote end closed the connection using only send system call ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该做的第一件事是调用
signal(SIGPIPE, SIG_IGN);
以避免在send()
上接收SIGPIPE
,或者在 Linux 上,通过MSG_NOSIGNAL
标记为send()
。完成此操作后,当对等方断开连接时,send()
将不会生成SIGPIPE
信号,而是返回 -1 且errno == EPIPE
。发送
SIGPIPE
是write()
函数的历史行为。 http://pubs.opengroup.org/onlinepubs/009604599/functions/write.html最初它是为 shell 管道创建的(如
cmd | other_cmd
)。当在 shell 管道执行期间按下 ctrl-c 时,只有管道中的最后一个进程收到SIGINT
并终止。要终止管道中的其他进程,SIGPIPE
会被发送到尝试write()
写入STDOUT
且其读取器已终止的进程。The first thing you should do is call
signal(SIGPIPE, SIG_IGN);
to avoid receivingSIGPIPE
onsend()
or, on Linux, passMSG_NOSIGNAL
flag tosend()
. Having done that,send()
will not generateSIGPIPE
signal when a peer has disconnected, but rather return -1 witherrno == EPIPE
.Sending
SIGPIPE
is historical behaviour ofwrite()
function. http://pubs.opengroup.org/onlinepubs/009604599/functions/write.htmlOriginally it was created for shell pipes (like
cmd | other_cmd
). Whenctrl-c
is pressed during shell pipe execution only the last process in the pipe receivesSIGINT
and terminates. To terminate other processes in the pipeSIGPIPE
is sent to the process trying towrite()
intoSTDOUT
whose reader has already terminated.