nse_select_server.c

Go to the documentation of this file.
00001 #include "echo.h"
00002 
00003 int ns_errno;
00004 
00005 int main(int argc, char **argv)
00006 {
00007   int i, maxi, maxfd, listenfd, connfd, sockfd;
00008   short port;
00009   int nready, client[FD_SETSIZE];
00010   ssize_t n;
00011   fd_set rset, allset;
00012   char line[MAXLINE];
00013   socklen_t clilen;
00014   struct sockaddr_in cliaddr, servaddr;
00015 
00016   if (argc == 2)
00017     port = atoi(argv[1]);
00018   else
00019     port = DEFAULT_PORT;
00020 
00021   proxy_init("nse.cfg");
00022 
00023   if ((listenfd = proxy_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00024     perror("Failed to create socket");
00025     exit(EXIT_FAILURE);
00026   }
00027 
00028   memset(&servaddr, 0x0, sizeof(servaddr));
00029   servaddr.sin_family = AF_INET;
00030   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
00031   servaddr.sin_port = htons(port);
00032 
00033   if (proxy_bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
00034     perror("Failed to bind");
00035     exit(EXIT_FAILURE);
00036   }
00037 
00038   printf("Server listening on port %d\n", ntohs(servaddr.sin_port));
00039 
00040   if (proxy_listen(listenfd, 20) < 0) {
00041     perror("Listen failed");
00042     exit(EXIT_FAILURE);
00043   }
00044 
00045   maxfd = listenfd;     /* initialize */
00046   maxi = -1;            /* index into client[] array */
00047   for (i = 0; i < FD_SETSIZE; i++)
00048     client[i] = -1;     /* -1 indicates available entry */
00049   FD_ZERO(&allset);
00050   FD_SET(listenfd, &allset);
00051   /* end fig01 */
00052 
00053   /* include fig02 */
00054   for (;;) {
00055     rset = allset;      /* structure assignment */
00056     nready = select(maxfd + 1, &rset, NULL, NULL, NULL);
00057 
00058     if (FD_ISSET(listenfd, &rset)) {    /* new client connection */
00059       clilen = sizeof(cliaddr);
00060       connfd = proxy_accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
00061 #ifdef  NOTDEF
00062       printf("new client: %s, port %d\n",
00063          Inet_ntop(AF_INET, &cliaddr.sin_addr, 4, NULL), ntohs(cliaddr.sin_port));
00064 #endif
00065 
00066       for (i = 0; i < FD_SETSIZE; i++)
00067     if (client[i] < 0) {
00068       client[i] = connfd;   /* save descriptor */
00069       break;
00070     }
00071       if (i == FD_SETSIZE) {
00072     fprintf(stderr, "too many clients");
00073     close(connfd);
00074     continue;
00075       }
00076 
00077       FD_SET(connfd, &allset);  /* add new descriptor to set */
00078       if (connfd > maxfd)
00079     maxfd = connfd;     /* for select */
00080       if (i > maxi)
00081     maxi = i;       /* max index in client[] array */
00082 
00083       if (--nready <= 0)
00084     continue;       /* no more readable descriptors */
00085     }
00086 
00087     for (i = 0; i <= maxi; i++) {   /* check all clients for data */
00088       if ((sockfd = client[i]) < 0)
00089     continue;
00090       if (FD_ISSET(sockfd, &rset)) {
00091     if ((n = Readline(sockfd, line, MAXLINE)) == 0) {
00092       /* 4connection closed by client */
00093       close(sockfd);
00094       FD_CLR(sockfd, &allset);
00095       client[i] = -1;
00096     } else
00097       Writen(sockfd, line, n);
00098 
00099     if (--nready <= 0)
00100       break;        /* no more readable descriptors */
00101       }
00102     }
00103   }
00104 }
00105 
00106 /* end fig02 */