- 论坛徽章:
- 0
|
以下是unix网络编程上figure6.22的代码,我没有明白为什么要将rset = allset; 而在select里又用rset ,我觉的应该在将select函数的第2,3,4个参数全部设置。
25 for ( ; ; ) {
26 rset = allset; /* structure assignment */
27 nready = Select(maxfd + 1, &rset, NULL, NULL, NULL);
28 if (FD_ISSET(listenfd, &rset)) { /* new client connection */
29 clilen = sizeof(cliaddr);
30 connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
31 for (i = 0; i < FD_SETSIZE; i++)
32 if (client < 0) {
33 client = connfd; /* save descriptor */
34 break;
35 }
36 if (i == FD_SETSIZE)
37 err_quit("too many clients");
38 FD_SET(connfd, &allset); /* add new descriptor to set */
39 if (connfd > maxfd)
40 maxfd = connfd; /* for select */
41 if (i > maxi)
42 maxi = i; /* max index in client[] array */
43 if (--nready <= 0)
44 continue; /* no more readable descriptors */
45 }
46 for (i = 0; i <= maxi; i++) { /* check all clients for data */
47 if ( (sockfd = client) < 0)
48 continue;
49 if (FD_ISSET(sockfd, &rset)) {
50 if ( (n = Read(sockfd, buf, MAXLINE)) == 0) {
51 /* connection closed by client */
52 Close(sockfd);
53 FD_CLR(sockfd, &allset);
54 client = -1;
55 } else
56 Writen(sockfd, buf, n);
57 if (--nready <= 0)
58 break; /* no more readable descriptors */
59 }
60 }
61 }
62 }
[ 本帖最后由 aobai 于 2007-6-21 22:30 编辑 ] |
|