Linux Select Code Framework

Source: Internet
Author: User

#definePORT 1234#defineBACKLOG 5#defineMaxdatasize 1000typedefstructclient{intFD; Char*name; structSockaddr_in addr;//addr with client_addr more accurate   Char*data; }; voidMain () {intI,MAXI,MAXFD,SOCKFD;inchnready; fd_set rset,allset;//Allset is a set of FD we want to monitor, because each call to select will change the state of the FD set monitored, so when you call again, you need to reset the FD collection, where you can use Allset to maintain the monitoring set, each call with RSet. The only change is RSet.ssize_t N;intLISTENFD.CONNFD;structSockaddr_in server_addr, client_addr;//Note that here we keep the client's information in the client structure,CLIENT Client[fd_setsize];//Fd_setsize is a macro constant CharBuf[maxdatasize];intsin_size;if(Listenfd=socket (Af_inet,sock_stream,0)==-1) {perror ("Create socket failed"); Exit (1); } bzero (&AMP;SERVER_ADDR,sizeof(SERVER_ADDR)); server_addr.sin_family=Af_inet;server_addr.sin_port=htons (PORT); Server_addr.sin_addr.s_addr=htonl (inaddr_any);if(Bind (LISTENFD, (structsockaddr*) &server_addr,sizeof(structSOCKADDR) ==-1) {perror ("bind socket failed"); Exit (1); }if(Listen (Listenfd,backlog) ==-1) {perror ("Listen socket failed"); Exit (1);} Sin_size=sizeof(structsockaddr_in) Maxfd=LISTENFD;//the largest file descriptormaxi=-1;//Maximum used array subscript for a client for(i=0; i<fd_setsize;i++) {CLIENT[I].FD=-1; }//initializing the client arrayFd_zero (&allset); Fd_set (LISTENFD,&allset);//Join the Monitor  while(1){ structSockaddr_in addr;//information to return to the client sideRset=allset;//re-assign value againnready=Select(maxfd+1, &rset,null,null,null)//This tests the FD read-ready, and writes the fdset to null   if(Fd_isset (Listenfd,&allset)) {//New Client Connection    if(Connfd=accept (LISTENFD, (structsockaddr*) (&addr,&sin_size)) ==-1) {perror ("Accept () error\n"); Exit (1); Continue; }   for(i=0; i<fd_setsize;i++)     if(client[i].fd<0) {CLIENT[I].FD=CONNFD; Client[i].name=New Char[Maxdatasize]; Client[i].addr=ADDR;//socket address information on the customer service sideprintf"You got a connection from client");  Break; }          if(i==fd_setsize) printf ("Too many connection")//; The number of connections to FD has exceeded the maximumFd_set (Connfd,&allset);//add CONNFD to the listening set   if(CONNFD&GT;MAXFD) MAXFD=CONNFD;//Update the current largest FD;   if(I>maxi) maxi=i; if(--nready<=0)Continue;//The previous select returns when only one listener listen is ready, then continues the loop while (1), the following program does not execute}//corresponds to if (Fd_isset (listenfd,&allset))    for(i=0; i<=maxfd;i++){     if((sockfd=client[i].fd<0))Continue; if(Fd_isset (Sockfd,&rset)) {//in two cases, one is that this FD becomes readable due to a closed connection       if(N=recv (Sockfd,recvbuf,maxdatasize,0))==0) {close (SOCKFD);//Close the connection to the corresponding server socketFD_CLR (Sockfd,&allset);//Remove from the monitored FD collection                ..................                 ...................         } ElseProcess (&client[i],revbuf,n)//Otherwise it will be read ready to do our work       if(--nready<=0) Break;//Finish all the ready descriptors}}} close (LISTENFD);//Bring up while}                 

Linux Select Code Framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.