C ++:接收SIGKILL时如何关闭TCP套接字(服务器) [英] C++ : how to close a tcp socket (server) when receiving SIGKILL
问题描述
我怎么能在死之前关闭插座? 我在Linux上有一个创建TCP服务器的服务.有时我必须重新启动它,并且我希望套接字确实关闭.此刻挂起. 我也知道SIGKILL无法处理. 谁能指出我正确的方向?
How can I close a socket just before die? I have a service on linux that create a TCP server. Sometimes I have to restart it and I want that the socket is really closed. At the moment it hangs. I also know that SIGKILL can't be handled. Can anyone point me in the right direction?
推荐答案
The socket will be closed - the OS will automatically do this, see: Using SO_REUSEADDR - What happens to previously open socket?
代替尝试处理无效的SIGKILL,而是在开始时解决问题:
Instead of trying to handle SIGKILL, which won't work, solve the problem at start:
使用SO_REUSEADDR
套接字选项,该选项可让您立即重用端口.
Use the SO_REUSEADDR
socket option which allows you to reuse the port immediately.
这篇关于C ++:接收SIGKILL时如何关闭TCP套接字(服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!