int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeva l *timeout) |
¹¦ÄÜ£ºÂÖѯ»úÖÆ£¬¿ÉÒÔ¼àÌýÎļþÃèÊö·û¼¯ÖÐÎļþÃèÊö·ûµÄ״̬¡££¨¿É¶Á,¿Éд£¬Ò쳣״̬£©
²ÎÊý£ºnfds : ×î´óÎļþÃèÊö·û+ 1
readfds : ¶ÁÎļþÃèÊö¼¯ºÏ
writefds : дÎļþÃèÊö·û¼¯ºÏ
execeptfds ÆäËûÎļþÃèÊö·û¼¯ºÏ
timeout£º ³¬Ê±Ê±¼ä
³¬Ê±Ê±¼ä:
struct timeva l {
long tv_sec;
long tv_usec;
};
·µ»ØÖµ£º³É¹¦£º·µ»ØÖµ´óÓÚ0:¼àÌýµ½ÎļþÃèÊö·ûµÄÖµÓб仯£¨¿É¶Á¿Éд£©·µ»ØֵΪ0 :³¬Ê±,ʧ°Ü£º< 0µÄÖµ¡£
ÏÈ˵Ã÷Á½¸ö½á¹¹Ì壺
select»úÖÆÖÐÌṩÁËÒ»¸öÊý¾Ý½á¹¹ struct fd_set £¬¿ÉÒÔÀí½âΪһ¸ö¼¯ºÏ£¬Êµ¼ÊÉÏÊÇÒ»¸öλͼ£¬Ã¿Ò»¸öÌض¨ÎªÀ´±êÖ¾ÏàÓ¦´óСÎļþÃèÊö·û£¬Õâ¸ö¼¯ºÏÖдæ·ÅµÄÊÇÎļþÃèÊö·û(file descriptor)£¬¼´Îļþ¾ä±ú(Ò²¾ÍÊÇλͼÉϵÄÿһλ¶¼ÄÜÓëÒ»¸ö´ò¿ªµÄÎļþ¾ä±ú(ÎļþÃèÊö·û)½¨Á¢ÁªÏµ£¬Õâ¸ö¹¤×÷ÓɳÌÐòÔ±À´Íê³É)£¬Õâ¿ÉÒÔÊÇÎÒÃÇËù˵µÄÆÕͨÒâÒåµÄÎļþ£¬µ±È»UnixÏÂÈκÎÉ豸¡¢¹ÜµÀ¡¢FIFOµÈ¶¼ÊÇÎļþÐÎʽ£¬È«²¿°üÀ¨ÔÚÄÚ£¬ËùÒÔºÁÎÞÒÉÎÊÒ»¸ösocket¾ÍÊÇÒ»¸öÎļþ£¬socket¾ä±ú¾ÍÊÇÒ»¸öÎļþÃèÊö·û¡£fd_set¼¯ºÏ¿ÉÒÔͨ¹ýһЩºêÓÉÈËΪÀ´²Ù×÷£¬³ÌÐòԱͨ¹ý²Ù×÷4Ààºê£¬À´Íê³É×îfd_setµÄ²Ù×÷£º
void FD_CLR(int fd, fd_set *set);
¹¦ÄÜ£º½«ÎļþÃèÊö·ûfd ´ÓÎļþÃèÊö·û·û¼¯ºÏµ±ÖÐÇå³ý
int FD_ISSET(int fd, fd_set *set);
¹¦ÄÜ£ºÅжÏÎļþÃèÊö·û¼¯ºÏÖеÄÎļþÃèÊö·ûʱºò¿É¶Á¿Éд
void FD_SET(int fd, fd_set *set);
¹¦ÄÜ£º½«fd Ìí¼Óµ½ÎļþÃèÊö·û¼¯ºÏµ±ÖÐ
void FD_ZERO(fd_set *set);
¹¦ÄÜ£ºÇå¿ÕÕû¸öÎļþÃèÊö·û¼¯ºÏ¡£
fd_set £º ÉèÖÃÎļþÃèÊö·û·û¼¯ºÏ
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/time.h> #include <sys/select.h>
#define IP "192.168.11.111" #define PROT 10086 #define N 1024
//tcp ·þÎñÆ÷ int main( ) { int file = 0; int sendfile = 0; int readsize = 0; int fd = 0; int filesize = 0; off_t filelength = 0; int newid; int ret; int retsend; int socketfd; struct sockaddr_in addr; socklen_t addrlen; char buf[10] = {'0'};
//½¨Á¢socket socketfd = socket(AF_INET, SOCK_STREAM,0); if(socketfd < 0) { printf("socket is error\r\n"); return -1; }
memset(&addr,0,sizeof(addr));
addrlen = sizeof(addr); addr.sin_family = AF_INET; addr.sin_port = htons(PROT); //´æ·Å¶Ë¿Ú addr.sin_addr.s_addr = inet_addr(IP);// 0.0.0. Ìî³äÈÎÒâµÄIP
//bind ¶Ë¿ÚÓë IP ret = bind(socketfd,(struct sockaddr *)&addr,addrlen); if(ret < 0) { printf("bind is error\r\n"); return -1; }
printf("bind is ok!\r\n");
//listen ret = listen(socketfd,15); if(ret < 0) { printf("listen is error\r\n"); return -1; } printf("listen.............\r\n");
memset(&addr,0,sizeof(addr)); //½ÓÊÕ¿Í»§¶ËµÄÎļþÃèÊö·û Èç¹û·þÎñÓë¿Í»§¶ËÁ´½Ó³É¹¦»á²úÉúÒ»¸öеÄÎļþÃèÊö·û
//ÉèÖÃÎļþÃèÊö·ûµÄ¼¯ºÏ fd_set readfds;
//Çå¿ÕÎļþÃèÊö·û¼¯ºÏ FD_ZERO(&readfds);
//Ìí¼ÓÎļþÃèÊö·ûµ½¼¯ºÏµ±ÖÐ FD_SET(socketfd,&readfds);
// ÉèÖÃ×î´óÎļþÃèÊö·û + 1 int MAXFDS = socketfd;
while(1) {
fd_set tmp = readfds; ret = select(MAXFDS+1,&tmp,NULL,NULL,NULL); if(ret < 0) { printf("select is error\r\n"); return -1; }else if(ret == 0) { printf("time out\r\n"); break; }else {
int i; for(i=1;i<MAXFDS+1;i++) {
if(FD_ISSET(socketfd,&tmp)&&socketfd==i) {
newid = accept(socketfd,(struct sockaddr *)&addr,&addrlen); if(newid < 0) { printf("accept is error\r\n"); return -1; }else { // ½«Ð²úÉúµÄÎļþÃèÊö·ûÌí¼Óµ½ readÎļþÃèÊö·û¼¯ºÏµ±ÖÐ FD_SET(newid,& readfds); //¸üÐÂ×î´óÎļþÃèÊö MAXFDS = (newid > socketfd ) ? newid :socketfd; printf("client:%d\r\n",newid); } } else if(FD_ISSET(i,&tmp)) { retsend = recv(i,buf,sizeof(buf),0); if(retsend > 0) { printf("buf:%s\r\n",buf); } else if(retsend <= 0) { FD_CLR(newid, &readfds); close(newid); printf("close:%d\r\n",newid); break;
}
} } } } close(socketfd); close(newid);
return 0; }
|