开发者

Select() problems in C (windows 7)

开发者 https://www.devze.com 2022-12-10 11:48 出处:网络
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 ha

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);
.....
F开发者_StackOverflow中文版D_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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消