I am facing a problem using the select function in Unix.
I have a server that waits for for a connection. First I add the listening socket file descriptor listener
to the fd_set readfds
using FD_SET(listener, readfds)
and then I use that in select()
.
When I get a connection, I call accept()
and set the readfds
in select with the accepted file descriptor and start receiving the data from connection. However, when I check the code in strace, The select doesn't show the listener in the readfds
while select()
is executing a second time.
Do I need to set the listener file descriptor again using FD_SET(listener, readfds)
before calling select()
again?
Thanks.
The fd_set structure is used by various Windows Sockets functions and service providers, such as the select function, to place sockets into a "set" for various purposes, such as testing a given socket for readability using the readfds parameter of the select function.
The select() system call enables a system to keep track of several file descriptors. So, the select system call waits for one of the descriptors or a whole to turn out to be “ready” for a particular type of I/O activity (e.g., input possible).
RETURN VALUE FD_ISSET() a non-zero value if the bit for the file descriptor fd is set in the file descriptor set pointed to by fdset, and 0 otherwise. On successful completion, select() returns the total number of bits set in the bit masks. Otherwise, -1 is returned, and errno is set to indicate the error.
RETURN VALUE On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets (that is, the total number of bits that are set in readfds, writefds, exceptfds) which may be zero if the timeout expires before anything interesting happens.
Yes (it is necessary to reset the fd_set
between select()
system calls).
It is a nuisance, but they act as input/output parameters; they are read by and modified by the system call. When select()
returns, the values have all been modified to reflect the set of file descriptors ready. So, every time before you call select()
, you have to (re)initialize the fd_set
values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With