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   listenfd = socket(AF_INET, SOCK_STREAM, 0);
00022 
00023   memset(&servaddr, 0x0, sizeof(servaddr));
00024   servaddr.sin_family = AF_INET;
00025   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
00026   servaddr.sin_port = htons(port);
00027 
00028   bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
00029 
00030   printf("Server listening on port %d\n", ntohs(servaddr.sin_port));
00031 
00032   listen(listenfd, 20);
00033 
00034   maxfd = listenfd;     /* initialize */
00035   maxi = -1;            /* index into client[] array */
00036   for (i = 0; i < FD_SETSIZE; i++)
00037     client[i] = -1;     /* -1 indicates available entry */
00038   FD_ZERO(&allset);
00039   FD_SET(listenfd, &allset);
00040   /* end fig01 */
00041 
00042   /* include fig02 */
00043   for (;;) {
00044     rset = allset;      /* structure assignment */
00045     nready = select(maxfd + 1, &rset, NULL, NULL, NULL);
00046 
00047     if (FD_ISSET(listenfd, &rset)) {    /* new client connection */
00048       clilen = sizeof(cliaddr);
00049       connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
00050 #ifdef  NOTDEF
00051       printf("new client: %s, port %d\n",
00052          Inet_ntop(AF_INET, &cliaddr.sin_addr, 4, NULL), ntohs(cliaddr.sin_port));
00053 #endif
00054 
00055       for (i = 0; i < FD_SETSIZE; i++)
00056     if (client[i] < 0) {
00057       client[i] = connfd;   /* save descriptor */
00058       break;
00059     }
00060       if (i == FD_SETSIZE) {
00061     fprintf(stderr, "too many clients");
00062     close(connfd);
00063     continue;
00064       }
00065 
00066       FD_SET(connfd, &allset);  /* add new descriptor to set */
00067       if (connfd > maxfd)
00068     maxfd = connfd;     /* for select */
00069       if (i > maxi)
00070     maxi = i;       /* max index in client[] array */
00071 
00072       if (--nready <= 0)
00073     continue;       /* no more readable descriptors */
00074     }
00075 
00076     for (i = 0; i <= maxi; i++) {   /* check all clients for data */
00077       if ((sockfd = client[i]) < 0)
00078     continue;
00079       if (FD_ISSET(sockfd, &rset)) {
00080     if ((n = Readline(sockfd, line, MAXLINE)) == 0) {
00081       /* 4connection closed by client */
00082       close(sockfd);
00083       FD_CLR(sockfd, &allset);
00084       client[i] = -1;
00085     } else
00086       Writen(sockfd, line, n);
00087 
00088     if (--nready <= 0)
00089       break;        /* no more readable descriptors */
00090       }
00091     }
00092   }
00093 }
00094 
00095 /* end fig02 */