Functions | Variables

nse_select_server.c File Reference

#include "echo.h"
Include dependency graph for nse_select_server.c:

Go to the source code of this file.

Functions

int main (int argc, char **argv)

Variables

int ns_errno

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 5 of file nse_select_server.c.

{
  int i, maxi, maxfd, listenfd, connfd, sockfd;
  short port;
  int nready, client[FD_SETSIZE];
  ssize_t n;
  fd_set rset, allset;
  char line[MAXLINE];
  socklen_t clilen;
  struct sockaddr_in cliaddr, servaddr;

  if (argc == 2)
    port = atoi(argv[1]);
  else
    port = DEFAULT_PORT;

  proxy_init("nse.cfg");

  if ((listenfd = proxy_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Failed to create socket");
    exit(EXIT_FAILURE);
  }

  memset(&servaddr, 0x0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(port);

  if (proxy_bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
    perror("Failed to bind");
    exit(EXIT_FAILURE);
  }

  printf("Server listening on port %d\n", ntohs(servaddr.sin_port));

  if (proxy_listen(listenfd, 20) < 0) {
    perror("Listen failed");
    exit(EXIT_FAILURE);
  }

  maxfd = listenfd;     /* initialize */
  maxi = -1;            /* index into client[] array */
  for (i = 0; i < FD_SETSIZE; i++)
    client[i] = -1;     /* -1 indicates available entry */
  FD_ZERO(&allset);
  FD_SET(listenfd, &allset);
  /* end fig01 */

  /* include fig02 */
  for (;;) {
    rset = allset;      /* structure assignment */
    nready = select(maxfd + 1, &rset, NULL, NULL, NULL);

    if (FD_ISSET(listenfd, &rset)) {    /* new client connection */
      clilen = sizeof(cliaddr);
      connfd = proxy_accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
#ifdef  NOTDEF
      printf("new client: %s, port %d\n",
         Inet_ntop(AF_INET, &cliaddr.sin_addr, 4, NULL), ntohs(cliaddr.sin_port));
#endif

      for (i = 0; i < FD_SETSIZE; i++)
    if (client[i] < 0) {
      client[i] = connfd;   /* save descriptor */
      break;
    }
      if (i == FD_SETSIZE) {
    fprintf(stderr, "too many clients");
    close(connfd);
    continue;
      }

      FD_SET(connfd, &allset);  /* add new descriptor to set */
      if (connfd > maxfd)
    maxfd = connfd;     /* for select */
      if (i > maxi)
    maxi = i;       /* max index in client[] array */

      if (--nready <= 0)
    continue;       /* no more readable descriptors */
    }

    for (i = 0; i <= maxi; i++) {   /* check all clients for data */
      if ((sockfd = client[i]) < 0)
    continue;
      if (FD_ISSET(sockfd, &rset)) {
    if ((n = Readline(sockfd, line, MAXLINE)) == 0) {
      /* 4connection closed by client */
      close(sockfd);
      FD_CLR(sockfd, &allset);
      client[i] = -1;
    } else
      Writen(sockfd, line, n);

    if (--nready <= 0)
      break;        /* no more readable descriptors */
      }
    }
  }
}

Here is the call graph for this function:


Variable Documentation

int ns_errno

Definition at line 3 of file nse_select_server.c.