Source of cmd_sockets.c


/* sockets.c -- test of assynchronous input over stdin/stdout and berkley sockets */

/* for assynchronous i/o via select() */
#include <sys/types.h>
#include <sys/select.h>
#include <sys/time.h>

/* for printf() */
#include <stdio.h>

/* why aren't these already defined? */
#define FALSE (0)
#define TRUE (!(FALSE))


static int poll_socket_read(int socket_handle) {                 /* poll a socket for readability */
  fd_set rfds;                                                   /* the read file descriptors */
  struct timeval tv;                                             /* timer value */
  FD_ZERO(&rfds);                                                /* clear out all read files */
  FD_SET(socket_handle, &rfds);                                  /* Watch stdin (fd 0) to see when it has input. */
  tv.tv_sec = 0;                                                 /* wait zero seconds to poll */
  tv.tv_usec = 0;                                                /* and zero microsec too */
  return select(2, &rfds, NULL, NULL, &tv) != 0;
}


static int poll_stdin(void) {                                    /* test if any input is waiting for us on stdin */
  return poll_socket_read(0);                                    /* stdin == 0 */
}


#define MAXLEN 1024
char line[MAXLEN];                                               /* the input line buffer */
int len = 0;                                                     /* length of the line that we have read */

static char* nb_rdline(void) {                                   /* read stdin asynchronously */

  /* Return a pointer to the line that was read.  This routine is non-blocking.  It will only return one line per call, or NULL if
     no line was available, or EOF if end-of-file was detected.  The caller should copy the line into his own buffer if it needs
     to survive the next call to this routine. */

  int ch;                                                        /* a single character read from stdin */

  /* start with an empty line buffer if this is a new line of data */

  if (len != 0                                                   /* is line non-empty? */
      && line[len - 1] == '\n') {                                /* yes, does line end with newline? */
    len = 0;                                                     /* yes, make line empty so we can read the next line */
    line[len] = '\0';                                            /* nul-terminate per C convention */
  }

  if (!poll_stdin()) {                                           /* is there any input waiting? */
    return NULL;                                                 /* no, return empty handed */
  }

  for(;;) {                                                      /* loop until an exit condition occurs */
    ch = fgetc(stdin);                                           /* yes, input waiting, get a character */

    if (ch == EOF) {                                             /* is it end of file? */
      return (char*)EOF;
    }

    line[len++] = ch;                                            /* not EOF, save the character in the line buffer */
    line[len] = '\0';                                            /* nul-terminate it */

    if (ch == '\n') {                                            /* was it a newline? */
      return &line[0];                                           /* yes, return with the line we just finished */
    }
  }
}


char* enquiry(void) {                                            /* read an enquiry from a connection */
  return nb_rdline();                                            /* return the line */
}


void response(char* resp) {                                      /* send a response to an enquiry */
  printf("%s", resp);
  fflush(stdout);
  fprintf(stderr, "RESPONDED: %s", resp);
}