简体   繁体   中英

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.

The solution which I have in mind is use timerfd_create and use timers on descriptors to monitor all the active sockets. 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 . 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.

I used the timerfd_create and timerfd_settime api's in the timerfd.h. Other option to detect the closed socket was keep_alive timeout in sysconf. keep_alive timeout wasnt much of a help in case the socket was alive but no activity so had to use the earlier option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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