unix环境高级编程中有一个疑惑
loop(void)
{
int i, n, maxfd, maxi, listenfd, clifd, nread;
char buf[MAXLINE];
uid_t uid;
fd_set rset, allset;
FD_ZERO(&allset);
/* obtain fd to listen for client requests on */
if ( (listenfd = serv_listen(CS_OPEN)) < 0)
log_sys("serv_listen error");
FD_SET(listenfd, &allset);
maxfd = listenfd;
maxi = -1;
for ( ; ; ) {
rset = allset; /* rset gets modified each time around */
if ( (n = select(maxfd + 1, &rset, NULL, NULL, NULL)) < 0) //为什么测试socket的表述符
log_sys("select error");
if (FD_ISSET(listenfd, &rset)) {
/* accept new client request */
if ( (clifd = serv_accept(listenfd, &uid)) < 0)
log_sys("serv_accept error: %d", clifd);
i = client_add(clifd, uid);
FD_SET(clifd, &allset);
if (clifd > maxfd)
maxfd = clifd; /* max fd for select() */
if (i > maxi)
maxi = i; /* max index in client[] array */
log_msg("new connection: uid %d, fd %d", uid, clifd);
continue;
}
for (i = 0; i <= maxi; i++) { /* go through client[] array */
if ( (clifd = client[i].fd) < 0)
continue;
if (FD_ISSET(clifd, &rset)) {
/* read argument buffer from client */
if ( (nread = read(clifd, buf, MAXLINE)) < 0)
log_sys("read error on fd %d", clifd);
else if (nread == 0) {
log_msg("closed: uid %d, fd %d",
client[i].uid, clifd);
client_del(clifd); /* client has closed conn */
FD_CLR(clifd, &allset);
close(clifd);
} else /* process client's rquest */
request(buf, nread, clifd, client[i].uid);
}
}
}
}问题补充:
希望大家帮帮忙,本人感激不尽
------解决方案--------------------这儿来看,应该用处不大
不用的话就是阻塞在accept,用的话就是阻塞在select
------解决方案--------------------你可以理解为轮询一个数组,大小为maxfd + 1,当发现这个数组里面的某项有内容(就是描述符准备好),然后说明那项内容可以使用。然后就返回,可以往下accept了。
可能比喻得不太准确,不过,大概也就这个意思。