graw
graw

Reputation: 233

select() on sockets with timeout

Inside the read FD_SET I have several sockets:

  1. the main socket listening for new connections
  2. accepted sockets listening for incoming data.

I set the timeout for 30 seconds and called select(). I quickly noticed the behavior is different for each:

  1. When a new client connects on the listening port, it returns immediately from blocking.
  2. When an already connected client sends a message, it blocks for the entire 30 seconds.

Is there a way I can make it return immediately in the second case?

Upvotes: 1

Views: 643

Answers (3)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84151

One of the most common errors with select(2) is not re-initializing fd_sets before calling select() again.

Upvotes: 1

Bob
Bob

Reputation: 31

My guess is either you aren't including all your sockets in the correct fd_set or you aren't passing in the highest numbered file descriptor plus 1 as the first parameter (nfds below) to the select call.

select(nfds, &readfds, &writefds, &execptfds, &timeout);

Upvotes: 2

Roman Nikitchenko
Roman Nikitchenko

Reputation: 13046

I constantly use such select()s without any problem.

Probably you do something wrong with accepted sockets waiting on data. Could you please post code fragment? Especially most important is how you set first select() parameter.

Upvotes: 0

Related Questions