xgwang
xgwang

Reputation: 646

how to check nonblocking socket timeout WITHOUT select

i got some legacy code using: nonblocking socket, select for timeout, read(2) and write(2). Now it occasionally failed due to select/1024 fd limits. so i need to replace the select.

It seems RCVTIMEO and SNDTIMEO can also check timeout but they work for blocking mode, and it impacts too much to change from non-blocking to blocking.

So is there any other best practice to check timeout for nonblocking socket(no select)? Or i have to get some timer/nanosleep to solve this?

Upvotes: 1

Views: 1655

Answers (3)

ciphor
ciphor

Reputation: 8288

epoll is a better solution than select, which is not limited to 1024 descriptors.

In fact, you can use libevent or libev to handle low level asynchronous socket I/O, they are the so-called "best practice" for async I/O.

Upvotes: 3

Andy Ross
Andy Ross

Reputation: 12043

The poll() system call will take a timeout and doesn't have a fixed limit of file descriptors. If you really have 1000 open descriptors though, you would probably be better served by epoll(), which is more complicated to use but has much better scaling characteristics.

Upvotes: 2

caf
caf

Reputation: 239041

poll() is essentially a drop-in replacement for using select(), but does not have the 1024 file descriptor limit. You will have to change your code slightly to create an array of struct pollfd structures instead of using fd_sets, but the overall structure of the code should not have to change.

Upvotes: 4

Related Questions