gtpc2m8kC/C++ Language Support User's Guide

tpf_select_bsd-Indicates Read, Write, and Exception Status

This function indicates which of the specified file descriptors is ready for reading and writing, or has an error condition pending. If the specified condition is false for all of the specified file descriptors, the tpf_select_bsd function blocks up to the timeout interval specified, until the specified condition is true for at least one of the specified file descriptors.

Format

#include   <sysgtime.h>
int tpf_select_bsd(int maxfds, fd_set* reads, fd_set* writes,
                   fd_set* excepts, struct timeval* tv)   

maxfds
The value is ignored.

reads
A pointer to a set of file descriptors to be checked for readiness for reading.

writes
A pointer to a set of file descriptors to be checked for readiness for writing.

excepts
A pointer to a set of file descriptors to be checked for exception pending conditions.

tv
A pointer to the maximum timeout interval in milliseconds.

If the tv argument is not NULL, it points to an object of type struct timeval that specifies a maximum interval to wait for the selection to complete. If the tv argument points to an object whose members are 0, the tpf_select_bsd function does not block. If the tv argument is a null pointer, the tpf_select_bsd function blocks until an event causes one the file descriptor sets to not be empty. If the time limit expires before any event occurs that would cause one the file descriptor sets not to be empty, the tpf_select_bsd function completes successfully and returns 0.

On succesful completion, the objects pointed to by the reads, writes, and excepts arguments are modified to indicate which file descriptors are ready. The file descriptors in each file descriptor set input that are not ready are removed from the respective file descriptor set.

If the reads, writes, and excepts arguments are all null pointers and the tv argument is not a null pointer, the tpf_select_bsd function blocks for the time interval specified. If the reads, writes, and excepts arguments are all null pointers and the tv argument is a null pointer, the tpf_select_bsd function returns immediately.

On error return, the objects pointed to by the reads, writes, and excepts arguments are not modified.

Normal Return

The number of ready file descriptors. A value of 0 indicates an expired time limit. If the return value is greater than 0, the reads, writes, and excepts arguments are modified.

Error Return

A value of -1 indicates an error. See select-Monitor Read, Write, and Exception Status for possible sock_errno values.

Programming Considerations

Examples

The following example shows a simplified server using the BSD style functions to listen on a socket and accept new connections.

#include <stdio.h>
#include <stdlib.h>
#include <socket.h>
#include <sysgtime.h>
#include <sys/types.h>
#include <unistd.h>
 
int main(void)
{
    fd_set rfds, rfd2;
    fd_set efds, efd2;
    struct timeval tv;
    int myfds[FD_SETSIZE];
    int rc;
    int newsock;
    int maxsock = 1;
    int i;
 
    /* establish listener in first position */
    myfds[0] = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if (myfds[0] < 0)
       exit(1);
    rc = listen(myfds[0], 5);
    if (rc < 0)
       exit(1);
    FD_ZERO(&rfds);
    FD_ZERO(&efds);
 
    FD_SET(myfds[0], &rfds); /* add listener socket */
    /* Wait up to 60 seconds. */
    tv.tv_sec = 60;
    tv.tv_usec = 0;
    FD_COPY(&rfds, &rfd2);
    FD_COPY(&efds, &efd2);
 
    /* loop forever unless error from select */
    while ((rc = tpf_select_bsd(maxsock, &rfd2, NULL, &efd2, &tv)) >= 0) {
       /* when listener is ready, there is a new socket to accept */
       if (FD_ISSET(myfds[0], &rfd2)) {
          newsock = accept(myfds[0], NULL, NULL);
          if (newsock > 0) {
             FD_SET(newsock, &rfds);
             FD_SET(newsock, &efds);
             myfds[maxsock++] = newsock;
             my_accept(&newsock); /* call a function to handle the new connection */
          }
       }
       my_reads(&myfds, &rfd2); /* call a function to handle reads */
       for (i = 0; i < maxsock; i++)  /* look for errors */
          if (FD_ISSET(myfds[i], &efd2)) {
             /* close socket, remove from list */
             close(myfds[i]);
             FD_CLR(myfds[i], &rfds);
             FD_CLR(myfds[i], &efds);
          }
    /* last thing before returning to top of loop, reestablish the file descriptors
       used by select */
       FD_COPY(&rfds, &rfd2);
       FD_COPY(&efds, &efd2);
    }
    exit(1); /* if we get here, select had an error */
}
 

Related Information

See TPF Transmission Control Protocol/Internet Protocol for more information about socket file descriptors and the sock_errno function.