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

前端 未结 2 1106
小鲜肉
小鲜肉 2020-12-23 21:28

I am using the select function for communication between my sockets. I have a while loop and I do -

    while(!done) {

    FD_ZERO(&read_flags);
    FD         


        
相关标签:
2条回答
  • 2020-12-23 22:14

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

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

    0 讨论(0)
  • 2020-12-23 22:29

    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.

    0 讨论(0)
提交回复
热议问题