Thread: FD_ZERO and FD_SET.. i still don't get it

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    FD_ZERO and FD_SET.. i still don't get it

    ok i know that select picks the sockets that are set, but do the sockets set themselves when they have information to be received?

    i was told that in the begining of the loop to set all the sockets, but that doesn't really make much sense to me. i want to have a loop and it pick out sockets that have information to be received in it. can anyone tell me if i need to set the sockets before hand or if they become set when they have information. i also need to know if when i process the socket if i should FD_CLEAR it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like
    Code:
    fd_set socketSet, selectSet;
    // socketSet is the permanent list of sockets you want to monitor.
    // having a separate variable saves a lot of FD_SET calls.
    
    // just before calling select, do
    selectSet = socketSet;
    int rval = select ( maxFd+1, &selectSet, NULL, NULL, NULL );
    
    // if rval > 0, then use FD_ISSET on the selectSet to see which particular sockets have something
    // available for reading.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    ok well that helps some but i still don't get the whole picture. when i accept a socket and i want to add it to the list what should i do? just FD_SET(sock, list_to_add_to)? whenever a message is sent to that socket is it FD_SET? how does select help any if if doesn't get set when it has something in it?

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The bits you FD_SET in a set on input to select() will be those things you want to watch.
    When select returns, the set is modified to indicate which fd's have changed, and these you test with FD_ISSET.

Popular pages Recent additions subscribe to a feed