简体   繁体   English

监控套接字描述符

[英]Monitoring Socket descriptors

I have an application that has many socket descriptors open at a time.我有一个应用程序一次打开许多套接字描述符。 I want to monitor these just for any activity and close the descriptor which hasnt seen any activity for some time.I have some solutions but not sure if this can be done via firewall rule or not.我想仅针对任何活动监视这些并关闭一段时间没有看到任何活动的描述符。我有一些解决方案,但不确定这是否可以通过防火墙规则完成。 I dont want to waste my effort for something that can be achieved via some system service or configuration.我不想把我的精力浪费在可以通过一些系统服务或配置来实现的事情上。

It has to be done on linux based system.它必须在基于 linux 的系统上完成。

The solution which I have in mind is use timerfd_create and use timers on descriptors to monitor all the active sockets.我想到的解决方案是使用 timerfd_create 并在描述符上使用计时器来监视所有活动套接字。 Any suggestion for achieving this in a better way would be help ful.任何以更好的方式实现这一目标的建议都会有所帮助。 Thanks in advance.提前致谢。

use select() to monitor the sockets, there's an example at the end of the man page .使用select()监视套接字,手册末尾有一个示例。 Basically, you add the file descriptors to an fd_set and call select() it returns when one of the monitored file descriptors can be read or written, or if you set a timeout, you could then use select with a small timeout to check the fd's in a timer callback and close the ones that are not ready.基本上,您将文件描述符添加到fd_set并调用select()它在可以读取或写入监控的文件描述符之一时返回它,或者如果您设置超时,则可以使用带有小超时的 select 来检查 fd 的在计时器回调中并关闭那些没有准备好的。

I used the timerfd_create and timerfd_settime api's in the timerfd.h.我在 timerfd.h 中使用了 timerfd_create 和 timerfd_settime api。 Other option to detect the closed socket was keep_alive timeout in sysconf.检测关闭套接字的其他选项是 sysconf 中的 keep_alive 超时。 keep_alive timeout wasnt much of a help in case the socket was alive but no activity so had to use the earlier option. keep_alive 超时在套接字处于活动状态但没有活动的情况下没有多大帮助,因此必须使用较早的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM