Why FD_SET/FD_ZERO for select() inside of loop?

我们两清 提交于 2019-11-29 22:20:24

When select returns, it has updated the sets to show which file descriptors have become ready for read/write/exception. All other flags have been cleared.

It's important that you re-enable the file descriptors that were cleared prior to starting another select, otherwise, you will no longer be waiting on those file descriptors.

As for re-clearing, it can be a good habit to get into, since if you need to change the set of file descriptors (such as adding a newly opened socket to the read set), you'll want to clear it and rebuilt it every time, so that it's correct as the state of the program changes.

Is it just because select modifies the contents of the set?

Yes, after select returns, only ready descriptors are left within the sets.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!