Select() problems in C (windows 7)

1k views Asked by At

I am trying to create a server which uses select() to handle multiple clients, as opposed to multi-threading which I have already accomplished. However select() just doesn't seem to do anything? I have all the necessary system calls e.g. socket() returning to an int called listener. bind() then listen(), all with suitable error checking, which isnt returning any problems. It also compiles just fine.

FD_ZERO(&fileDescriptors);
FD_ZERO(&tempSet);
.....
FD_SET(listener, &fileDescriptors);
fdmax = listener;
.....
while(1){
   if(select(fdmax+1, &tempSet, NULL, NULL, &timeout) == -1){
          //error occured
   }
.....
}

The client cant establish a connection, however WSAGetLastError() Returns 0 on the client side. And the server, never gets passed select(), apart from returning 0 due to a timeout. Im really struggling to see the problem in my code.

1

There are 1 answers

1
caf On

You're adding listener to the set fileDescriptors, but instead you're passing the (empty) set tempSet to select.

In your comment you say you actually have code to "set tempSet equal to fileDescriptors". In general you cannot do this - you do not know anything about the internal structure of an fd_set, it may well have pointers that need to be deep-copied.

It is a pity that there is no FD_COPY - but that's how it is. You have to construct the set each time with FD_ZERO and FD_SET.