Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:20:22+00:00 2026-05-30T05:20:22+00:00

So I totally lost it when someone showed me some code using select() like:

  • 0

So I totally lost it when someone showed me some code using select() like:

FD_ZERO(&readfds);
FD_SET(socket,&readfds);
FD_SET(0,&readfds);
.......// bind() and listen(),regular stuff
// Before select(),this guy cleared readfds to null and sort of start all over again
FD_ZERO(&readfds);// Behold,this is exactly the line confusing myself
FD_SET(socket,&readfds);
// select will be called in the future within a infinite loop 

So basically saying what FD_ZERO after bind and listen is clear previous contents for some reason and insert socket file descriptor to fd set again.Meanwhile,in fd set,the 0 descriptor(standard input) apparently disappeared.

Could anyone explain this whole thing? I don’t feel like this is correct but i cant really figure it out. Maybe setting up some specific fd set all over again is a practical mechanism…

I thought bind() and listen() could modify fd set in some sort of manner.I set up a couple of breaking points and examine the outputs.Consequently,fd set did not change at all.

UPDATE:

What really confuses me is the second FD_ZERO(&readfds).
Obviously the first one is like clearing rusty stuff in the buffer. On each iteration,we have to clear fdset to default for the future. I just dont understand the second one. Either it is a wrong one or it wont hurt at any rate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:20:24+00:00Added an answer on May 30, 2026 at 5:20 am

    Not sure where your confusion lies, but here’s an explanation.

    An fdset is a bitmask of file descriptors indexed from zero (technically, I don’t think it has to be a bitmask but any other implementation would be … strange).

    So an fdset may be something like:

                  Byte 0                              Byte 1
    +---+---+---+---+---+---+---+---+ +---+---+----+----+----+----+----+----+
    |fd0|fd1|fd2|fd3|fd4|fd5|fd6|fd7| |fd8|fd9|fd10|fd11|fd12|fd13|fd14|fd15|
    +---+---+---+---+---+---+---+---+ +---+---+----+----+----+----+----+----+
    

    The idea is that you set the bit for a given fd (file descriptor) if you’re interested in it. If you’re not interested in it, you leave the bit clear.

    FD_ZERO will clear all bits, which is a good starting point. Then the two FD_SET calls will set the bits for fd0 (standard input) and fd<socket>.

    Then, a select call using that fdset would return if either there was action on standard input or action on the socket (the type of action would depend on which parameter position you used when passing the fdset to select. Passing it as the read fdset means that you’ll return if either you can read the socket or something has shown up on standard input (such as you using the keyboard).

    This may well be the useful in the code for a “chat” application since it would wait on input from either you or your friend at the other end.

    The reason why you have to re-zero and re-set the bits in an fdset is because select itself modifies the set to indicate what fd (or fds) it detected the action on.

    Example: if something came in via the socket and nothing via standard input, the fdset would be modified by select to only have that bit set. That way you can use FD_ISSET to detect which fds you should look at.


    If, as you seem to indicate in comments, there are no select calls (or other calls using readfds) between the two FD_ZERO()/FD_SET() sequences then, yes, it appears that the first is unnecessary, since the information will be overwritten by the second.

    Without seeing the full code, or the architectural design documents 🙂 , it’s hard to tell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Totally lost with this one. Here's my code: theColor = [NSString stringWithFormat:@white]; NSLog(@%s, theColor);
I'm getting totally lost and confused on how to use the new strongly typed
Something in here is incompatible with PHP5, but I am totally lost as to
I am a totally blind programmer who would like to learn Python. Unfortunately the
I am totally new to SQL . I have a simple select query similar
I'm getting a totally bizzare error trying to compile a C program using GCC.
I'm a totally blind individual who would like to learn more of the theory
I'm totally lost on how to build Google's breakpad. There is a sln file,
I am totally lost in how I should do this. I'm trying to display
I received a requirement and I'm totally lost, I hope some1 can light me.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.