The select() function checks the sockets specified in the sets of descriptors supplied as parameters to see if any of the sockets are ready for receiving or sending, or have out of band data pending. On return, the descriptor sets are replaced with subsets consisting of those sockets of each set that are ready for the associated operation.
Only one thread may select on a particular aspect of a socket at a time. For example, one thread may select on only receiving and another thread may simultaneously select on only sending on the same socket, but both threads may not select on receiving on the same socket at the same time. If this is attempted, only one thread will succeed and an EALREADY error will be returned to any other threads.
int select( int nfds; fd_set * recvfds, fd_set * sendfds, fd_set * oobfds, unsigned short timeout );
nfds
nfds
should be eight, not two. The value of nfds
may be used as an alternative to FD_SETSIZE.
recvfds
sendfds
oobfds
timeout
timeout
to 0xffff causes select() to wait until one or more of the sockets specified by the descriptor sets are ready. Setting timeout
to 0 causes select() to return immediately with the current status of the sockets specified by the descriptor sets. Setting timeout
to any value in between specifies the maximum amount of time to wait for any socket specified by the descriptor sets to become ready. A set of macros is supplied to ease manipulation of sets of socket descriptors.
FD_SET(s, &fds) |
Adds socket s to descriptor set fds. |
FD_CLR(s, &fds) |
Removes socket s from descriptor set fds. |
FD_ISSET(s, &fds) |
Returns nonzero if socket s is in set fds; if not, returns 0 (zero). |
FD_ZERO(&fds) |
Clears descriptor set fds. |
An additional macro is supplied for compatibility with other implementations to convert a timeout specified as a number of seconds and microseconds in a timeval structure to the equivalent number of 10ms ticks required by the select() function. A null pointer translates to a timeout value that causes select() to wait indefinitely. A timeval structure that specifies a timeout of more than 655.35 seconds produces an undefined result.
TV_TICKS(&tv) |
Converts timeout in timeval structure tv to 10 ms ticks. |
The total number of ready sockets contained in the descriptor sets, or -1 if an error occurs. If the timeout expires before any sockets become ready, 0 is returned. If select() returns an error, the socket descriptor sets are unmodified.
The select call may be used to determine when more data arrives.
Possible errno values returned on failure are as follows:
EALREADY |
Another thread is currently performing a select() on at least one of the sockets referenced by recvfds , sendfds or oobfds . |
EBADF |
At least one of the sockets referenced by recvfds , sendfds or oobfds is an invalid descriptor. |
EINVAL |
Invalid nfds parameter specified; the recvfds , sendfds and oobfds pointer parameters are all null. |
ENOTSOCK |
At least one of the descriptors referenced by recvfds , sendfds or oobfds is not a socket. |
EUNATCH |
The TCP/IP kernel has not been loaded. |
Versions | Defined in | Include | Link to |
---|---|---|---|
INtime 3.0 | intime/rt/include/sys/socket.h | sys/types.h sys/socket.h |
netiff3m.lib |