Linux?select???????
???????????? ???????[ 2015/2/16 11:48:08 ] ????????Linux ?????? ????
????select?????????????????????????????????????仯???????????select????????????????????????????????????????????
???????????????????????????????????socket????????????????
????int socket(int domain?? int type?? int protocol);
?????????????????0??1??2??????0????????1?????????2?????????????0??1??2?????????????????FILE *????????stdin??stdout??stderr??
?????????????select??????????????????Щ????????仯???select??????????????£?
????int select (int nfds?? fd_set *readfds?? fd_set *writefds?? fd_set *exceptfds?? struct timeval *timeout);
?????????????????timeout??????????????????????struct timeval *???????struct timeval???????????????????????????????????struct timeval tv;???????tv????&tv?????select??????struct timeval?????????£?
????struct timeval
????{
????long tv_sec; //seconds
????long tv_usec; //microseconds
????}??
??????2??3??4??????????????????;fd_set *??????????????????????fd_set?????????????rdfds??wtfds??exfds?????????????????&rdfds??&wtfds??&exfds?????select????????????????????????????????????rdfds??????????????????:?????????????????????select??????????????????????????о????????д????????select????????????????????exfds???????????????????????????????????????select????????????????????????????socket???????????????????????????????????????socket??????????????????????????
fd_set rdfds;
struct timeval tv;
int ret;
FD_ZERO(&rdfds);
FD_SET(socket?? &rdfds);
tv.tv_sec = 1;
tv.tv_uses = 500;
ret = select (socket + 1?? %rdfds?? NULL?? NULL?? &tv);
if(ret < 0) perror (“select”);
else if (ret = = 0) printf(“time out”);
else {
printf(“ret = %d/n”??ret);
if(FD_ISSET(socket?? &rdfds)){
/* ???socket?????????? */
recv( );
}
}
???????select??????????????????????????????????????????????1.????????????3?????;
int sa?? sb?? sc;
sa = socket(……);
connect (sa??….);
sb = socket(….);
connect (sb??…);
sc = socket(….);
connect(sc??…);
FD_SET(sa?? &rdfds);
FD_SET(sb?? &rdfds);
FD_SET(sc?? &rdfds);
?????????select???????????????3??????е????????????????????????????????????????socket????????£?
????int maxfd = 0;
????if(sa > maxfd) maxfd = sa;
????if(sb > maxfd) maxfd = sb;
????if(sc > maxfd) maxfd = sc;
??????????select??????
????ret = select (maxfd+1?? &rdfds?? NULL?? NULL??&tv);
?????????????????????????????????????????????????e???????0?????????select????????????????£?
FD_ZERO(&rdfds);
FD_SET(0?? &rdfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select (1?? &rdfds??NULL??NULL??&tv);
if(ret < 0) perror(“select”);
else if (ret = = 0) printf (“time out/n”);
else{
scanf(“%s”??buf);
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11