Home |
Function select |
Function selectDetermines a socket's state, optionally waits for state changes. typedef struct timeval { long tv_sec; long tv_usec; } timeval; int select(int fd, fd_set * fread, fd_set * fwrite, fd_set * fexception, const struct timeval * timeout); ParametersfdThis parameter is ignored. freadList of sockets to be checked if ready for reading. fwriteList of sockets to be checked if ready for writing. fexceptionList of sockets to be checked for error status. timeoutMaximum amount of time to block waiting for a socket to meet criteria. return valueThe number of sockets in the input lists which will not block when a read/write is done. It will return 0 if no sockets are ready (i.e. a timeout occurred). If an error occurred, call xn_getlasterror and xn_geterror_string to return the error value. Section Error Codes further describes each error. End of file is detected when the other side closes (i.e. sends a FIN) and our input window is empty. Possible values for this function are:
Select provides a timeout capability for connect, accept, recv, recvfrom, send, and sendto. Each of these functions block infinitely if the associated socket is in blocking mode or they return immediately if the socket is in non-blocking mode (see ioctlsocket). The following table summarizes the use of select; see below for more details.
The associated socket should be set to non-blocking before calling connect, accept, recv, and send to ensure the function will not block. select takes three socket lists and a timeout value as input. It blocks the select signal until either the timeout period expires or the select criteria, as specified by the lists, is met for at least one socket. If fread is non-null, select will return when any socket (UDP or TCP which is not a listener) in the fread socket list has data, or any read to the socket (TCP) will fail due to being in an illegal state for reads. For TCP, sockets which are listeners (i.e. returned by listen), select will return when a connection has been established and accept will succeed without blocking. If fwrite is non-null, select will return immediately if there is a UDP socket in the fwrite list, or when any TCP socket in the fwrite list which is not a non-blocking socket is connected, if there is any room in the output window. It will also return if a socket is in an illegal state for writes. For non-blocking TCP sockets, select will return immediately if the connection has been established or an error occurred. A non-blocking TCP socket is considered to be in the process of connecting if a connect has been done but a write (i.e. send and sendto) has not been done and if a select has not been done which returned the socket as ready. The three lists are modified to contain only the sockets which pass the above criteria. select uses the parameter timeout to determine how long to block. If timeout is NULL, select will block indefinitely until a socket is ready. If the *timeout fields tv_sec and tv_usec are both 0, select will not block. This feature allows polling on multiple sockets from one thread. select modifies the lists of sockets passed as parameters. It deletes the sockets from the lists which do not meet the criteria for the type of socket, as explained above. The following macros are available for setting up the lists passed to select: FD_SET(socket, fd)- add a socket to the list fd FD_CLR(socket, fd)- delete a socket from the list fd FD_ISSET(socket, fd)- determine if socket is in the list fd FD_ZERO(fd)- empty the list fd FD_SETSIZE (255), which is defined in socket.h, is the maximum number of sockets which can be in a socket list.
|