linuxÖÐselect()º¯Êý·ÖÎö
select()µÄ»úÖÆÖÐÌṩһfd_setµÄÊý¾Ý½á¹¹£¬Êµ¼ÊÉÏÊÇÒ»longÀàÐ͵ÄÊý×飬 ÿһ¸öÊý×éÔªËض¼ÄÜÓëÒ»´ò¿ªµÄÎļþ¾ä±ú£¨²»¹ÜÊÇSocket¾ä±ú,»¹ÊÇÆäËû Îļþ»òÃüÃû¹ÜµÀ»òÉ豸¾ä±ú£©½¨Á¢ÁªÏµ£¬½¨Á¢ÁªÏµµÄ¹¤×÷ÓɳÌÐòÔ±Íê³É£¬ µ±µ÷ÓÃselect()ʱ£¬ÓÉÄں˸ù¾ÝIO״̬ÐÞ¸Äfd_setµÄÄÚÈÝ£¬ÓÉ´ËÀ´Í¨ÖªÖ´ ÐÐÁËselect()µÄ½ø³ÌÄÄÒ»Socket»òÎļþ¿É¶Á£¬ÏÂÃæ¾ßÌå½âÊÍ£º
#include
#include
#include
int select(nfds, readfds, writefds, exceptfds, timeout)
int nfds;
fd_set *readfds, *writefds, *exceptfds;
struct timeval *timeout;
ndfs£ºselect¼àÊÓµÄÎļþ¾ä±úÊý£¬ÊÓ½ø³ÌÖдò¿ªµÄÎļþÊý¶ø¶¨,Ò»°ãÉèΪÄØÒª¼àÊÓ¸÷Îļþ ÖеÄ×î´óÎļþºÅ¼ÓÒ»¡£
readfds£ºselect¼àÊӵĿɶÁÎļþ¾ä±ú¼¯ºÏ¡£
writefds: select¼àÊӵĿÉдÎļþ¾ä±ú¼¯ºÏ¡£
exceptfds£ºselect¼àÊÓµÄÒì³£Îļþ¾ä±ú¼¯ºÏ¡£
timeout£º±¾´Îselect()µÄ³¬Ê±½áÊøʱ¼ä¡££¨¼û/usr/sys/select.h£¬ ¿É¾«È·ÖÁ°ÙÍò·ÖÖ®Ò»Ã룡£©
µ±readfds»òwritefdsÖÐÓ³ÏóµÄÎļþ¿É¶Á»ò¿Éд»ò³¬Ê±£¬±¾´Îselect() ¾Í½áÊø·µ»Ø¡£³ÌÐòÔ±ÀûÓÃÒ»×éϵͳÌṩµÄºêÔÚselect()½áÊøʱ±ã¿ÉÅÐ ¶ÏÄÄÒ»Îļþ¿É¶Á»ò¿Éд¡£¶ÔSocket±à³ÌÌرðÓÐÓõľÍÊÇreadfds¡£ ¼¸Ö»Ïà¹ØµÄºê½âÊÍÈçÏ£º
FD_ZERO(fd_set *fdset)£ºÇå¿ÕfdsetÓëËùÓÐÎļþ¾ä±úµÄÁªÏµ¡£
FD_SET(int fd, fd_set *fdset)£º½¨Á¢Îļþ¾ä±úfdÓëfdsetµÄÁªÏµ¡£
FD_CLR(int fd, fd_set *fdset)£ºÇå³ýÎļþ¾ä±úfdÓëfdsetµÄÁªÏµ¡£
FD_ISSET(int fd, fdset *fdset)£º¼ì²éfdsetÁªÏµµÄÎļþ¾ä±úfdÊÇ·ñ
¿É¶Áд£¬>0±íʾ¿É¶Áд¡£
£¨¹ØÓÚfd_set¼°Ïà¹ØºêµÄ¶¨Òå¼û/usr/include/sys/types.h£©
ÕâÑù£¬ÄãµÄsocketÖ»ÐèÔÚÓ櫶«¶ÁµÄʱºò²Å¶ÁÈ룬´óÖÂÈçÏ£º
...
int sockfd;
fd_set fdR;
struct timeval timeout = ..;
...
for(;;) {
FD_ZERO(&fdR);
FD_SET(sockfd, &fdR);
switch (select(sockfd + 1, &fdR, NULL, &timeout)) {&
Ïà¹ØÎĵµ£º
Ò»¡¢ÒýÑÔ£º
±¾ÎĵµµÄÄÚÈݴ󲿷ÝÄÚÈݶ¼ÊÇ´ÓÍøÉÏÊÕ¼¯¶øÀ´£¬È»ºóÅäºÏһЩеĽØͼ£¨Äں˰汾£ºV2.4.19£©¡£ÔÚÿһÅäÖÃÏîºó»áÓÐÒ»¸öÑ¡ÔñÖ¸ÄϵIJ¿·Ý£¬ÓÃÀ´Ö¸µ¼´ó¼ÒÔõôÑù¸ù¾Ý×Ô¼ºµÄÇé¿öÀ´×öÏàÓ¦µÄÑ¡Ôñ£»»¹ÓÐÔÚÿһ¸ö´óÏîºÍÎĵµµÄ×îºó»áÓÐÒ»¸ö¾Ñé̸£¬ËüÊÇһЩ¸ßÊÖÃÇÔÚÓ¦¶ÔÎÊÌâºÍ´¦ÀíÌØÓÐÓ²¼þʱµÄһЩ¾Ñ飨Õâ¸ö»¹µ ......
1. ÄÚºËѧϰ·½·¨£¬±àÒë¡¢µ÷ÊԵȳ£¼ûÎÊÌâ
1.1 ¹ØÓÚ±àÒëÉý¼¶Äں˵½2.6.0µÄһЩÎÊÌâ ×÷Õߣºommm
http://linux.chinaunix.net/bbs/thread-281831-1-5.html
1.2 VMWare Workstation 6.0µ÷ÊÔLinux Kernel£¬¾¹Èç´Ë·½±ã ×÷Õߣºalbcamus
http://linux.chinaunix.net/bb ......
LinuxÏÂC++¿ª·¢¹¤¾ß½éÉÜ
¡¡¡¡¸ÅÊö
¡¡¡¡¾ÍC£«£«¿ª·¢¹¤¾ß¶øÑÔ£¬ÓëWindowsÏÂ΢Èí£¨VC£¬ VS2005µÈ£©Ò»Í³ÌìÏÂÏà±È£¬Linux/UnixÏÂC£«£«¿ª·¢£¬¿ÉνÎ廨°ËÃÅ£¬¸÷ʽ¸÷Ñù¡£Emacs, vi, eclipse, anjuta£¬kdevelopµÈ²ã³ö²»Çî¡£
¡¡¡¡WindowsÏ£¬¿ª·¢¹¤¾ß¶àÒÔ¼¯³É¿ª·¢»·¾³IDEµÄÐÎʽչÏÖ¸ø×îÖÕÓû§¡£ÀýÈ磬VS2005¼¯³ ......
ʲôÊÇSocket
¡¡¡¡ Socket½Ó¿ÚÊÇTCP/IPÍøÂçµÄAPI£¬Socket½Ó¿Ú¶¨ÒåÁËÐí¶àº¯Êý»òÀý³Ì£¬³ÌÐòÔ±¿ÉÒÔÓÃËüÃÇÀ´¿ª·¢TCP/IPÍøÂçÉϵÄÓ¦ÓóÌÐò¡£ÒªÑ§InternetÉϵÄTCP/IPÍøÂç±à³Ì£¬±ØÐëÀí½âSocket½Ó¿Ú¡£
¡¡ ¡¡ Socket½Ó¿ÚÉè¼ÆÕß×îÏÈÊǽ«½Ó¿Ú·ÅÔÚUnix²Ù×÷ϵͳÀïÃæµÄ¡£Èç¹ûÁ˽âUnixϵͳµÄÊäÈëºÍÊä³öµÄ»°£¬¾ÍºÜÈÝÒ×Á˽âSocketÁË¡£ÍøÂçµÄ ......