Basic knowledge about Select
A. Select is a system-supplied multiplexed input-output model. It is used to monitor the state changes of multiple file handles.
B. The program will stop at select and so on until at least one of the monitored file handles has changed state.
C. File handle is an integer
Function
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/01/wKiom1dG_begWcr6AABAizofHMA736.png "title=" capture. PNG "alt=" Wkiom1dg_begwcr6aabaizofhma736.png "/>
A. Parameters
Nfds: The maximum file descriptor value to be monitored plus 1;
The struct TIMEVAL structure is used to describe a length of time, and if this time is exceeded, the descriptor that needs to be monitored does not occur and returns 0.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/81/FF/wKioL1dHAD2SqbumAAAnuwXy2EI040.png "title=" capture. PNG "alt=" Wkiol1dhad2sqbumaaanuwxy2ei040.png "/>
3. Code implementation
21 // 22 int sock=socket (AF_INET, sock_stream,0); 23 if (sock<0) 24 { 25 perror ("socket"); 26 exit (1); 27 } 28 // 28 // 29 struct sockaddr_in local; 30 local.sin_family=AF_INET; 31 local.sin_port=htons (port); 32 LOCAL.SIN_ADDR.S_ADDR=INET_ADDR (IP); 33 // 34 if (Bind (sock, (struct sockaddr*) &local,sizeof (local)) <0) 35 { 36 perror ("Bind"); 37 Exit (2); 38 } 39 // 40 if (Listen (Sock,_backlog_) <0) 41 { 42 perror ("Listen"); 43 exit (3); 44 } 45 return Sock; 46 } 47 int main (int argc,char*argv[]) 48 { 49 if (argc!=3) 50 { 51 usage (Argv[0]); 52 exit (1); 53 } 54 55 char* ip=argv[ 1]; 56 &nBsp; int port=atoi (argv[2]); 57 58 int Listen_sock=startup (ip,port); 59 60 int done=0; 61 int new_sock=-1; 62 struct sockaddr_ in client; 63 socklen_t len=sizeof (client); 64 65 int max_fd; 66 fd_set _reads; 67 fd_set _writes; 68 69 int i=0; 70 int fds_num=sizeof (FDS)/sizeof (fds[0]); 71 for (; i<fds_num;++i) 72 { 73 fds[i]=-1; 74 } 75 fds[0]=listen_sock; 76 max_fd=fds[0]; 77 78 while (!done) 79 { 80 fd_zero (&_reads); 81 fd_zero (&_writes) ; 82 fd_set (listen_sock,&_reads); 83 struct timeval _timeout={5,0}; 84 for (i=1;i<fds_num;++i) 85 { 86 if (fds[i]>0) 87 { 88 Fd_set (fds[i],&_reads); &nbSp;89 if ( FDS[I]>MAX_FD) 90 { 91 max_fd=fds[i]; 92 } 93 } 94 } 95 switch (SELECT (max_fd+1,&_reads,&_ Writes,null,null)) 96 { 97 case 0: 98 //timeout 99 printf ("timeout\n");100 break;101 case -1:102 perror ("select");103 break;104 default:105 {106 i=0;107 for (; i<fds_num;++i) 108 {109 if (Fds[i]==listen_sock&&fd_isset (fds[i],&_ reads)) 110 {111 //listen socket is ready!112 new_sock=accePT (Listen_sock, (struct sockaddr*) &client,&len);113 if (new_sock<0) 114 { 115 perror ("Accept ");116 continue;117 }118 printf ("get a new connect...%d\n", New_sock);119 for (i=0;i<fds_num;++i) 120 {121 if (fds[i ]==-1) 122 {123 fds[i]=new_sock;124 break; 125 }126 }127 &nBsp; if (I==fds_ NUM) 128 {129 close (New_sock);130 }131 }132 //listen socket133 else if (fds[i]>0 && fd_ ISSET (fds[i],&_reads)) 134 {135 char buf[1024];136 ssize_t _s=read (fds[i],buf,sizeof (BUF)-1);137 if (_s>0) 138 {139 //read sucess140 buf[_s]= ';141 printf ("client:%s\n", buf);142 }143 else if (_s==0) 144 {145 //client shutdown146 printf ("client shutdown...\n"); 147 &nbsP; close (Fds[i]);148 fds[i]=-1;149 }150 else151 {152 perror ("read");153 }154 }155 //normal socket156 else157 {}158 }159 }160 break;161 }162 }163 return 0;164 }
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1784034
select--of I/O multiplexing based on TCP protocol