Skip to main content
GameDev.net gamedev.net
🔒 Locked

select() Question(s).

Started by ChaosPhoenix Mar 17, 2005 at 3:00 PM 1 replies 1.3k views
Original Post
ChaosPhoenix
ChaosPhoenix
Using select() for the first time and I just have a question that I really havn't found an explanation for in my books or online. So, first, let me see if I have this correct. When using select, you set the FD masks for each socket and select() simply tells you when an event has happened. You then iterate through your clients(assuming this is server side) and use the FD_ISSET macro to find out what socket has what event and then deal with it appropriately. Rinse and repeat for as many events as you have. So, do the flags have to be reset each time you deal with an event? (i.e. setting them back to zero) or do you just have to set them up once and then forget about them? And do I have the general jist of the concept correct? Thanks.
cmptrgear
cmptrgear
You just need to set the flags once, then use FD_ISSET to check which one received data. Here is some code I have for a mutlicast tree project I did.

fd_set clients;                 // used by select() ...FD_ZERO(&clients);//code to set up a server socket is hereFD_SET(server_socket, &clients);  //add server_socket to listwhile(stuff is happening){   retval=select(type+1, &clients, 0, 0, 0);	   //Is another node trying to connect to our server socket?  If so, get    //the file descriptor for that socket, receive the data packet containing   //the nodes ID number and add the information to the neighbors table.   if(FD_ISSET(server_socket, &clients))   ...   //more code down here}
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
hplus0603
hplus0603
Select changes the fd_sets you pass into it. The only fds that are set when select() returns are the fds that had events.

What I do is create fd_sets that have all interesting sockets in them, and then copy that into a fresh fd_set each time I call select(). I also don't select() for writability, instead using non-blocking sockets and attempting sendto() whenever I enter select and there's something in the queue (with an appropriately short time-out in this case).
enum Bool { True, False, FileNotFound };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.