这是代码:
FD_ZERO(&t_set);
num = 0;
maxfd = 0;
while (num < MAX)
{
if ((sockfd = connectSocket(ptr)))
;
//printf("connect to server succeed!n%dn", fd);
else
{
fprintf(stderr, "connect errorn");
exit(1);
}
if (!writeSocket(sockfd, ptr))
;
//printf("write to server succeed!n");
else
{
fprintf(stderr, "write errorn");
exit(1);
}
if (maxfd < sockfd)
maxfd = sockfd;
FD_SET(sockfd, &t_set);
printf("%dn", t_set);
memset(fileName, 0, BUFSIZE);
sprintf(fileName,"%d.html", sockfd);
if ((filefd = open(fileName, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1)
{
printf("create file failed!n");
return -1;
}
ff[num].sockfd = sockfd;
ff[num].filefd = filefd;
++num;
}
printf(">------------------->n");
while (1)
{
//sleep(1);
tv.tv_sec = 1;
tv.tv_usec = 0;
h = 0;
printf("%dn", t_set); //print the t_set before select
h = select(maxfd + 1, &t_set, NULL, NULL, &tv);
printf("%dn", t_set); //print the t_set after select
printf(">------------------->n");
//fd_set is an array of long;
//if use printf("%d %d", t_set, h); secondly may print the second value of t_set , not h;
if (h < 0)
{
fprintf(stderr, "select error");
return -1;
}
if (h == 0)
{
fprintf(stderr, "select return 0n");
break;
}
else
{
int n = 0;
while (!FD_ISSET(ff[n].sockfd, &t_set))
++n;
if (readSocket(ff[n].sockfd, ff[n].filefd))
{
FD_CLR(ff[n].sockfd, &t_set);
printf("===================== %dn", n);
}
}
}
num = 0;
while (num++ < MAX)
close(ff[num].filefd);
输出:8 // initial of t_set
40
168
680
2728
>------------------->
2728 // the two value were print before and after the select;
680
>------------------->
680
680
>------------------->
680
680
>------------------->
680
680
打印t_set的值,我发现t_set的值在第一次和最后一次循环调用select后发生了变化。如果在select和set tv_set之前添加sleep(1)。SEC = 0, tv_usec = 0,使用sleep timeout代替select timeout,将t_set执行正常。还有一个问题,上面的例子不能监控最后一个套接字,使用睡眠改变也可以修复这个问题;
任何建议都是感激的!
从select(2)手册页:
"在退出时,设置[即readfs
, writefds
和exceptfds
]被修改,以指示哪些文件描述符实际更改了状态。"
您只使用readfs
,但假定由select
系统调用修改。