????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);
}