close() 优于 shutdown() 的情况?
我是一个开源项目的开发人员,我一直遇到一些问题,服务器认为它已经完全应答了套接字(意味着它已经发送回复或关闭它以响应失败)并且客户端被困在轮询()。经过一些研究,我发现 close() 并不总是生成 POLLHUP 事件,但 shutdown(sock, 2) 会生成。
有鉴于此,我正在考虑在发生错误处理时添加 shutdown(sock,2) (除了 close() 调用之外)。有谁知道这会导致问题的一些原因?我是不是找错了树?我在想,如果服务器认为套接字已关闭,则客户端绝对不应该尝试使用该套接字进行任何其他操作,并且我想不出不添加此套接字的原因,但我还没有使用 tcp联系这么久,希望得到一些建议。
I am a developer on an open source project and I have been having some problems with the server thinking it has answered a socket completely (meaning it has either sent a reply or closed it's end in response to a failure) and the client being stuck in poll(). After some research, I found that close() doesn't always generate a POLLHUP event, but shutdown(sock, 2) does.
In light of that, I'm considering adding a shutdown(sock,2) in the event of error handling (in addition to the close() call). Does anyone know of some reasons that this would cause problems? Am I barking up the wrong tree? I'm thinking that if the server believes that the socket is closed, the client should definitely not attempt anything else with that socket, and I can't think of a reason not to add this, but I haven't been working with tcp connections for that long and would love some advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要弄清楚为什么
close
套接字不会导致它关闭
。最可能的原因是有另一个描述符访问同一端点。仅关闭
最后一个端点会导致隐式关闭
。您曾经
复制
文件描述符吗?您是否确保它在所有子进程中都是close
?如果套接字在fork
此进程之前位于父进程中,则父进程是否关闭了其副本?You need to figure out why
close
ing the socket isn't causing it toshutdown
. The most likely reason is that there is another descriptor that accesses the same endpoint. Onlyclose
ing the last endpoint causes an implicitshutdown
.Do you ever
dup
the file descriptor? Do you make sure it isclose
d in all child processes? If the socket was in a parent process before itfork
ed this process, did the parent close their copy?POLLHUP 不是测试关闭连接的正确方法。您应该测试文件描述符是否变得可读并随后返回零长度读取。这是文件结尾的定义。
POLLHUP
is not the right way to test for a closed connection. You should be testing for the file descriptor becoming readable and subsequently returning a zero-length read. This is the definition of end-of-file.