I’m using epoll to manage about 20 to 30 sockets. I figure out that epoll_wait can be used to wait for some data to arrive over one of the socket but I’m missing how do I implement timeouts on socket level. I can use timeout on epoll_wait but it not very useful in my case. For example, if I need to every close a socket where no activity is recorded for > 500 ms orr may be send some data to a socket every 200 ms no matter what. How can these socket level timeout be implemented using epoll? Any suggestion and idea would be appreciated!
Thanks,
Shivam Kalra
Sounds like you’re trying to write an event loop (if so have a look at libev btw).
epollwill not help you there, you have to keep track of socket inactivity yourself (clock_gettime()orgettimeofday()for instance), then wake up several times a second and check everything you need.Some pseudo code
This will wake you up 200 times a second if all sockets are inactive.
The inactivity check requires a list of the sockets to be examined along with timestamps of the last inactivity:
where
diff()gives you the difference of 2struct timevals andnow()gives you the current timestamp.