C ++:接收SIGKILL时如何关闭TCP套接字(服务器) [英] C++ : how to close a tcp socket (server) when receiving SIGKILL

查看:314
本文介绍了C ++:接收SIGKILL时如何关闭TCP套接字(服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在死之前关闭插座? 我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆