All pastes #793493 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-11-22 09:47 UTC
rendered paste body
#include <sys/select.h>

void wait_and_act(int sockfd)
{
  struct timeval timeout;
  int rc; /* select() return code */
  long timeout_ms = 1000;

  fd_set fdread;
  fd_set fdwrite;
  fd_set fdexcep;
  int maxfd;

  FD_ZERO(&fdread);
  FD_ZERO(&fdwrite);
  FD_ZERO(&fdexcep);

  /* set timeout to wait */
  timeout.tv_sec = timeout_ms/1000;
  timeout.tv_usec = (timeout_ms%1000)*1000;

  FD_SET(fileno(stdin), &fdread);
  FD_SET(sockfd, &fdread);
  FD_SET(sockfd, &fdwrite);

  /* check stdout for writabilty can't make sense? */

  rc = select(maxfd+1,
              (fd_set *)&fdread,
              (fd_set *)&fdwrite,
              (fd_set *)&fdexcep, &timeout);

  switch(rc) {
  case -1:
    /* select error */
    break;
  case 0:
    /* timeout! */
    break
  default:
    /* use FD_ISSET() to check what happened, then read/write accordingly */
    break;
  }

}