Linux io mode and select, poll, Epoll detailed

Source: Internet
Author: User

Original: 1190000003063859

I've only extracted the Epoll code example, the server-side code

#defineIPADDRESS "127.0.0.1"#definePORT 8787#defineMAXSIZE 1024#defineListenq 5#defineFdsize 1000#defineEpollevents 100LISTENFD=Socket_bind (ipaddress,port);structepoll_event events[epollevents];//Create a descriptorEPOLLFD =epoll_create (fdsize);//Adding a Listener descriptor eventadd_event (Epollfd,listenfd,epollin);//Loop Wait for ( ; ; ) {    //The function returns the number of descriptor events that have been preparedret = epoll_wait (epollfd,events,epollevents,-1); //processing the received connectionhandle_events (EPOLLFD,EVENTS,RET,LISTENFD,BUF);}//event handler functionStatic voidHandle_events (intEPOLLFD,structEpoll_event *events,intNumintLISTENFD,Char*buf) {     inti; intFD; //to traverse, as long as the prepared IO events are traversed. Num was not fdsize at the time of Epoll_create.       for(i =0; I < num;i++) {FD=EVENTS[I].DATA.FD; //processing based on the type and event type of the descriptor         if(FD = = LISTENFD) && (Events[i].events &Epollin))         Handle_accpet (EPOLLFD,LISTENFD); Else if(Events[i].events &Epollin) Do_read (EPOLLFD,FD,BUF); Else if(Events[i].events &epollout) Do_write (EPOLLFD,FD,BUF); }}//Add EventStatic voidAdd_event (intEPOLLFD,intFdintState ) {    structepoll_event ev; Ev.events=State ; EV.DATA.FD=FD; Epoll_ctl (EPOLLFD,EPOLL_CTL_ADD,FD,&ev);}//handles the received connection. Add a file descriptor to the Epoll monitoring queueStatic voidHandle_accpet (intEPOLLFD,intLISTENFD) {     intCLIFD; structsockaddr_in cliaddr;          Socklen_t Cliaddrlen; CLIFD= Accept (LISTENFD, (structsockaddr*) &cliaddr,&Cliaddrlen); if(CLIFD = =-1) perror ("Accpet Error:"); Else{printf ("accept a new client:%s:%d\n", Inet_ntoa (CLIADDR.SIN_ADDR), cliaddr.sin_port);//Add a customer descriptor and eventadd_event (Epollfd,clifd,epollin); } }//Read ProcessingStatic voidDo_read (intEPOLLFD,intFdChar*buf) {    intnread; Nread=read (fd,buf,maxsize); if(Nread = =-1) {perror ("Read error:"); Close (FD); //remember the close FDDelete_event (Epollfd,fd,epollin);//Delete Listener    }    Else if(Nread = =0) {fprintf (stderr,"Client close.\n"); Close (FD); //remember the close FDDelete_event (Epollfd,fd,epollin);//Delete Listener    }         Else{printf ("Read message is:%s", BUF); //modifies the event corresponding to the descriptor, changed from read to writemodify_event (epollfd,fd,epollout); } }//Write ProcessingStatic voidDo_write (intEPOLLFD,intFdChar*buf) {         intNwrite; Nwrite=Write (Fd,buf,strlen (BUF)); if(Nwrite = =-1) {perror ("Write Error:");   Close (FD); //remember the close FDDelete_event (epollfd,fd,epollout);//Delete Listener}Else{modify_event (Epollfd,fd,epollin); } memset (BUF,0, MAXSIZE); }//Delete EventStatic voidDelete_event (intEPOLLFD,intFdintState ) {    structepoll_event ev; Ev.events=State ; EV.DATA.FD=FD; Epoll_ctl (EPOLLFD,EPOLL_CTL_DEL,FD,&ev);}//Modifying EventsStatic voidModify_event (intEPOLLFD,intFdintState ) {         structepoll_event ev; Ev.events=State ; EV.DATA.FD=FD; Epoll_ctl (EPOLLFD,EPOLL_CTL_MOD,FD,&ev);}//Note: I'll save the other end.

Linux io mode and select, poll, epoll details (RPM)

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.